Fau*_*rel 5 python java encryption cryptography aes
我有以下要用Python复制的Java代码。
public class AESDecryption {
protected SecretKeySpec getPublicKey() {
try {
byte[] key = "MuidKeibimbtjph9".getBytes("UTF-8");
key = MessageDigest.getInstance("SHA-256").digest(key);
key = Arrays.copyOf(key, 32);
return new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public String decrypt(byte[] data) {
Cipher cipher = null;
try {
cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(2, new SecretKeySpec(getPublicKey().getEncoded(), "AES"), new IvParameterSpec(new byte[cipher.getBlockSize()]));
byte decryptedBytes[] = cipher.doFinal(data);
return new String(Arrays.copyOf(decryptedBytes, decryptedBytes.length - decryptedBytes[-1 + decryptedBytes.length]));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return "";
}
public static void main(String[] args) {
try {
byte[] content = Files.readAllBytes(Paths.get("/tmp/dump.gzip"));
AESDecryption aesDecryption = new AESDecryption();
System.out.println(aesDecryption.decrypt(content));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码来自客户端应用程序。生成加密内容的服务器端没有电源。对于这个问题,我更改了对称密钥以及如何检索内容(在此示例中是从文件中获取的,但实际上是从https响应中获取的)
我想使用PyCrypto库在python脚本中复制此功能。这是我的初始代码的样子:
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto import Random
BLOCK_SIZE = 16
unpad = lambda s: s[0:-ord(s[-1])]
hash = SHA256.new()
hash.update('MuidKeibimbtjph9')
symmetric_key = hash.digest()
symmetric_key = symmetric_key[:32]
bytes_store = None
with open('/tmp/dump.gzip','r') as f:
bytes_store = f.read()
rndfile = Random.new()
aes_decryptor = AES.new(symmetric_key, AES.MODE_CBC, rndfile.read(BLOCK_SIZE))
print unpad(aes_decryptor.decrypt(bytes_store))
Run Code Online (Sandbox Code Playgroud)
在加密文件上运行Java代码就可以了。结果看起来像这样:
{"code":200,"status":"ok","api_version":"0.0.0","data":[.....],"notifications":{}}
Run Code Online (Sandbox Code Playgroud)
但是,python复制会转储“半解密”文本。好吧..
=c?q[A?$?dl?tus":"ok","api_version":"0.0.0","data":[.....],"notifications":{}}
Run Code Online (Sandbox Code Playgroud)
我什么都做不了。查看Java代码,很明显,密码块中没有填充,因此我认为服务器端的数据可能已经是密码块大小的倍数。在python输出的末尾也有很多▯▯▯字符,但是我通过取消填充解密数据来迅速摆脱了它们。不过,我无法弄清楚有效内容的第一部分被打乱了,这是我做错了。我对数据加密的知识非常基础,因此我可以联系您:)
问题在于服务器代码使用固定的IV(不好),其中包含零,但是在python代码中,您将新随机生成的IV传递给AES.new。
您可以替换rndfile.read(BLOCK_SIZE)为"\x00"*BLOCK_SIZE。