AES加密是一总比较常用的,会补足长度的加密方式,不管你长度为多少,加密完之后都会变成128位(仅指这里,当然也有其他的AES)。
敲黑板:之前做过一次跨语言的加密解密,发现是不可以的。即java php c++之间不同的加密解密不要通用,因为进制和编码的问题,如果强行使用的话。需要二进制十六进制八进制各种换算 - -。 我这么懒,当然没有去做。
而且任何加密后的内容是没有办法看到的,如果要显示,统统需要base64处理。
代码片如下:
public static byte[] AESdecrypt(byte[] input , String key){
byte[] output = null
try{
javax.crypto.spec.SecretKeySpec skey = new javax.crypto.spec.SecretKeySpec(key.getBytes(), "AES")
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/ECB/PKCS5Padding")
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, skey)
output = cipher.doFinal(input)
} catch (Exception e) {
System.out.println(e.toString())
}
return output
}
//AES的加密部分
public static byte[] AESencrypt(byte[] input , String key) throws Exception{
byte[] crypted = null
try{
javax.crypto.spec.SecretKeySpec skey = new javax.crypto.spec.SecretKeySpec(key.getBytes(), "AES")
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/ECB/PKCS5Padding")
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, skey)
crypted = cipher.doFinal(input)
}catch(Exception e){
System.out.println(e.toString())
}
return crypted
}
//base64解密部分
public static String Base64decrypt(String input){
return new String(org.apache.commons.codec.binary.Base64.decodeBase64(input.getBytes()))
}
//base64加密部分
public static String Base64encrypt(String input){
return new String(org.apache.commons.codec.binary.Base64.encodeBase64(input.getBytes()))
}