JAVA中AES的加密解密算法与PYTHON加密解密一致
加密: AES加密再通过base64编码
解密: base64解码再通过AES解码
Java中AES的加密解密算法
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AESencrpUtils {
private static final Logger LOG = LoggerFactory.getLogger(AESencrpUtils.class);
public static void main(String[] args) {
// 创建加解密
AESencrpUtils aes = new AESencrpUtils();
// 要进行加密的密码
String passwordEnc = aes.encrypt("itdaima.com本人博客");
System.out.println("加密后的原密码 : " + passwordEnc);
// password = password.replace("13293745", "+").replace("27460381", "=").replace("53823629", "/");
// 进行加密后的字符串
String passwordDec = aes.decrypt(passwordEnc);
System.out.println("解密后的原密码 : " + passwordDec);
}
/**
* 用来进行加密的操作
*
* @param Data
* @return
*/
public String encrypt(String Data) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
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("abcdef0123456789".getBytes("utf-8"), "AES");
IvParameterSpec ivspec = new IvParameterSpec("0123456789abcdef".getBytes("utf-8"));
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
String EncStr = new sun.misc.BASE64Encoder().encode(encrypted);
return EncStr;
} catch (Exception e) {
LOG.error("AES加密错误", e);
}
}
/**
* 用来进行解密的操作
*
* @param encryptedData
* @return
*/
public String decrypt(String encryptedData) {
try {
//替换相关字符
encryptedData = encryptedData.replace("13293745", "+").replace("27460381", "=")
.replace("53823629", "/");
byte[] encrypted1 = new sun.misc.BASE64Decoder().decodeBuffer(encryptedData);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keyspec = new SecretKeySpec("abcdef0123456789".getBytes("utf-8"), "AES");
IvParameterSpec ivspec = new IvParameterSpec("0123456789abcdef".getBytes("utf-8"));
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
LOG.error("AES解密错误", e);
}
}
}
Python中AES的加密解密算法
安装 python 依赖 Crypto 和 base64
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import base64
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
AES_KEY = "abcdef0123456789".encode("utf-8")
IV = "0123456789abcdef".encode("utf-8")
def encrypt(str):
aes = AES.new(AES_KEY, AES.MODE_CBC, IV)
str = str + ('\0' * (16 - len(str.encode("utf-8")) % 16))
str = aes.encrypt(str.encode("utf-8"))
str = base64.b64encode(str).decode()
print str
return str.replace("+", "13293745").replace("=", "27460381").replace("/", "53823629")
# return str
def decrypt(str):
aes = AES.new(AES_KEY, AES.MODE_CBC, IV)
str = str.replace("13293745", "+").replace("27460381", "=").replace("53823629", "/")
str = base64.b64decode(str)
str = aes.decrypt(str).decode().strip('\x02').strip('\x03').strip('\x04').strip('\x05').strip(
'\x06').strip('\x07').strip('\x08').strip('\x09').rstrip('\0').strip()
return str
if __name__ == '__main__':
encrypt1 = encrypt('itdaima.com本人博客')
print encrypt1
encrypt1 = decrypt(encrypt1)
print encrypt1