Reputation: 5962
I'm trying to encrypt and decrypt text using TripleDESCryptoServiceProvider
.
My requirement is for same plaintext, ciphertext should not be same, for this I have generated different vector each time.
This is code to Encrypt and Decrypt text.
public string EncryptString(string PlainText)
{
GenerateIV();
GenerateKey();
if (PlainText == null || PlainText.Length <= 0)
{
throw new ArgumentNullException("Invalid Plaintext.");
}
if (Key == null || Key.Length <= 0)
{
throw new ArgumentNullException("Key Can Not Be Null Or Empty.");
}
if (IV == null || IV.Length <= 0)
{
throw new ArgumentNullException("Vector Can Not Be Null Or Empty.");
}
byte[] encryptedText;
using (TripleDESCryptoServiceProvider tdsObj = new TripleDESCryptoServiceProvider())
{
if (!isKeyStrengthChecked)
{
bool isWeekKey = TripleDESCryptoServiceProvider.IsWeakKey(Key);
if (isWeekKey)
{
throw new Exception("Weak Key.");
}
else
{
isKeyStrengthChecked = true;
}
}
tdsObj.Key = Key;
tdsObj.IV = IV;
tdsObj.Mode = CipherMode.CBC;
tdsObj.Padding = PaddingMode.PKCS7;
ICryptoTransform encryptor = tdsObj.CreateEncryptor(tdsObj.Key, tdsObj.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter srEncrypt = new StreamWriter(csEncrypt))
{
srEncrypt.Write(PlainText);
}
encryptedText = msEncrypt.ToArray();
}
}
}
return Convert.ToBase64String(encryptedText);
}
public string DecryptString(string cipherText)
{
GenerateIV();
GenerateKey();
if (cipherText == null || cipherText.Length <= 0)
{
throw new ArgumentNullException("Invalid CipherText.");
}
if (Key == null || Key.Length <= 0)
{
throw new ArgumentNullException("Key Can Not Be Null Or Empty.");
}
if (IV == null || IV.Length <= 0)
{
throw new ArgumentNullException("Vector Can Not Be Null Or Empty.");
}
byte[] cipherBytes = Convert.FromBase64String(cipherText);
string PlainText = null;
using (TripleDESCryptoServiceProvider tdsDecrypt = new TripleDESCryptoServiceProvider())
{
tdsDecrypt.Key = Key;
tdsDecrypt.IV = IV;
tdsDecrypt.Mode = CipherMode.CBC;
tdsDecrypt.Padding = PaddingMode.PKCS7;
ICryptoTransform decrytor = tdsDecrypt.CreateDecryptor(Key, IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherBytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decrytor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
PlainText = srDecrypt.ReadToEnd();
}
}
}
}
return PlainText;
}
My complete Code Is Here .Net Fiddle
But when I decrypt Text, I'm not getting the same plain text. What's wrong I have Done.
Upvotes: 0
Views: 192
Reputation: 2200
Try this
Comment // GenerateIV(); GenerateKey();
on both EncryptString and DecryptString function
string plainText1 = "Hello Amit";
string plainText2 = "Hello Amit";
Helper helper = new Helper();
helper.GenerateIV();
helper.GenerateKey();
string encryptedData1 = helper.EncryptString(plainText1);
string encryptedData2 = helper.EncryptString(plainText2);
string getBackText = helper.DecryptString(encryptedData1);
Console.WriteLine(getBackText);
Console.ReadLine();
Upvotes: 0
Reputation: 7528
You generate new IV and key with the decrypt call
public string DecryptString(string cipherText)
{
GenerateIV();
GenerateKey();
if (cipherText == null || cipherText.Length <= 0)
You should use the same IV and key on both encrypt and decrypt
Upvotes: 2