AES対称暗号化と復号化
25000 ワード
/*
* AES
*/
public class SymmetricEncoder {
/*
*
* 1.
* 2. ecnodeRules
* 3.
* 4.
* 5.
* 6.
*/
public static String AESEncode(String encodeRules,String content){
try {
//1. , AES ,
KeyGenerator keygen= KeyGenerator.getInstance("AES");
//2. ecnodeRules
// , Linux
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(encodeRules.getBytes());
// 128 ,
//keygen.init(128, new SecureRandom(encodeRules.getBytes()));
// ,
keygen.init(128, secureRandom);
//3.
SecretKey original_key=keygen.generateKey();
//4.
byte [] raw=original_key.getEncoded();
//5. AES
SecretKey key=new SecretKeySpec(raw, "AES");
//6. AES
Cipher cipher=Cipher.getInstance("AES");
//7. , (Encrypt_mode) (Decrypt_mode) , KEY
cipher.init(Cipher.ENCRYPT_MODE, key);
//8. ( utf-8)
byte [] byte_encode=content.getBytes("utf-8");
//9. -- :
byte [] byte_AES=cipher.doFinal(byte_encode);
//10.
// Base64Encoder
// :
// Build path JRE System Library, JRE System Library, 。
String AES_encode=new String(new BASE64Encoder().encode(byte_AES));
//11.
return AES_encode;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// nulll
return null;
}
/*
*
* :
* 1. 1-4
* 2. byte[]
* 3.
*/
public static String AESDncode(String encodeRules,String content){
try {
//1. , AES ,
KeyGenerator keygen=KeyGenerator.getInstance("AES");
//2. ecnodeRules
// , Linux
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(encodeRules.getBytes());
// 128 ,
//keygen.init(128, new SecureRandom(encodeRules.getBytes()));
// ,
keygen.init(128, secureRandom);
//3.
SecretKey original_key=keygen.generateKey();
//4.
byte [] raw=original_key.getEncoded();
//5. AES
SecretKey key=new SecretKeySpec(raw, "AES");
//6. AES
Cipher cipher=Cipher.getInstance("AES");
//7. , (Encrypt_mode) (Decrypt_mode) , KEY
cipher.init(Cipher.DECRYPT_MODE, key);
//8.
byte [] byte_content= new BASE64Decoder().decodeBuffer(content);
/*
*
*/
byte [] byte_decode=cipher.doFinal(byte_content);
String AES_decode=new String(byte_decode,"utf-8");
return AES_decode;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
// nulll
return null;
}
public static void main(String[] args) {
SymmetricEncoder se=new SymmetricEncoder();
Scanner scanner=new Scanner(System.in);
/*
*
*/
System.out.println(" AES , ");
String encodeRules=scanner.next();
System.out.println(" :");
String content = scanner.next();
System.out.println(" "+encodeRules+" :"+se.AESEncode(encodeRules, content));
/*
*
*/
System.out.println(" AES , :( )");
encodeRules=scanner.next();
System.out.println(" ( ):");
content = scanner.next();
System.out.println(" "+encodeRules+" :"+se.AESDncode(encodeRules, content));
// String encodeRules="xingz";
// String contentIn = "hahaha";
// System.out.println(" "+encodeRules+" :"+se.AESEncode(encodeRules, contentIn));
// System.out.println(" "+encodeRules+" :"+se.AESDncode(encodeRules, se.AESEncode(encodeRules, contentIn)));
}
}