使用C#中的Bouncy Castle加密/解密

时间:2011-05-06 11:03:59

标签: c# bouncycastle

我正在使用“BouncyCastle.Crypto.dll”来加密/解密我的应用中的字符串。我正在使用 关注code from this blog

  1. 我有一个BCEngine类,与上面提到的链接完全相同。

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

    }

  2. 我使用的是asp.net表单,其中我编写了如下代码:

        public partial class EncryptionForm : System.Web.UI.Page
        {
          Encoding _encoding;
          IBlockCipherPadding _padding;
          string key = "DFGFRT";
           string textToBeEncrypted = "Original text. Please encrypt me.";
           string txtEncryptedText = string.empty;
           string txtDecryptedText = string.empty;
    
          protected void Page_Load(object sender, EventArgs e)
          {
              _encoding = Encoding.ASCII; 
              Pkcs7Padding pkcs = new Pkcs7Padding();
              _padding = pkcs;   
          }
    
          protected void btnEncrypt_Click(object sender, EventArgs e)
          {
              txtEncryptedText = AESEncryption(textToBeEncrypted, key, true);
          }
    
          protected void btnDecrypt_Click(object sender, EventArgs e)
          {
              txtDecryptedText = AESDecryption(txtEncryptedText.Text, key, true);
          }
    
          public string AESEncryption(string plain, string key, bool fips)
          {
              BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
              bcEngine.SetPadding(_padding);
              return bcEngine.Encrypt(plain, key);
          }
    
          public string AESDecryption(string cipher, string key, bool fips)
          {
              BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
              bcEngine.SetPadding(_padding);
              return bcEngine.Decrypt(cipher, key);
          }
        }
    

    不确定,但由于某种原因,我在调用btnEncrypt_Click时遇到异常

    “密钥长度不是128/192/256位。”

    有人可以指导吗?我是一个完整的新手。 在此先感谢。

2 个答案:

答案 0 :(得分:8)

您的string key = "DFGFRT";不是128/192/256位。

DFGFRT是6个字符,即6(或12?)字节= 8 * 12 = 96位(最多)。

要获得一个128位密钥,你需要一个16字节的字符串,所以我会安全地使用16个字符的字符串,因此如果使用单字节字符则为128位密钥,如果使用宽字符则为256

答案 1 :(得分:5)

相当简单,你的密钥是“DFGFRT”,它是6个字符/字节,即6 * 8 = 48位。

使用的加密方法需要长度为128/192/256位的密钥,相当于16/24/32个字符/字节。