这是我的代码块, 我假设Decrypt方法不起作用,它返回一些垃圾值。我是新手。
public string Encrypt(string toEncrypt, string key, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.Zeros;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public string Decrypt(string toDecrypt, string key, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.Zeros;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
String key = "ShHhd8a08JhJiho98ayslcjh";
string tmp = "My Name Is Tanmoy Mitra";
string encryptStr = Encrypt(tmp,key,false);
Debug.Log("Encrypt Text:"+encryptStr);
Debug.Log("Decrypt Text:"+Decrypt(encryptStr,key,false));
但它会返回��2�cU:EIs Tanmoy Mitra
它适用于//CipherMode.ECB模式,但我想知道为什么这种CBC模式在这种情况下不起作用。请帮我解决这个问题。
答案 0 :(得分:2)
您没有指定IV(初始化向量)。加密和解密需要使用相同的IV。例如,一个坏的方式是:
tdes.IV = new byte[tdes.BlockSize / 8];
...加密和解密。更好的方法是在加密时生成一次随机IV(在创建加密服务提供程序时自动完成)并在解密时记住它。
来自IV property documentation(强调我的):
派生自SymmetricAlgorithm类的类使用称为密码块链接(CBC)的链接模式,该模式需要密钥和初始化向量来对数据执行加密转换。 要解密使用其中一个SymmetricAlgorithm类加密的数据,必须将Key属性和IV属性设置为用于加密的相同值。
答案 1 :(得分:0)
试试这个......
[Serializable]
public class CriptoEngine
{
#region Variables
private string key;
byte[] keyArray;
byte[] arrayDecypt;
byte[] resultArray;
byte[] arrayEncrypt;
#endregion
#region Metodos Publicos
public CriptoEngine(string user)
{
key = user;
}
public TDecrypData SetEncriptar<TEncr, TDecrypData>(TEncr objEncrypt)
{
object tEncr;
arrayEncrypt = UTF8Encoding.UTF8.GetBytes(Convert.ToString(objEncrypt));
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray; tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
resultArray = cTransform.TransformFinalBlock(arrayEncrypt, 0, arrayEncrypt.Length);
tdes.Clear();
Type t = typeof(TDecrypData);
switch (t.Name)
{
case "Byte[]":
tEncr = resultArray;
break;
default:
tEncr = Convert.ToBase64String(resultArray, 0, resultArray.Length);
break;
}
return (TDecrypData)tEncr;
}
public TEncryptData GetDescr<TDesc, TEncryptData>(TDesc objDecry)
{
arrayDecypt = Convert.FromBase64String(objDecry.ToString());
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
resultArray = cTransform.TransformFinalBlock(arrayDecypt, 0, arrayDecypt.Length);
tdes.Clear();
object crypto = UTF8Encoding.UTF8.GetString(resultArray);
return (TEncryptData)crypto;
}
#endregion
}