Note that there are some explanatory texts on larger screens.

plurals
  1. POEncrypt/Decrypt using Bouncy Castle in C#
    primarykey
    data
    text
    <p>I am using the "BouncyCastle.Crypto.dll" for encrypt/decrypt a string in my app. I am using the following <a href="https://web.archive.org/web/20140120123730/http://elian.co.uk/post/2009/07/29/Bouncy-Castle-CSharp.aspx" rel="noreferrer">code from this blog</a>:</p> <ol> <li><p>I have a class BCEngine, exactly the same as the one given in the link mentioned above.</p> <pre><code>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); } /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="forEncrypt"&gt;&lt;/param&gt; /// &lt;param name="input"&gt;&lt;/param&gt; /// &lt;param name="key"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; /// &lt;exception cref="CryptoException"&gt;&lt;/exception&gt; 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); } } </code></pre> <p>}</p></li> </ol> <p>I am using an asp.net form in which i have written code as given below:</p> <pre><code> 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); } } </code></pre> <p>Not sure, but due to some reason, I get an exception when I call the btnEncrypt_Click</p> <p>"Key length not 128/192/256 bits."</p> <p>Can anybody please guide? I am a complete newbie to this. Thanks in Advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload