org.bouncycastle.crypto.datalength Exception:最后一个块解密不完整
我已经使用弹力城堡库实施了AES 256欧洲央行加密和解密。我能够加密数据,但无法解密密码文本。它引发了一个错误,说“ org.bouncycastle.crypto.datalength Exception:最后一个块不完整”。以下是我的加密和解密代码;
public byte[] encrypt(byte[] skey, byte[] data)
{
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new AesEngine(), new Pkcs7Padding());
cipher.Init(true, new KeyParameter(skey));
int outputSize = cipher.GetOutputSize(data.Length);
byte[] tempOP = new byte[outputSize];
int processLen = cipher.ProcessBytes(data, 0, data.Length, tempOP, 0);
int outputLen = cipher.DoFinal(tempOP, processLen);
byte[] result = new byte[processLen + outputLen];
Array.Copy(tempOP, 0, result, 0, result.Length);
return result;
}
public byte[] decrypt(byte[] skey, byte[] data)
{
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new AesEngine(), new Pkcs7Padding());
cipher.Init(false, new KeyParameter(skey));
int outputSize = cipher.GetOutputSize(data.Length);
byte[] tempOP = new byte[outputSize];
int processLen = cipher.ProcessBytes(data, 0, data.Length, tempOP, 0);
int outputLen = cipher.DoFinal(tempOP, processLen);
byte[] result = new byte[processLen + outputLen];
Array.Copy(tempOP, 0, result, 0, result.Length);
return result;
}
I have implemented AES 256 ECB Encryption and Decryption using Bouncy Castle library. I am able to encrypt the data , but unable to decrypt the cipher text. It throws an error saying "Org.BouncyCastle.Crypto.DataLengthException: last block incomplete in decryption". Following is my code for Encryption and Decryption;
public byte[] encrypt(byte[] skey, byte[] data)
{
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new AesEngine(), new Pkcs7Padding());
cipher.Init(true, new KeyParameter(skey));
int outputSize = cipher.GetOutputSize(data.Length);
byte[] tempOP = new byte[outputSize];
int processLen = cipher.ProcessBytes(data, 0, data.Length, tempOP, 0);
int outputLen = cipher.DoFinal(tempOP, processLen);
byte[] result = new byte[processLen + outputLen];
Array.Copy(tempOP, 0, result, 0, result.Length);
return result;
}
public byte[] decrypt(byte[] skey, byte[] data)
{
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
new AesEngine(), new Pkcs7Padding());
cipher.Init(false, new KeyParameter(skey));
int outputSize = cipher.GetOutputSize(data.Length);
byte[] tempOP = new byte[outputSize];
int processLen = cipher.ProcessBytes(data, 0, data.Length, tempOP, 0);
int outputLen = cipher.DoFinal(tempOP, processLen);
byte[] result = new byte[processLen + outputLen];
Array.Copy(tempOP, 0, result, 0, result.Length);
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也有同样的问题。
类型:org.bouncycastle.crypto.datalength Exception
stacktrace:at org.bouncycastle.crypto.paddings.paddedbufferedblockcipher.dofinal(byte [Byte [] output,int32 Outoff)in/_/_/crypto/src/src/crypto/crypto/ppto/paddings/paddedbufferedblockcipher.csline offinal.padddedbufferdblockcipher.csline 281- fofinal 281-
fofinal法
I have same issue.
Type : Org.BouncyCastle.Crypto.DataLengthException
StackTrace : at Org.BouncyCastle.Crypto.Paddings.PaddedBufferedBlockCipher.DoFinal(Byte[] output, Int32 outOff) in /_/crypto/src/crypto/paddings/PaddedBufferedBlockCipher.cs:line 281
-- FoFinal method