简体   繁体   English

AES-256和PKCS7Padding在Java中失败

[英]AES-256 and PKCS7Padding fails in Java

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, ie 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. 我现在需要用标准的Java应用程序做同样的事情,所以我从我的Android库中获取代码并需要库,但我得到了一个例外。 As far as I know the code wasn't android specific so it shouldn't be a problem. 据我所知,代码不是特定于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"); 当我调用Encryption.encrypt("myString"); I get the following exception: 我得到以下异常:

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上运行良好,它在运行的地方不应该有任何区别。

Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

UPDATE UPDATE

I found that I needed PKCS5Padding instead of 7 thanks to a link on a comment. 我发现由于评论链接,我需要PKCS5Padding而不是7。 I am now though getting the following exception: 我现在虽然得到以下异常:

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

First, in Java, the standard padding name is PKCS5Padding, not PKCS7Padding. 首先,在Java中,标准填充名称是PKCS5Padding,而不是PKCS7Padding。 Java is actually performing PKCS #7 padding, but in the JCA specification, PKCS5Padding is the name given. Java实际上正在执行PKCS#7填充,但在JCA规范中,PKCS5Padding是给定的名称。

Next, you are trying to use AES-256, so you'll need to install the Unlimited Strength Jurisdiction policy 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,对吧?

@Boardy If you are still facing the issue then i think you have to use MessageDigest which is compatible for both for C# and Java. @Boardy如果您仍然面临这个问题,那么我认为您必须使用兼容C#和Java的MessageDigest。 I have faced the similar issue for AES 256 encryption and decryption. 我遇到过AES 256加密和解密的类似问题。 The sample code will be as follows. 示例代码如下。

public static String encryptWithAES256(String strToEncrypt) throws Exception
{
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] encodedhash = digest.digest(KEY.getBytes(StandardCharsets.UTF_8));
    IvParameterSpec ivspec = new IvParameterSpec(Arrays.copyOf(KEY.getBytes(),16));
    SecretKeySpec secretKey = new SecretKeySpec(encodedhash, AES_ENCRYPTION_ALGORITHM);
    Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
    return new String(Base64.encodeBase64(cipher.doFinal(strToEncrypt.getBytes(CHARACTER_ENCODING))));
}

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

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