Home > Backend Development > PHP Tutorial > 懂java和php来,aes加解密将java版转为php版

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-02 11:32:53
Original
951 people have browsed it

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;
}

<code>    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;}</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template