gpt4 book ai didi

java - python 和 android 之间的 AES 加密互操作

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:20 27 4
gpt4 key购买 nike

我在 python (Django) 服务器上获取了此代码,用于加密发送给 Java 客户端 (Android) 的消息。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES

class AESCipher:

def __init__(self, key):
self.bs = 16
self.key = hashlib.sha256(key.encode()).digest()

def encrypt(self, raw):
raw = self._pad(raw)
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
cTxt = (iv + cipher.encrypt(raw))
return base64.b64encode(cTxt)

def decrypt(self, enc):
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
dPadded = cipher.decrypt(enc[AES.block_size:])
return self._unpad(dPadded).decode('utf-8')

def _pad(self, s):
return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)

@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]

我用的是这种方式:

    phrase = "Prueba de Encryptado"
key = "e8ffc7e56311679f12b6fc91aa77a5eb"

cryp = AESCipher(key)
eTxt = cryp.encrypt(phrase)
dTxt = cryp.decrypt(eTxt)

在我的 Java-Android 客户端上;我尝试用此类解密:

 public class Crypt {

private static final String tag = Crypt.class.getSimpleName();

private static final String characterEncoding = "UTF-8";
private static final String cipherTransformation = "AES/CBC/PKCS5Padding";
private static final String aesEncryptionAlgorithm = "AES";
private static final String key = "e8ffc7e56311679f12b6fc91aa77a5eb";
private static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
private static byte[] keyBytes;

private static Crypt instance = null;


private Crypt(){}



public static Crypt getInstance() {
if(instance == null){
instance = new Crypt();
}

return instance;
}



public byte[] encrypt( byte[] mes)
throws NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException, IOException {

keyBytes = key.getBytes("UTF-8");
Log.d(tag,"Long KEY: "+keyBytes.length);
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(keyBytes);
keyBytes = md.digest();

Log.d(tag,"Long KEY: "+keyBytes.length);

AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = null;
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);

byte[] destination = new byte[ivBytes.length + mes.length];
System.arraycopy(ivBytes, 0, destination, 0, ivBytes.length);
System.arraycopy(mes, 0, destination, ivBytes.length, mes.length);
return cipher.doFinal(destination);

}

public byte[] decrypt( byte[] bytes)
throws NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException, IOException, ClassNotFoundException {

keyBytes = key.getBytes("UTF-8");
Log.d(tag,"Long KEY: "+keyBytes.length);
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(keyBytes);
keyBytes = md.digest();
Log.d(tag,"Long KEY: "+keyBytes.length);

byte[] ivB = Arrays.copyOfRange(bytes,0,16);
Log.d(tag, "IV: "+new String(ivB));
byte[] codB = Arrays.copyOfRange(bytes,16,bytes.length);


AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivB);
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
return cipher.doFinal(codB);

}

}

但是我无法像在 python 代码中那样解密;我不知道为什么,但每次我尝试;这些是我的异常(exception)情况:

javax.crypto.BadPaddingException: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher.doFinalInternal(OpenSSLCipher.java:430)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:466)
at javax.crypto.Cipher.doFinal(Cipher.java:1340)
at net.kaimanden.testcrypt.Crypt.doDecryption(Crypt.java:75)

最佳答案

您忘记在 Java 代码中导出 key 。在 python 代码中,您使用密码 (key) 通过 SHA-256 派生实际的 256 位 key 。

另一方面,在 Java 中,您直接使用 key = "HolaQueTal" ,它甚至太短而无法用作正确的 key ,甚至与您的 python 示例中的 key 不同。 MessageDigest类支持 SHA-256。

关于java - python 和 android 之间的 AES 加密互操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28171353/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com