C# AES解密希伯来字母显示为问号
我有一个包含希伯来字母的字符串, 加密后,当我试图解密加密字符串时,所有希伯来文字母都显示为问号,如-> 这是我用来加密和解密的两种方法C# AES解密希伯来字母显示为问号,c#,encryption,C#,Encryption,我有一个包含希伯来字母的字符串, 加密后,当我试图解密加密字符串时,所有希伯来文字母都显示为问号,如-> 这是我用来加密和解密的两种方法 public static string Encrypt(string dectypted) { byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(dectypted); AesCryptoServiceProvider encdec = new AesCryptoServ
public static string Encrypt(string dectypted)
{
byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(dectypted);
AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
encdec.BlockSize = 128;
encdec.KeySize = 256;
encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
encdec.Padding = PaddingMode.PKCS7;
encdec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = encdec.CreateEncryptor(encdec.Key, encdec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return Convert.ToBase64String(enc) + Key;
}
public static string Decrypt(string enctypted)
{
byte[] encbytes = Convert.FromBase64String(enctypted);
AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
encdec.BlockSize = 128;
encdec.KeySize = 256;
encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
encdec.Padding = PaddingMode.PKCS7;
encdec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = encdec.CreateDecryptor(encdec.Key, encdec.IV);
byte[] dec = icrypt.TransformFinalBlock(encbytes, 0, encbytes.Length);
icrypt.Dispose();
return ASCIIEncoding.ASCII.GetString(dec);
}
有人能告诉我怎么了,为什么我用问号代替希伯来文字母吗?
提前感谢ASCII不能表示希伯来文字符。它只能表示一组有限的拉丁字符和符号。UTF8可能是您想要使用的编码。使用Encoding.UTF8替换ASCIIEncoding.ASCII。使用UTF8,而不是ASCII。请注意,ASCII中的字符代码仅为7位值0-127。请注意,Decrypt不会解密Encrypt的输出,因为您已将密钥附加到Base64数据,因此它不再是有效的Base64。此外,正如@AndrewMorton所指出的,ASCII仅包含7位,因此将其用于密钥和IV将执行缩小转换。也许也可以使用Base64。@Trevor是的,我知道,我后来在程序中缩减了它,我只是展示了我的代码。问题是每个人都提到ascii使用,我改为utf8,它开始工作了