Blowfish最后一块在解密时不完整

时间:2012-06-08 21:25:37

标签: c# bouncycastle encryption blowfish

我在解密时只有一个exeption最后一个块在解密中不完整,加密它的工作完美。如何修复这个?

我使用充气城堡。

这是我找到的代码here

 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);
  }

Exeption:

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: 

在解密后显示执行(“test”,“HjcWtqDOBMo =”,true)

1 个答案:

答案 0 :(得分:0)

static Encoding _encoding = Encoding.ASCII;

return Convert.ToBase64String(result);

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

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