java需要下载外部包, commons codec.jar 1.6 較新的JAVA版本把Base64的方法改成靜態方法,可能會寫成
Base64.encodeToString(encrypted, 0);
Base64.encodeToString(encrypted, 0);
import
javax.crypto.Cipher;
import
javax.crypto.spec.SecretKeySpec;
import
org.apache.commons.codec.binary.Base64;
/**
*
* @author Administrator
*
*/
public
class
AES {
// 加密
public
static
String Encrypt(String sSrc, String sKey)
throws
Exception {
if
(sKey ==
null
) {
System.out.print(
"Key为空null"
);
return
null
;
}
// 判断Key是否为16位
if
(sKey.length() !=
16
) {
System.out.print(
"Key长度不是16位"
);
return
null
;
}
byte
[] raw = sKey.getBytes(
"utf-8"
);
SecretKeySpec skeySpec =
new
SecretKeySpec(raw,
"AES"
);
Cipher cipher = Cipher.getInstance(
"AES/EC