package com.ryx.frame.security.aes;
import java.security.SecureRandom;
import java.util.Date;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import org.apache.commons.codec.binary.Base64;
public class KeyGeneral{
/**
* AES 128,256
* DEA 56
*/
//===========================加密方式======================\\
private static final String ALGORITHM_HMACMD5="HmacMD5";
private static final String ALGORITHM_AES="AES";
private static final String ALGORITHM_DES="DES";
//===========================加密类型于算法无关======================\\
//默认
public static String initDefault() {
String key = "";
try {
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM_HMACMD5);
SecretKey secretKey=generator.generateKey();
byte[]bytes=encode(secretKey.getEncoded());
key=new String(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return key;
}
//安全随机种子
public static String initAESKey(int keysize,long seed) {
String key = "";
try {
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM_AES);
SecureRandom random=new SecureRandom();
random.setSeed(seed);
generator.init(keysize, random);
SecretKey secretKey=generator.generateKey();
byte[]bytes=encode(secretKey.getEncoded());
key=new String(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return key;
}
//安全随机种子
public static String initDESKey(int keysize,long seed) {
String key = "";
try {
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM_DES);
SecureRandom random=new SecureRandom();
random.setSeed(seed);
generator.init(keysize, random);
SecretKey secretKey=generator.generateKey();
byte[]bytes=encode(secretKey.getEncoded());
key=new String(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return key;
}
//===========================base64======================\\
public static byte[] encode(byte[]plainBytes){
Base64 base64=new Base64();
return base64.encode(plainBytes);
}
public static byte[] decode(byte[]cipherText){
Base64 base64=new Base64();
return base64.decode(cipherText);
}
//============================test========================\\
public static void main(String[] args) {
System.out.println("加载默认配置算法为HmacMD5__:"+initDefault());
System.out.println("加载默认配置算法为AES__:"+initAESKey(256,new Date().getTime()));
System.out.println("加载默认配置算法为DES__:"+initDESKey(56,new Date().getTime()));
}
}
//=====================运行结果
加载默认配置算法为HmacMD5__:1MeVXQZkMWHPy0VLgpwrO44AIfFq2Y+D6gDqPz7BSLQ3iaZOR18DBI5yaSYtCTEXRQYQpwYV5d1UWVefd6v58A==
加载默认配置算法为AES__:5pUQCt/CLNK44vL+Nm7mgC7IcX1QfGnVc5BwqOrvk7M=
加载默认配置算法为DES__:FnldAm04/dY=