[英]Encrypt decrypt password with ToBase64String() - special characters are replaced by?
我正在使用以下代码对解密文本进行加密:
public static string Encrypt(string inputText)
{
if (string.IsNullOrEmpty(inputText))
return string.Empty;
else
{
ASCIIEncoding textConverter = new ASCIIEncoding();
RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform encryptor = myRijndael.CreateEncryptor(_key, _iV);
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
byte[] toEncrypt = textConverter.GetBytes(inputText);
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
return Convert.ToBase64String(msEncrypt.ToArray());
}
}
public static string Decrypt(string inputText)
{
try
{
if (string.IsNullOrEmpty(inputText))
return string.Empty;
else
{
inputText = inputText.Replace(" ", "+");
byte[] encrypted = Convert.FromBase64String(inputText);
ASCIIEncoding textConverter = new ASCIIEncoding();
RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform decryptor = myRijndael.CreateDecryptor(_key, _iV);
MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[encrypted.Length];
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return textConverter.GetString(fromEncrypt).TrimEnd('\x0');
}
}
catch (Exception ex)
{
}
return string.Empty;
}
如果在解密后尝试对äöü++++ 2014M进行加密,它将返回++++ 2014M 。
我该怎么做才能得到相同的文字?
谢谢,
普里亚
这是因为您正在使用ASCIIEncoding
。 ASCII不支持这些字符(äöü),并将其替换为问号。
您可以改用UTF8Encoding
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.