Java中的AES-256和PKCS7Padding失败 [英] AES-256 and PKCS7Padding fails in Java

查看:859
本文介绍了Java中的AES-256和PKCS7Padding失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我现在需要使用标准Java应用程序做同样的事情,所以我已经从我的Android库中获取了代码,需要库,但是我遇到了异常。据我所知,代码并不是android特定的,所以它不应该是一个问题。



以下是我的加密功能

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

密码密码= 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());
}
返回密码;
}

当我调用 Encryption.encrypt(myString ); 我得到以下异常:

 加密异常:java.security.NoSuchAlgorithmException:找不到任何提供支持AES / CBC / PKCS7Padding 

正如我所说这个代码在Android上工作正常,它应该没有任何差异在哪里运行。



感谢您提供的任何帮助。



更新



我发现我需要PKCS5Padding而不是7感谢链接评论。我现在收到以下例外:

 加密异常:java.security.InvalidKeyException:非法密钥大小


解决方案

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



接下来,您尝试使用AES-256,需要安装无限力量管辖权政策文件。



希望这只是一个例子,你不是每个消息使用相同的IV,对吗?


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.

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.

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 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.

Thanks for any help you can provide.

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:

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

解决方案

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.

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

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

这篇关于Java中的AES-256和PKCS7Padding失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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