[英]Encrypt decrypt password with ToBase64String() - special characters are replaced by?
I am using following code to encrypt decrypt text: 我正在使用以下代码对解密文本进行加密:
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;
}
If I try to encrypt äöü++++2014M after decryption it returns ???++++2014M . 如果在解密后尝试对äöü++++ 2014M进行加密,它将返回++++ 2014M 。
What can I do to get same text? 我该怎么做才能得到相同的文字?
Thanks, 谢谢,
Priya 普里亚
This is because you are using ASCIIEncoding
. 这是因为您正在使用
ASCIIEncoding
。 ASCII does not support these characters (äöü) and replaces them with question marks. ASCII不支持这些字符(äöü),并将其替换为问号。
You could use UTF8Encoding
instead. 您可以改用
UTF8Encoding
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.