简体   繁体   English

Java AES加密,Python解密不起作用

[英]Java AES Encryption, Python Decryption not working

I have tried multiple ways to do this, for three days now and many hours. 我已经尝试了多种方法来执行此操作,现在已经三天了,而且时间很多。 I have gotten NO WHERE. 我什么都没有。 I am using Java to encrypt certain data using AES/CBC/PKCS7Padding, and trying to decrypt using the same in Python but it just won't work. 我正在使用Java使用AES / CBC / PKCS7Padding加密某些数据,并尝试在Python中使用相同的数据解密,但它无法正常工作。 I am using this Python aes.py library http://anh.cs.luc.edu/331/code/aes.py 我正在使用此Python aes.py库http://anh.cs.luc.edu/331/code/aes.py
I am getting this error: 我收到此错误:

  File "/root/ascend/aes.py", line 384, in decrypt
  block[(i+(j*4))] = iput[(i*4)+j]
  exceptions.IndexError: list index out of range

Here is the Java Aes code: 这是Java Aes代码:

    public String aesEncrypt(String key, String data) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
          SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES");
          KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
          KeyGen.init(256);
          Cipher AesCipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
          AesCipher.init(Cipher.ENCRYPT_MODE, secKey, new IvParameterSpec(IV.getBytes("UTF-8")));
          byte[] byteCipherText = AesCipher.doFinal(data.getBytes());
          return Base64.encodeToString(byteCipherText, 0).trim();
    }

Here is the Java key gen which provides the key for python to use as well: 这是Java密钥生成,它还提供了供python使用的密钥:

        public String genAESKey() {
          String uuid = UUID.randomUUID().toString();
          return uuid.replace("-","");
        }

And here is the python code to decrypt: 这是解密的python代码:

self.data = aes.decryptData(user.aes_key, base64.b64decode(self.data))
#Where user.aes_key is the 256bit Aes key generated by java.

Can someone please take a look and explain what is wrong with this? 有人可以看看并解释这是怎么回事吗? They are both using the same aes 256 key, pkcs7 padding, and CBC. 它们都使用相同的AES 256键,pkcs7填充和CBC。 If anyone knows of a better library that works with such java code please do show. 如果有人知道可以使用此类Java代码的更好的库,请显示。

Edit: Just to clarify things, Aes decryption works in Java just not in python and Python encryption using that aes key works and so does python decryption. 编辑:只是为了说明问题,Aes解密仅在Java中有效,而不是在python中,使用该aes密钥的Python加密有效,而python解密也是如此。 Just not java -> python. 只是不是java-> python。 And self.data is the java encrypted aes data. 而self.data是java加密的aes数据。

Edit #2: Just tried to do this with PyCrypto as well. 编辑2:刚尝试使用PyCrypto做到这一点。 Same exact error is occuring. 发生完全相同的错误。

    return self._cipher.decrypt(ciphertext)
    exceptions.ValueError: Input strings must be a multiple of 16 in length

The problem was that for some reason Java was padding the first byte of the resulting String. 问题在于,由于某种原因,Java将填充结果字符串的第一个字节。 Why? 为什么? I don't have the slightest clue but after stripping it off in Python all was well. 我没有丝毫线索,但是在Python中将其剥离后,一切都很好。 Both the PyCrypto code and the Aes.py code work fine. PyCrypto代码和Aes.py代码都可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM