gpt4 book ai didi

java - BouncyCaSTLe GCM/CCM ArrayIndexOutOfBoundsException 异常

转载 作者:搜寻专家 更新时间:2023-11-01 02:13:32 24 4
gpt4 key购买 nike

谁能给我一个在 BouncyCaSTLe 中使用 AES 的 GCM 和/或 CCM 模式的例子?
我的代码是这样的:

SecretKeySpec   key = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
byte[] block = new byte[1048576];
int i;
long st,et;

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

BufferedInputStream bIn=new BufferedInputStream(new ProgressMonitorInputStream(null,"Encrypting ...",new FileInputStream("input")));
CipherInputStream cIn = new CipherInputStream(bIn, cipher);
BufferedOutputStream bOut=new BufferedOutputStream(new FileOutputStream("output.enc"));

int ch;
while ((i = cIn.read(block)) != -1) {
bOut.write(block, 0, i);
}
cIn.close();
bOut.close();

Thread.sleep(5000);

cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);

BufferedInputStream fis=new BufferedInputStream(new ProgressMonitorInputStream(null,"Decrypting ...",new FileInputStream("output.enc")));
//FileInputStream fis=new FileInputStream("output.enc");
//FileOutputStream ro=new FileOutputStream("regen.plain");
BufferedOutputStream ro=new BufferedOutputStream(new FileOutputStream("regen.plain"));

CipherInputStream dcIn = new CipherInputStream(fis, cipher);

while ((i = dcIn.read(block)) != -1) {
ro.write(block, 0, i);
}

dcIn.close();
ro.close();

但是在GCM模式下解密时会抛出这个异常(第70行是bOut.write(block, 0, i);):

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at org.bouncycastle.crypto.modes.CCMBlockCipher.processPacket(Unknown Source)
at org.bouncycastle.crypto.modes.CCMBlockCipher.doFinal(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$AEADGenericBlockCipher.doFinal(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at javax.crypto.CipherInputStream.a(DashoA13*..)
at javax.crypto.CipherInputStream.read(DashoA13*..)
at javax.crypto.CipherInputStream.read(DashoA13*..)
at enctest.Main.main(Main.java:70)

并且在 CCM 模式下加密时出现此异常(第 70 行是 bOut.write(block, 0, i);):

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at org.bouncycastle.crypto.modes.CCMBlockCipher.processPacket(Unknown Source)
at org.bouncycastle.crypto.modes.CCMBlockCipher.doFinal(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$AEADGenericBlockCipher.doFinal(Unknown Source)
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at javax.crypto.CipherInputStream.a(DashoA13*..)
at javax.crypto.CipherInputStream.read(DashoA13*..)
at javax.crypto.CipherInputStream.read(DashoA13*..)
at enctest.Main.main(Main.java:70)

最佳答案

对于 CCM 模式,有一个小障碍:IV 的大小应该小于 block 大小。您的代码在以下情况下崩溃:

BlockCipher ctrCipher = new SICBlockCipher(cipher);
byte[] iv = new byte[blockSize];
byte[] out;

iv[0] = (byte)(((15 - nonce.length) - 1) & 0x7);

System.arraycopy(nonce, 0, iv, 1, nonce.length);

尝试用 15 个字节的“IV”代替(IV 实际上是一个 NONCE,但 IvParameterSpec 用于 NONCE)。

另一个问题是 cipher.doFinal() 方法在 CipherInputStream 无法从底层流中检索任何数据时以及在 close() 被调用。请注意,CipherInputStream 是一个编写得非常糟糕的类,它在抛出时也会删除 BadPaddingException - 这是标签验证失败时出现的异常 (!!!)。您最好基于 CipherInputStream 创建您自己的一个。我已将代码更改为抛出基于特定 IOException 的异常而不是忽略异常,并保持 boolean 状态以查看是否 doFinal()已在基础密码上执行。它不应调用 doFinal() 两次。

所以您在这里运行时遇到了 Java JCE 错误。我可能会把它放在 Oracle 错误数据库中,直到现在我所有的错误报告都被完全忽略了。

针对最新版本的 OpenJDK 7 和 Bouncy CaSTLe 1.47(2012-08-30 或接近的版本)进行了测试。

关于java - BouncyCaSTLe GCM/CCM ArrayIndexOutOfBoundsException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12224686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com