org.bouncycastle.crypto.datalength Exception:最后一个块解密不完整

发布于 2025-02-06 09:01:45 字数 1289 浏览 3 评论 0原文

我已经使用弹力城堡库实施了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2025-02-13 09:01:45

我也有同样的问题。

类型: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法

        if (bufOff == blockSize)
        {
            num = cipher.ProcessBlock(buf, 0, buf, 0);
            bufOff = 0;
            try
            {
                num -= padding.PadCount(buf);
                Array.Copy(buf, 0, output, outOff, num);
                return num;
            }
            finally
            {
                Reset();
            }
        }

        Reset();
        **throw new DataLengthException("last block incomplete in decryption");**
                               

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

        if (bufOff == blockSize)
        {
            num = cipher.ProcessBlock(buf, 0, buf, 0);
            bufOff = 0;
            try
            {
                num -= padding.PadCount(buf);
                Array.Copy(buf, 0, output, outOff, num);
                return num;
            }
            finally
            {
                Reset();
            }
        }

        Reset();
        **throw new DataLengthException("last block incomplete in decryption");**
                               
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文