C method of encrypting letters or numbers into letters
- 2020-12-20 03:43:31
- OfStack
This article shows an example of how C# encrypts letters or numbers into letters. Share to everybody for everybody reference. Specific implementation methods are as follows:
public class MD5
{
static List<KeyValuePair<char, char>> MappingList;
#region encryption public static string Encrypt(string str)
/// <summary>
/// encryption
/// </summary>
/// <param name="str"> The string to encrypt </param>
/// <returns> The string that returns the result </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 decryption public static string Decrypt(string str)
/// <summary>
/// decryption
/// </summary>
/// <param name="str"> Decrypted string </param>
/// <returns> Return result string </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
}
Hopefully this article has helped you with your C# programming.