Rijndael algorithm


Here i posted code to encrypt the plain text and decrypt the cipher text

your work is to give plain text as input to encrypter ,which gives you cipher text as output and viceversa.,




public static string Encrypt(string plainText,
string passPhrase,
string saltValue,
string hashAlgorithm,
int passwordIterations,
string initVector,
int keySize)
{

byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);


byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);


PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);


byte[] keyBytes = password.GetBytes(keySize / 8);


RijndaelManaged symmetricKey = new RijndaelManaged();


symmetricKey.Mode = CipherMode.CBC;


ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
keyBytes,
initVectorBytes);


MemoryStream memoryStream = new MemoryStream();

CryptoStream cryptoStream = new CryptoStream(memoryStream,
encryptor,
CryptoStreamMode.Write);

cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);


cryptoStream.FlushFinalBlock();


byte[] cipherTextBytes = memoryStream.ToArray();


memoryStream.Close();
cryptoStream.Close();


string cipherText = Convert.ToBase64String(cipherTextBytes);


return cipherText;
}




public static string Decrypt(string cipherText,
string passPhrase,
string saltValue,
string hashAlgorithm,
int passwordIterations,
string initVector,
int keySize)
{


byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);


byte[] cipherTextBytes = Convert.FromBase64String(cipherText);


PasswordDeriveBytes password = new PasswordDeriveBytes(
passPhrase,
saltValueBytes,
hashAlgorithm,
passwordIterations);


byte[] keyBytes = password.GetBytes(keySize / 8);


RijndaelManaged symmetricKey = new RijndaelManaged();


symmetricKey.Mode = CipherMode.CBC;


ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
keyBytes,
initVectorBytes);


MemoryStream memoryStream = new MemoryStream(cipherTextBytes);


CryptoStream cryptoStream = new CryptoStream(memoryStream,
decryptor,
CryptoStreamMode.Read);


byte[] plainTextBytes = new byte[cipherTextBytes.Length];

int decryptedByteCount = cryptoStream.Read(plainTextBytes,
0,
plainTextBytes.Length);


memoryStream.Close();
cryptoStream.Close();

string plainText = Encoding.UTF8.GetString(plainTextBytes,
0,
decryptedByteCount);


return plainText;
}
}


here passphrase and saltvalue can be any string,hashalgorithm may be SHA1 or MD5, iterations can be any number, initvector must be a 16 byte(ex:"ddf346hj85F6d7H8") and key size may be 128 or 256 or 192


Comments

Author: Manigandan04 Mar 2010 Member Level: Gold   Points : 1

Hi,

Nice one.

Whether RijndaelManaged(); function is created by you or we have add some DLL's for that


Thanks And Regards,
Mani

Author: karthick11 Mar 2010 Member Level: Silver   Points : 1

no external dll is needed.,,

just include this.,, thats enough nd sorry for late repli,,
now onli i saw ur msg.,
using System.Security.Cryptography;



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: