Bouncy Castle C#

by Jesse 29. July 2009 11:54

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)

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

 

The encryption/decryption interface:

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

 

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

Paypal bugged their logon

by Jesse 24. June 2009 11:03

To all 150 million Paypal users (The number is what they say on their homepage):

Can you believe a company can make your money better than their website? Tried to logon this morning during 9:50 - 10:20, keep getting error message showing below:

 

 

Welcome any comments on problem using Paypal!!! Maybe we should let them know what WE, the customers, think.

Tags: , , ,
Categories: IT | Security | News

TTPlayer dll

by Jesse 19. June 2009 15:08

上传一个TTPlayer的DLL, 想知道它是干什么的吗? 请用正确的email留言, 我会Email给你所有的细节. 现在所能说的就是, 它与TTPlayer的安全漏洞有关, 相关讨论可以在

http://bbs.chinaunix.net/viewthread.php?tid=1340433&extra=&page=2
http://bbs.chinaunix.net/viewthread.php?tid=1341275

Jesse

 

lrcsh.rar (19.00 kb)

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant