日期:2014-05-20  浏览次数:20852 次

java Scanner是否和其他流冲突?新人求解
本帖最后由 u011751266 于 2013-09-22 22:29:05 编辑
下面是代码。我们老师要求应用DES算法。于是就根据自己查资料什么的写了一下。想用Scanner包装System.in来获取数字,但是在运行的时候有很多问题。
package com.gmail.jennis19118;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Scanner;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import java.io.*;

import sun.misc.*;


思路:
1、生成密钥
generateKey()
2、加密方法
encrypt()
3、解密方法
decrypt()
4、主函数
*/

public class DESDemo {

/**
 * @param args
 * @throws NoSuchPaddingException 
 * @throws NoSuchAlgorithmException 
 */
private static final String ALGORITHM = "DESede";
private static final String KEYFILE = "keyFile";
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, FileNotFoundException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingExceptionIOExceptionClassNotFoundException{
// TODO Auto-generated method stub
/*a.)1表示加密 b.)2表示解密 c.)可重复执行 d.)选择0退出*/

/* this is a test
String source = "hello,world";
String encrypted_source = encrypt(source);
System.out.println("encrypted String is:\t"+encrypted_source);

String decrypted_source = decrypt(encrypted_source);
System.out.println("decrypted String is:\t"+decrypted_source);
*/

System.out.println("enter 1 to encrypt.\tenter 2 to decrept.\tenter 0 to quit.");
                /*我想用Scanner(System.in)来获取choose的值。但是没做到*/
int choose = 1;
while(true)
{
switch(choose)
{
case 1:
String source = input_source();
String encrypted_source = encrypt(source);
System.out.println("encrypted String is:\t"+encrypted_source);
write_tofile(encrypted_source);
choose = 2;
break;
case 2:
String str;
BufferedReader in
   = new BufferedReader(new FileReader("encryptedSource.txt"));