直接上代码,拿来就能用那种!
Java 端 aes 加密代码,如下:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
*
* @ClassName: AESToPhpUtil
* @Description: AES与php互通加解密工具类
* @author: Stone
* @date: 2019年4月17日
*
*/
public class AESToPhpUtil {
// 约定加密规则
private static final String KEY_ALGORITHM = "AES";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/NoPadding";
/**
* AES加密
*
* @param data 待加密数据
* @param key 算法要求16位
* @param iv 算法要求16位
* @return
*/
public static String encryptData(String data, String key, String iv) {
try {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new String(Base64.encodeBase64(encrypted));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* AES解密
*
* @param data 待加密数据
* @param key 算法要求16位
* @param iv 算法要求16位
* @return
*/
public static String decryptData(String data, String key, String iv) {
try {
byte[] encrypted = Base64.decodeBase64(data.getBytes());
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), KEY_ALGORITHM);
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String secret = "1GStYafrhv7cBJ4KzXQPmEui63DO258N";
String key = secret.substring(0, 16);// key值取密钥前16位(可以任意截取其中16位,只要和对接方约定好即可)
String iv = secret.substring(16, 32);// 随机iv值取密钥后16位(可以任意截取其中16位,只要和对接方约定好即可)
String data = "AES加解密JAVA与PHP互通";
String enStr = AESToPhpUtil.encryptData(data, key, iv);
System.out.println("加密后:" + enStr);
String deStr = AESToPhpUtil.decryptData(enStr, key, iv);
System.out.println("解密后:" + deStr);
}
}
运行效果,如下:
加密后:I0E/I/D3xXJI18cfqJP8023ml6dC/F62IRJ8qVEh2Ew=
解密后:AES加解密JAVA与PHP互通
php 端加解密,如下:
<?php
$secret = "1GStYafrhv7cBJ4KzXQPmEui63DO258N";
$privateKey = substr($secret,0,15); // key值取密钥前16位(可以任意截取其中16位,只要和对接方约定好即可)
$iv = substr($secret,16,32); // 随机iv值取密钥后16位(可以任意截取其中16位,只要和对接方约定好即可)
$data = "AES加解密JAVA与PHP互通";
//加密
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);
echo(base64_encode($encrypted));
echo '<br/>';
//解密
$encryptedData = base64_decode(base64_encode($encrypted));
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
echo($decrypted);
?>
python 端加解密,如下:
# -*- coding:utf-8 -*-
import base64
from Crypto.Cipher import AES
import json
# 秘钥
key = "1GStYafrhv7cBJ4K"
iv = "zXQPmEui63DO258N"
def add_to_16(s):
"""
判断str不是16的倍数,不足就补足为16的倍数
:param s: 传入的原始字符串
:return: 返回是16倍的字符串 bytes
"""
while len(s) % 16 != 0:
s += '\0'
return str.encode(s)
def aes_encrypt(s):
"""
AES加密
:param s: 传入的原始字符串
:return: 加密后字符串
"""
aes = AES.new(add_to_16(key), AES.MODE_CBC, add_to_16(iv)) # 初始化加密器
en = str(base64.encodebytes(aes.encrypt(add_to_16(s))), encoding='utf8').replace('\n', '')
return en
def aes_descrypt(s):
"""
AES解密
:param s: 传入的原始字符串
:return: 解密后字符串
"""
aes = AES.new(add_to_16(key), AES.MODE_CBC, add_to_16(iv)) # 初始化加密器
de = str(aes.decrypt(base64.decodebytes(bytes(s, encoding='utf8'))).rstrip(b'\0').decode("utf8"))
return de
if __name__ == '__main__':
data_str = "{'pageNo': 1, 'pageSize': 20}" # 字符串
data_dict = json.dumps(data_str) # 字典
en = aes_encrypt(data_dict)
print("加密后的数据: " + en)
de = aes_descrypt(en)
print("解密后的数据: " + de)
有任何问题欢迎跟帖交流 0.0
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于