【问题标题】:Blowfish last block incomplete in decryption河豚最后一个块在解密中不完整
【发布时间】:2012-06-12 22:45:20
【问题描述】:

我有一个例外,最后一个块在解密中不完整,只有解密,加密它的工作完美。如何解决这个问题?

我使用充气城堡。

这是我找到的代码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);
  }

例外:

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: 

Decryption("test", "HjcWtqDOBMo=", true)

【问题讨论】:

  • 如果您要就遇到的异常寻求帮助,请发布您遇到的异常。
  • 程序崩溃并在 Visual Studio 2010 Express 中的解密中显示此消息最后一个块不完整;P
  • 你需要显示异常,here is a good example,否则没人知道出了什么问题。
  • 你有什么理由在 AES 上使用河豚?
  • 不,但是使用 AES 也是同样的问题

标签: c# bouncycastle encryption blowfish


【解决方案1】:
static Encoding _encoding = Encoding.ASCII;

return Convert.ToBase64String(result);

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

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

【讨论】:

  • 好的,我将 Encoding.ASCII 更改为 Encoding.Unicode。我应该在哪里添加/更改 return Convert.ToBase64String(result); ?
  • 尝试在任何地方使用 _encoding.GetBytes 和 _encoding.GetString。
  • Exeption 它只是解密功能,当我将 Convert.FromBase64String(cipher) 更改为 _encoding.GetBytes(cipher) exeption 消息板块损坏
猜你喜欢
  • 1970-01-01
  • 2022-06-11
  • 2016-02-29
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
相关资源
最近更新 更多