河豚最后一个块在解密中不完整

本文关键字:解密 最后一个 河豚 | 更新日期: 2023-09-27 18:36:48

我有一个执行最后一个块在解密中不完整,仅在解密中,加密其工作完美。如何解决这个问题?

我使用充气城堡。

这是我在这里找到的代码

 namespace BlowFishCS
  {
     public class BCEngine
    {
    private readonly Encoding _encoding;
    private readonly IBlockCipher _blockCipher;
    private PaddedBufferedBlockCipher _cipher;
    private IBlockCipherPadding _padding;
    public BCEngine(IBlockCipher blockCipher, Encoding encoding)
    {
        _blockCipher = blockCipher;
        _encoding = encoding;
    }
    public BCEngine()
    {
        // TODO: Complete member initialization
    }
    public void SetPadding(IBlockCipherPadding padding)
    {
        if (padding != null)
            _padding = padding;
    }
    public string Encrypt(string plain, string key)
    {
        byte[] result = BouncyCastleCrypto(true, _encoding.GetBytes(plain), key);
        return Convert.ToBase64String(result);
    }
    public string Decrypt(string cipher, string key)
    {
        //cipher = cipher.Replace(' ', '+');
        byte[] result = BouncyCastleCrypto(false, Convert.FromBase64String(cipher), key);
        return _encoding.GetString(result);
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="forEncrypt"></param>
    /// <param name="input"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    /// <exception cref="CryptoException"></exception>

    private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, string key)
    {
        try
        {
            _cipher = _padding == null ?
        new PaddedBufferedBlockCipher(_blockCipher) :
        new PaddedBufferedBlockCipher(_blockCipher, _padding);
            // this line will make sure keyByte is 16 bytes long
            byte[] keyByte = _encoding.GetBytes(key);
            _cipher.Init(forEncrypt, new KeyParameter(keyByte));
            return _cipher.DoFinal(input);
        }
        catch (Org.BouncyCastle.Crypto.CryptoException ex)
        {
            throw new CryptoException(ex.Message); <here is exception last block incomplete in decryption
        }
    }
}
}

我使用此编码和函数。

    static Encoding _encoding = Encoding.ASCII; 
    static Pkcs7Padding pkcs = new Pkcs7Padding();
    static IBlockCipherPadding _padding = pkcs;
public static string Encryption(string plain, string key, bool fips)
  {
      BCEngine bcEngine = new BCEngine(new BlowfishEngine(), _encoding);
      bcEngine.SetPadding(_padding);
      return bcEngine.Encrypt(plain, key);
  }
    public static string Decryption(string cipher, string key, bool fips)
  {
      BCEngine bcEngine = new BCEngine(new BlowfishEngine(), _encoding);
      bcEngine.SetPadding(_padding);
      return bcEngine.Decrypt(cipher, key);
  }

说明:

Org.BouncyCastle.Crypto.CryptoException was unhandled
    Message=last block incomplete in decryption
   Source=ID_Serwer
   StackTrace:
        at BlowFishCS.BCEngine.BouncyCastleCrypto(Boolean forEncrypt, Byte[] input, String  key) in C:'Documents and Settings'WirtualOS'Pulpit'serever'ID_Serwer'Blowfish.cs:line 96
       at BlowFishCS.BCEngine.Decrypt(String cipher, String key) in C:'Documents and Settings'WirtualOS'Pulpit'serever'ID_Serwer'Blowfish.cs:line 60
       at ID_Serwer.ID_Serv_Main.Decryption(String cipher, String key, Boolean fips) in C:'Documents and Settings'WirtualOS'Pulpit'serever'ID_Serwer'ID_Serv_Main.cs:line 154
       at ID_Serwer.ID_Serv_Main.process_cmd(String cmd) in C:'Documents and Settings'WirtualOS'Pulpit'serever'ID_Serwer'ID_Serv_Main.cs:line 163
       at ID_Serwer.ID_Serv_Main.Main(String[] args) in C:'Documents and Settings'WirtualOS'Pulpit'serever'ID_Serwer'ID_Serv_Main.cs:line 137
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Exeption在那之后显示解密("test", "HjcWtqDOBMo=", true)

河豚最后一个块在解密中不完整

static Encoding _encoding = Encoding.ASCII;
return Convert.ToBase64String(result);

应在加密和解密中使用相同的编码。

更新:使用 System.Text.Encoding.Unicode 不支持 ASCII 字符。