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);
}
}
If you write a little test, your problem becomes obvious,
In my testing, every attempted roundtrip fails. This confirms Damien's comment.
The
keyAfter
is (almost) always somewhat expanded, sometimes doubled in length. In fact, I'm unable to fluke a key that is round trippable, this may have something to do with avoiding weak keys.However, if i try
failures
always equals0
, as expected. So, there is a simple solution.