Quantcast
Channel: Jesse's Blog - C#
Viewing all articles
Browse latest Browse all 5

Bouncy Castle C#

$
0
0

It has been really long time since my last post. And finally I get a few minutes to write this post on using BouncyCacle encryption in C#, hopefully it can help some one.

BouncyCastle is a encryption library widely used in Java, and has been porting to C# ages ago. But due to lack of documentation, I believe many people including myself have much trouble on implement any encrypt/decrypt functions by using it. Right, let's start.

 

First of all, you need to get BouncyCastle library from its website on http://www.bouncycastle.org/csharp/. It is not necessary to get the source code. Also, there are two type of binary format libraries you can download. One is with IDEA, and another is not. If you dont know which one you need, just download the one without IDEA. As IDEA is patented in many country, so you must be more careful when choosing to use.

My implementation consist of two  parts: block cipher encryption engine, and encryption/decryption interface.

BCEngine class (Block cipher engine)

publicclass BCEngine
    {
        privatereadonly Encoding _encoding;
        privatereadonly IBlockCipher _blockCipher;
        private PaddedBufferedBlockCipher _cipher;
        private IBlockCipherPadding _padding;

        public BCEngine(IBlockCipher blockCipher, Encoding encoding)
        {
            _blockCipher = blockCipher;
            _encoding = encoding;
        }

        publicvoid SetPadding(IBlockCipherPadding padding)
        {
            if(padding != null)
                _padding = padding;
        }

        publicstring Encrypt(string plain, string key)
        {
            byte[] result = BouncyCastleCrypto(true, _encoding.GetBytes(plain), key);
            return Convert.ToBase64String(result);
        }

        publicstring 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>


        privatebyte[] 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)
            {
                thrownew CryptoException(ex);
            }
        }
    }

 

The encryption/decryption interface:

publicstring AESEncryption(string plain, string key, bool fips)
        {
            BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
            bcEngine.SetPadding(_padding);
            return bcEngine.Encrypt(plain, key);
        }

        publicstring AESDecryption(string cipher, string key, bool fips)
        {
            BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
            bcEngine.SetPadding(_padding);
            return bcEngine.Decrypt(cipher, key);
        }

 

You can easily change the BouncyCastle engine to Blowfish, DES, TripleDES, TwoFish, etc.


Viewing all articles
Browse latest Browse all 5

Trending Articles