C#將字母或數字加密成字母的辦法。本站提示廣大學習愛好者:(C#將字母或數字加密成字母的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#將字母或數字加密成字母的辦法正文
本文實例講述了C#將字母或數字加密成字母的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
public class MD5
{
static List<KeyValuePair<char, char>> MappingList;
#region 加密 public static string Encrypt(string str)
/// <summary>
/// 加密
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns>前往成果的字符串</returns>
public static string Encrypt(string str)
{
MappingList = new List<KeyValuePair<char, char>>();
for (char c = '0'; c <= '9'; c++)
MappingList.Add(new KeyValuePair<char, char>(c, (char)(c - '0' + 'a')));
for (char c = 'a'; c <= 'f'; c++)
MappingList.Add(new KeyValuePair<char, char>(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)
/// <summary>
/// 解密
/// </summary>
/// <param name="str">解密的字符串</param>
/// <returns>前往成果字符串</returns>
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
}
願望本文所述對年夜家的C#法式設計有所贊助。