日期:2014-05-20 浏览次数:20919 次
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, 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"));