Encryption and Decryption using TrippleDES or Rijndael algorithm

Posted by Basawareddy under C# on 12/3/2009 | Views : 4079 | Status : [Member] | Replies : 1
Dear Friends,

I wanted to encrypt password in 128 bit format using TrippleDES or Rijndael methods. If any body is having same please post here.

I have done using Rijndael algorithm but which will encrypt in 64 bits format. But i wanted it in 128 bits. Please help me in this regard.

internal string Encryption(string content)
{
byte[] key;
byte[] iv;
byte[] toEncrypt = Encoding.UTF8.GetBytes(content);
byte[] encrypted;

try
{
using (RijndaelManaged RijndaelAlg = new RijndaelManaged())
{
RijndaelAlg.GenerateKey();
RijndaelAlg.GenerateIV();
key = RijndaelAlg.Key;
iv = RijndaelAlg.IV;

RijndaelAlg.Key = key;
RijndaelAlg.IV = iv;
using (ICryptoTransform encryptor = RijndaelAlg.CreateEncryptor(key, iv))
{
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
encrypted = msEncrypt.ToArray();

byte[] contentWithKey = new byte[encrypted.Length + key.Length + iv.Length];

key.CopyTo(contentWithKey, 0);
encrypted.CopyTo(contentWithKey, key.Length);
iv.CopyTo(contentWithKey, key.Length + encrypted.Length);

string strContent = Convert.ToBase64String(contentWithKey);

return strContent;
}
}
}
}
}
catch
{
throw;
}
}

internal byte[] ExtractBytes(byte[] source, int startIndex, int length)
{
byte[] extracted = new byte[length];

for (int i = startIndex; i < startIndex + length; i++)
{
extracted[i - startIndex] = source[i];
}

return extracted;
}

internal string Decrypt(string content)
{
if (content == "")
{
return string.Empty;
}

byte[] key = new byte[32];
byte[] iv = new byte[16];
byte[] byteContent = Convert.FromBase64String(content);
byte[] finalContent = new byte[byteContent.Length - (key.Length + iv.Length)];

key = ExtractBytes(byteContent, 0, key.Length);
iv = ExtractBytes(byteContent, byteContent.Length - iv.Length, iv.Length);
finalContent = ExtractBytes(byteContent, key.Length, byteContent.Length - iv.Length - key.Length);

// Create or open the specified file.
using (MemoryStream msDecryp = new MemoryStream(finalContent))
{
// Create a new Rijndael object.
using (RijndaelManaged RijndaelAlg = new RijndaelManaged())
{
using (ICryptoTransform decryptor = RijndaelAlg.CreateDecryptor(key, iv))
{
// Create a CryptoStream using the FileStream and the passed key and initialization vector (iv).
using (CryptoStream cryptoStreamDecr = new CryptoStream(msDecryp, decryptor, CryptoStreamMode.Read))
{
byte[] decryptedContent = new byte[finalContent.Length];
cryptoStreamDecr.Read(decryptedContent, 0, finalContent.Length);

string decrString = Encoding.UTF8.GetString(decryptedContent);
char[] sep = { '\0' };
return decrString.TrimEnd(sep);
}
}
}
}
}

Regards,
Basawareddy M Gopsen



Responses

Posted by: Virendradugar on: 12/3/2009 [Member] [MVP] Silver

Up
0
Down
Hi,

Have a look at this article.

http://www.codeproject.com/KB/vb/VB_NET_TripleDES.aspx?df=100&amp;forumid=70758&amp;exp=0&amp;select=1999507

I have implemented in the same way and it worked for me.

Hope this helps..

Thanks,
Virendra Dugar

Basawareddy, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response