C#将字母或数字加密成字母的方法
2021-07-25 09:04
{
static List
#region 加密 public static string Encrypt(string str)
///
/// 加密
///
/// 要加密的字符串
///
public static string Encrypt(string str)
{
MappingList = new List
for (char c = ‘0’; c
MappingList.Add(new KeyValuePair
for (char c = ‘a’; c
MappingList.Add(new KeyValuePair
return Encoding.ASCII.GetBytes(str)
.Select((b, i) => (b ^ ((byte)(0xa0 + i))).ToString(“x2”))
.Aggregate(“”, (s, c) => s + c)
.ToCharArray().Select(c => MappingList.First(kv => kv.Key == c).Value)
.Aggregate(“”, (s, c) => s + c);
}
#endregion
#region 解密 public static string Decrypt(string str)
///
/// 解密
///
/// 解密的字符串
///
public static string Decrypt(string str)
{
string base16 = str.ToCharArray()
.Select(c => MappingList.First(kv => kv.Value == c).Key)
.Aggregate(“”, (s, c) => s + c);
return Encoding.ASCII.GetString((new byte[base16.Length / 2])
.Select((b, i) => (byte)(Convert.ToByte(base16.Substring(i * 2, 2), 16) ^ ((byte)(0xa0 + i)))).ToArray());
}
#endregion
}