我用BASE64加密提示“x不是受支持的编码名”,请高手解答,全分给
如题。
函数如下:
public string EncodeBase(string code)
{
string encode = " ";
Encoding encoding = Encoding.GetEncoding( "x ");
byte[] bytes = encoding.GetBytes(code);
try
{
encode = Convert.ToBase64String(bytes);
//encode = System.Text.Encoding.Unicode.GetString(bytes);
}
catch
{
encode = code;
}
return encode;
}
------解决方案--------------------=================
public static string Encrypt(string text)
{
try
{
//将明文转换为 ASCII 字节数组
byte[] bytes = Encoding.ASCII.GetBytes(text);
return Convert.ToBase64String(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return string.Empty;
}
}
public static string Decrypt(string text)
{
try
{
// 将 64位整数组成的字符串转换为8位无符号整数数组
byte[] bytes = Convert.FromBase64String(text);
// 返回解码候的字符串
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return string.Empty;
}
}