加密/解密在C#中使用充气城堡 [英] Encrypt/Decrypt using Bouncy Castle in C#
问题描述
我使用的是BouncyCastle.Crypto.dll的加密/解密的字符串。我现在用的是
以下的从这个博客代码:
I am using the "BouncyCastle.Crypto.dll" for encrypt/decrypt a string in my app. I am using the following code from this blog:
-
我有一个类BCEngine,完全一样,在上述给出的链接之一。
I have a class BCEngine, exactly the same as the one given in the link mentioned above.
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);
}
}
}
}
我使用其中下面给出我写的代码一个asp.net的形式:
I am using an asp.net form in which i have written code as given below:
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
Not sure, but due to some reason, I get an exception when I call the btnEncrypt_Click
密钥长度不128/192/256位。
"Key length not 128/192/256 bits."
灿有人请指导?我是一个完整的新手到这一点。
在此先感谢
Can anybody please guide? I am a complete newbie to this. Thanks in Advance.
推荐答案
您字符串键=DFGFRT;
不是128/192/256位。
Your string key = "DFGFRT";
is not 128/192/256 bits.
DFGFRT
是6个字符,这是6(或12?)字节= 8 * 12 = 96位(最多)。
DFGFRT
is 6 characters, which is 6 (or 12?) bytes = 8*12 = 96 bits (at most).
要得到你需要一个16字节的字符串,一个128位的密钥,所以我会去在安全方面,并使用16字符串,所以这将是一个128位如果钥匙如果使用的宽字符使用单字节字符和256。
To get a 128 bit key you need a 16 byte string, so I'd go on the safe side and use a 16 character string so it will be a 128 bit key if using single byte characters and 256 if using wide characters.
这篇关于加密/解密在C#中使用充气城堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!