java Scanner是否和其他流冲突?新人求解
本帖最后由 u011751266 于 2013-09-22 22:29:05 编辑 下面是代码。我们老师要求应用DES算法。于是就根据自己查资料什么的写了一下。想用Scanner包装System.in来获取数字,但是在运行的时候有很多问题。
package com.gmail.jennis19118;加密 解密 流 Scanner
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, BadPaddingException, IOException, ClassNotFoundException{
// 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"));
str = in.readLine();
in.close();
String decrypted_source = decrypt(str);
System.out.println("decrypted String is:\t"+decrypted_source);
choose = 0;
break;
case 0:
System.out.println("program exit.");
System.exit(0);
default:
System.out.println("bad input!");
}
}
}
/*生成密钥*/
public static void generateKey() throws NoSuchAlgorithmException, FileNotFoundException, IOException
{
/*DES算法要求有一个可信任的随机数源*/
SecureRandom sr = new SecureRandom();
/*为DES算法创建一个keyGenerator对象*/
KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
/*初始化keyGenerator对象*/
kg.init(sr);
/*生成密钥*/
/*SecretKey extends Key*/
SecretKey key = kg.generateKey();
/*使用对象流将生成的密钥写入文件keyFile*/
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(KEYFILE));
oos.writeObject(key);
oos.close();
}
/*加密方法*/
public static String encrypt(String source) throws NoSuchAlgorithmException, FileNotFoundException, IOException, ClassNotFoundException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
/*生成一个密钥并写入文件*/
generateKey();
/*读取这个密钥*/
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(KEYFILE));
SecretKey key = (SecretKey)ois.readObject();
/*得到Cipher的对象来实现对源数据的DES加密*/
Cipher cip = Cipher.getInstance(ALGORITHM);
/*对Cipher对象cip初始化*/
cip.init(Cipher.ENCRYPT_MODE, key);
/*将源数据编码成字节串存入字节数组*/
byte[] b1 = source.getBytes();
/*执行加密操作*/
byte[] b2 = cip.doFinal(b1);
//使用sun.misc包中BASE64Encoder类的encode方法对数据进行加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b2);
}
/*解密方法*/
public static String decrypt(String encrypted_source) throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
/*读取密钥*/
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(KEYFILE));
SecretKey key = (SecretKey)ois.readObject();
/*获得Cipher对象来实现对源数据的DES解密*/
Cipher cip = Cipher.getInstance(ALGORITHM);
cip.init(Cipher.DECRYPT_MODE, key);
//同理对BASE64加密后的数据进行base64解密
BASE64Decoder decoder = new BASE64Decoder();
byte[] b1 = decoder.decodeBuffer(encrypted_source);
/*解密*/
byte[] b2 = cip.doFinal(b1);
return new String(b2);
}
/*获取用户的键盘输入的字符串*/
public static String input_source() throws IOException
{
String source = null;
System.out.println("please enter a string to encrypt.");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
source = in.readLine();
in.close();
System.out.println("this is the source string you entered:\t"+source);
return source;
}
/*将加密后的数据存入到文件中*/
public static void write_tofile(String encrypted_source) throws IOException
{
PrintWriter out
= new PrintWriter(new BufferedWriter(new FileWriter("encryptedSource.txt")));
out.write(encrypted_source);
out.close();
System.out.println("successfully saved encrypted_source to file.");
}
}
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
usage();
while(scanner.hasNextInt()){
int i = scanner.nextInt();
switch (i) {
case 1: System.out.println(i); break;
case 2: System.out.println(i); break;
case 0: System.exit(0); break;
default:
usage(); continue;
}
}
}
private static void usage() {
System.out.printf("enter 1 to encrypt.%nenter 2 to decrept.%nenter 0 to quit.%n");
}
}