in a case i need to decrypt texts with diffrent keys i also need to genreate valid keys in code behind.
i am using this line to generate a key.
var key = Encoding.UTF8.GetString(TripleDES.Create().Key);
but can't use that key in this method. it says "Specified key is not a valid size for this algorithm"
how can i fix this situation?
public class CryptoHelper
{
public string Encrypt(string toEncrypt, string key)
{
var toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
var keyArray = Encoding.UTF8.GetBytes(key);
var tdes = new TripleDESCryptoServiceProvider { Key = keyArray, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
var cTransform = tdes.CreateEncryptor();
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public string Decrypt(string cipherString, string key)
{
var toEncryptArray = Convert.FromBase64String(cipherString.Replace(' ', '+'));
var keyArray = Encoding.UTF8.GetBytes(key);
var tdes = new TripleDESCryptoServiceProvider { Key = keyArray, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
var cTransform = tdes.CreateDecryptor();
var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Encoding.UTF8.GetString(resultArray);
}
}