C#将字母或数字加密成字母的方法

2021-07-25 09:04

阅读:436

public class MD5

{

static List> MappingList;

#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(c, (char)(c – ‘0’ + ‘a’)));

for (char c = ‘a’; c

MappingList.Add(new KeyValuePair(c, (char)(c – ‘a’ + ‘u’)));

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

}


评论


亲,登录后才可以留言!