AES-256 和 PKCS7Padding 在 Java 中失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25942165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:59:52  来源:igfitidea点击:

AES-256 and PKCS7Padding fails in Java

javaencryptionaesjcepkcs#7

提问by Boardy

I have a couple of library, C#, PHP and Android where they all encrypt/decrypt a string in the same way so they are all compatible with each other, i.e. C# writes and encrypts data to a database and PHP can successfully decrypt it and return the original string.

我有几个库,C#、PHP 和 Android,它们都以相同的方式加密/解密字符串,因此它们彼此兼容,即 C# 将数据写入和加密到数据库,PHP 可以成功解密并返回原始字符串。

I now need to do the same thing with a standard Java application, so I've taken the code from my Android library and need libraries but I am getting an exception. As far as I know the code wasn't Android specific so it shouldn't be a problem.

我现在需要用标准 Java 应用程序做同样的事情,所以我从我的 Android 库中获取了代码并需要库,但我得到了一个例外。据我所知,该代码不是特定于 Android 的,因此应该不成问题。

Below is my encryption function

下面是我的加密功能

public static String encrypt(String plainPasword)
    {
            String password = "";
            try
            {
                SecretKeySpec key = new SecretKeySpec("hcxilkqbbhczfeultgbskdmaunivmfuo".getBytes("US-ASCII"), "AES");
                IvParameterSpec iv = new IvParameterSpec("ryojvlzmdalyglrj".getBytes("US-ASCII"));

                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

                cipher.init(Cipher.ENCRYPT_MODE, key, iv);

                byte[] encoded = cipher.doFinal(plainPasword.getBytes());
                password = new String(Base64.encodeBase64(encoded));

            }
            catch (Exception ex)
            {
                System.err.println("Encryption Exception: " + ex.toString());
            }
            return password;
    }

When I call Encryption.encrypt("myString")I get the following exception:

当我打电话时,Encryption.encrypt("myString")我收到以下异常:

Encryption Exception: java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding

As I said this code is working fine on Android and it shouldn't make any difference where it is running from.

正如我所说,这段代码在 Android 上运行良好,它从哪里运行应该没有任何区别。

Update

更新

I found that I needed PKCS5Padding instead of 7 thanks to a link on a comment. I am now though getting the following exception:

由于评论中的链接,我发现我需要 PKCS5Padding 而不是 7。我现在虽然收到以下异常:

Encryption Exception: java.security.InvalidKeyException: Illegal key size

回答by erickson

First, in Java, the standard padding name is PKCS5Padding, not PKCS7Padding. Java is actually performing PKCS #7 padding, but in the JCA specification, PKCS5Padding is the name given.

首先,在 Java 中,标准的填充名称是 PKCS5Padding,而不是 PKCS7Padding。Java 实际上执行 PKCS #7 填充,但在 JCA 规范中,PKCS5Padding 是给定的名称。

Next, you are trying to use AES-256, so you'll need to install the Unlimited Strength Jurisdictionpolicy files.

接下来,您尝试使用 AES-256,因此您需要安装Unlimited Strength Jurisdiction策略文件。

Hopefully this is just an example and you aren't using the same IV for every message, right?

希望这只是一个示例,您不会对每条消息都使用相同的 IV,对吗?