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 .
What can I do to get same text?
Thanks,
Priya
This is because you are using ASCIIEncoding
. ASCII does not support these characters (äöü) and replaces them with question marks.
You could use UTF8Encoding
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.