如何在Python中进行加密并在Java中进行解密? [英] How do I encrypt in Python and decrypt in Java?

查看:205
本文介绍了如何在Python中进行加密并在Java中进行解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加密Python程序中的一些数据并将其保存下来,然后在Java程序中解密该数据。在Python中,我正在加密:

 从Crypto.Cipher import AES 
KEY ='12345678901234567890123456789012'

def加密(数据):
cipher = AES.new(KEY,AES.MODE_CFB)
return cipher.encrypt(data)

在Java中,我正在解密它:

  import java.security。*; 
import javax.crypto。*;
import javax.crypto.spec.SecretKeySpec;

public class加密{
private static byte [] KEY = {'1','2','3','4','5','6' ','8','9','0','1','2','3','4','5','6','7',
' '9','0','1','2','3','4','5','6','7','8' ','2'};

public static byte [] decrypt(byte [] data)throws NoSuchAlgorithmException,NoSuchPaddingException,
InvalidKeyException,IllegalBlockSizeException,BadPaddingException {
Cipher c = Cipher.getInstance(AES / CFB / NoPadding);
Key key = new SecretKeySpec(KEY,AES);
c.init(Cipher.DECRYPT_MODE,key);
return c.doFinal(data);
}
}

但是我得到异常线程mainjava.security.InvalidKeyException:非法键大小或默认参数。显然,我做错了事情。但是什么?

解决方案

您有问题的原因是因为安全策略将您的密钥大小限制为128位,尝试使用256位密钥(需要Java加密扩展(JCE)无限优先权限策略文件)。



查看这个的讨论,你可能会注意到你有类似的问题。我的机器实际上有同样的问题。更新安全策略后,我能够运行代码。另外,我想你应该进行以下更改 c.init(Cipher.DECRYPT_MODE,key,new IvParameterSpec(new byte [16])); 你缺少初始化向量对于CFB模式。如果解密的值不正确,请检查初始化密钥的方式。


I'm trying to encrypt some data in a Python program and save it out, then decrypt that data in a Java program. In Python, I'm encrypting it like this:

from Crypto.Cipher import AES
KEY = '12345678901234567890123456789012'

def encrypt(data):
    cipher = AES.new(KEY, AES.MODE_CFB)
    return cipher.encrypt(data)

And in Java, I'm decrypting it like this:

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

public class Encryption {
    private static byte[] KEY = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2' };

    public static byte[] decrypt(byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher c = Cipher.getInstance("AES/CFB/NoPadding");
        Key key = new SecretKeySpec(KEY, "AES");
        c.init(Cipher.DECRYPT_MODE, key);
        return c.doFinal(data);
    }
}

But I get Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters. Clearly, I'm doing something wrong. But what?

解决方案

The reason you have a problem is because the security policy limits your key size to 128-bit and you are attempting to use a 256-bit key (need Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files).

Look at this discussion and you'll probably notice you have a similar problem. I actually had the same problem on my machine. After updating the security policy, I was able to run your code. Also, I think you should make the following change c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(new byte[16])); You are missing the initialization vector for the CFB mode. If the decrypted value is not correct, check the way you initialize the keys.

这篇关于如何在Python中进行加密并在Java中进行解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆