📄 aes.cpp
字号:
using System;
using System.Security.Cryptography;
using System.Text;
class Example
{
//输入为一个不定长的字符串,输出为32个字符的16进制的字符串
static string getMd5Hash(string input)
{
//创建一个新的工程
MD5 md5Hasher = MD5.Create();
//将输入的字符串存入一个字节数组,并计算其Hash值
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// 创建一个Stringbuilder存放字节
StringBuilder sBuilder = new StringBuilder();
// 循环计算每个字节的Hash值
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// 返回这个16进制的字符串
return sBuilder.ToString();
}
//验证此Hash值
static bool verifyMd5Hash(string input, string hash)
{
string hashOfInput = getMd5Hash(input);
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}
static void Main()
{
string source ;
Console.Write("Please input your string:");
source=Console.ReadLine();
string hash = getMd5Hash(source);
Console.WriteLine("The MD5 hash of " + source + " is: " + hash + ".");
Console.WriteLine("Verifying the hash...");
if (verifyMd5Hash(source, hash))
{
Console.WriteLine("The hashes are the same.");
}
else
{
Console.WriteLine("The hashes are not same.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -