欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

懂java和php来,aes加解密将java版转为php版

程序员文章站 2024-02-05 17:11:16
...
phpjavaaes

/**
* AES加密
*
* @param key
* 密钥信息
* @param content
* 待加密信息
*/
public static byte[] encodeAES(byte[] key, byte[] content) throws Exception {
// 不是16的倍数的,补足
int base = 16;
if (key.length % base != 0) {
int groups = key.length / base + (key.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(key, 0, temp, 0, key.length);
key = temp;
}

    SecretKey secretKey = new SecretKeySpec(key, "AES");    IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);    byte[] tgtBytes = cipher.doFinal(content);    return tgtBytes;}
相关标签: php java aes