【发布时间】:2019-10-27 07:55:21
【问题描述】:
我有这个 android java AES 加密代码来制作我的令牌,现在我也想在 python 中制作它,但是它有一些不同的结果。
java
private Void encrypt(String password) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, BadPaddingException, IllegalBlockSizeException {
byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
SecretKeySpec secretKeySpec = new SecretKeySpec(MessageDigest.getInstance("MD5")
.digest("mysecretkey".getBytes(StandardCharsets.UTF_8)), "AES");
Cipher instance = Cipher.getInstance("AES/ECB/PKCS5Padding");
instance.init(1, secretKeySpec);
byte[] bArr = new byte[instance.getOutputSize(bytes.length)];
instance.doFinal(bArr, instance.update(bytes,0,bytes.length, bArr, 0));
for (byte b : bArr){
Log.d("encrypt", String.valueOf(b));
}
}
蟒蛇
from Crypto.Cipher import AES
import hashlib
def pad(byte_array):
BLOCK_SIZE = 16
pad_len = BLOCK_SIZE - len(byte_array) % BLOCK_SIZE
return byte_array + (bytes([pad_len]) * pad_len)
def encrypt(key, message):
byte_array = message.encode("UTF-8")
panjang = len(message)
padded = pad(byte_array)
cipher = AES.new(key.encode("UTF-8"), AES.MODE_ECB)
encrypted = cipher.encrypt(padded)
for b in encrypted:
print(b)
password = "mypassword"
secret_key = "mysecretkey"
hashkey = hashlib.md5(secret_key.encode()).hexdigest()
encrypt(hashkey,password)
java结果
-25 -16 84 -36 100 -102 74 -98 -91 -77 100 -96 -86 28 -47 -67
python 结果
220 127 95 142 45 102 9 79 170 82 165 2 63 39 196 7
我已经解决了几天,但看不出问题出在哪里。
【问题讨论】:
标签: java python android encryption aes