日期:2014-05-20 浏览次数:21218 次
/**
* 数据加密
*
* @param name
* 算法名称
* @param password
* 密钥
* @param message
* 明文
* @return 返回密文
*/
public static byte[] Encode(String name, String password, byte[] message) {
if (name == null || name.length() <= 0
|| password == null || password.length() <= 0
|| message == null || message.length <= 0)
return null;
if (!name.equals(ALGO_AES))
return null;
byte[] content = null;
KeyGenerator kgen = null;
try {
kgen = KeyGenerator.getInstance(name);
byte[] okey = password.getBytes();
kgen.init(128, new SecureRandom(okey));
SecretKey secretKey = kgen.generateKey();
Cipher cipher = Cipher.getInstance(name); // 创建密码器
cipher.init(Cipher.ENCRYPT_MODE, secretKey);// 初始化
content = cipher.doFinal(message);
} 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();
}
return content;
}