C#, AES encryption check!

Posted by Data-Base on Stack Overflow See other posts from Stack Overflow or by Data-Base
Published on 2010-12-30T08:52:54Z Indexed on 2010/12/30 8:53 UTC
Read the original article Hit count: 408

Filed under:
|
|

I have this code for AES encryption, can some one verify that this code is good and not wrong? it works fine, but I'm more concern about the implementation of the algorithm

        // Plaintext value to be encrypted.

        //Passphrase from which a pseudo-random password will be derived.
        //The derived password will be used to generate the encryption key.

        //Password can be any string. In this example we assume that this passphrase is an ASCII string.

        //Salt value used along with passphrase to generate password.
        //Salt can be any string. In this example we assume that salt is an ASCII string.

        //HashAlgorithm used to generate password. Allowed values are: "MD5" and "SHA1".
        //SHA1 hashes are a bit slower, but more secure than MD5 hashes.

        //PasswordIterations used to generate password. One or two iterations should be enough.

        //InitialVector (or IV). This value is required to encrypt the first block of plaintext data.
        //For RijndaelManaged class IV must be exactly 16 ASCII characters long.

        //KeySize. Allowed values are: 128, 192, and 256.
        //Longer keys are more secure than shorter keys.

        //Encrypted value formatted as a base64-encoded string.


        public static string Encrypt(string PlainText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
        {
            byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
            byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
            byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);
            PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
            byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
            RijndaelManaged SymmetricKey = new RijndaelManaged();
            SymmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes);
            MemoryStream MemStream = new MemoryStream();
            CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write);
            CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
            CryptoStream.FlushFinalBlock();
            byte[] CipherTextBytes = MemStream.ToArray();
            MemStream.Close();
            CryptoStream.Close();
            return Convert.ToBase64String(CipherTextBytes);
        }

        public static string Decrypt(string CipherText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
        {
            byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
            byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
            byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
            PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
            byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
            RijndaelManaged SymmetricKey = new RijndaelManaged();
            SymmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes);
            MemoryStream MemStream = new MemoryStream(CipherTextBytes);
            CryptoStream cryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);
            byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
            int ByteCount = cryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
            MemStream.Close();
            cryptoStream.Close();
            return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);
        }

Thank you

© Stack Overflow or respective owner

Related posts about c#

Related posts about aes