解密错误:[否四集,当一个预期" [英] Decrypting error : "no iv set when one expected"

查看:1072
本文介绍了解密错误:[否四集,当一个预期"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎新的加密。

我试图解密的字节数组,而当我提供我正在一个异常IV: InvalidAlgorithmParameterException使用(当一个预期无四集)。

I am trying to decrypt an array of bytes, and when I am providing the IV I am getting an exception : InvalidAlgorithmParameterException (no iv set when one expected).

下面是我的code(的是16个字节数组,它不为空,并具有加密时所使用的值):

Here's my code (iv is an array of 16 bytes which is not null and has the values used when encrypting) :

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, encriptionKey,new IvParameterSpec(iv));

如果我不指定IV密码被初始化确定:

If I don't specify the IV the cipher gets initialized ok :

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, encriptionKey);

试图找到一个答案我找到JCEStreamCipher的实现(<一href="http://java2s.com/Open-Source/Java/Security/Bouncy-Castle/org/bouncycastle/jce/provider/JCEStreamCipher.java.htm">here)这可能与我使用的版本,但有一些code,这让我的事情,我无法正确理解它。

Trying to find an answer I did find an implementation of JCEStreamCipher (here) which may not correspond to the version I am using but has some code that makes me thing I am not understanding it correctly.

这里的code:

   if ((ivLength != 0) && !(param instanceof ParametersWithIV))
    {
        SecureRandom    ivRandom = random;

        if (ivRandom == null)
        {
            ivRandom = new SecureRandom();
        }

        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE))
        {
            byte[]  iv = new byte[ivLength];

            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV)param;
        }
        else
        {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }

貌似我不能提供解密时的IV,但它并没有使太大的意义了我。

Looks like I cannot provide an IV when decrypting, but it doesn't makes too much sense to me.

任何帮助将大大AP preciated。

any help will be greatly appreciated.

非常感谢, 理查德。

推荐答案

解决了。

我使用的是错误的SecretKey的,而不是一个你可以创建AES。

I was using a wrong SecretKey, not the one you can create for AES.

previously我有:

Previously I had :

KeySpec spec = new PBEKeySpec(password.toCharArray(), encryptionKeySalt, 12345,256);
SecretKey encriptionKey = factory.generateSecret(spec);

这将创建一个JCEPBEKey。

which creates a JCEPBEKey.

我失踪了:

Key encriptionKey = new SecretKeySpec(encriptionKey.getEncoded(), "AES"); 

这将创建一个适当的密钥的AES。

which creates an appropiate key for AES.

这篇关于解密错误:[否四集,当一个预期&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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