由于涉及到功能是数据加密。所以,在经过小伙伴的查询和测试,我也查询了一些资料来总结一下常用的数据加密算法。
static void Main(string[] args) { string source = "Hello World!"; using (MD5 md5Hash = MD5.Create()) { string hash = GetMd5Hash(md5Hash, source); //对字符串进行加密 Console.WriteLine("The MD5 hash of " + source + " is: " + hash + "."); //输出加密后的字符串 Console.WriteLine("Verifying the hash..."); //对加密后的字符串进行验证 if (VerifyMd5Hash(md5Hash, source, hash)) //判断是否一致 { Console.WriteLine("The hashes are the same."); Console.ReadKey(); } else { Console.WriteLine("The hashes are not same."); Console.ReadKey(); } } } #region "MD5加密" static string GetMd5Hash(MD5 md5Hash, string input) { // 将输入字符串转换为字节数组,然后计算哈希值。 byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); // 创建新的数据收集字节 // 创建一个字符串。 StringBuilder sBuilder = new StringBuilder(); // 通过散列数据的每个字节的环 // 和每一个的一个十六进制字符串格式。 for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // 返回十六进制字符串。 return sBuilder.ToString(); } #endregion #region "验证加密的字符是否一致" // 验证一个字符串的哈希值。 static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash) { //散列输入。 string hashOfInput = GetMd5Hash(md5Hash, input); // 创建一个比较哈希StringComparer。 StringComparer comparer = StringComparer.OrdinalIgnoreCase; if (0 == comparer.Compare(hashOfInput, hash)) //进行比较,类似于登陆时比对数据库中的密码 { return true; } else { return false; } } #endregion
// This code example produces the following output: // The MD5 hash of Hello World! is: ed076287532e86365e841e92bfc50d8c. // Verifying the hash... // The hashes are the same.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim data As String = "加密测试" Dim a As String TextBox1.Text = EncryptDes(data, "12345678", "23456789") a = TextBox1.Text TextBox2.Text = DecryptDes(a, "12345678", "23456789") End Sub Public Function EncryptDes(ByVal SourceStr As String, ByVal myKey As String, ByVal myIV As String) As String '使用的DES对称加密 Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法abcdefgh Dim inputByteArray As Byte() inputByteArray = System.Text.Encoding.Default.GetBytes(SourceStr) des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8个字符,TripleDES要24个字符 des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8个字符,TripleDES要24个字符 Dim ms As New System.IO.MemoryStream Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write) Dim sw As New System.IO.StreamWriter(cs) sw.Write(SourceStr) sw.Flush() cs.FlushFinalBlock() ms.Flush() EncryptDes = Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length) End Function #Region "解密函数" '解密函数 Public Function DecryptDes(ByVal SourceStr As String, ByVal myKey As String, ByVal myIV As String) As String '使用标准DES对称解密 Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法 'Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider'TripleDES算法 des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8个字符,TripleDES要24个字符 des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8个字符,TripleDES要24个字符 Dim buffer As Byte() = Convert.FromBase64String(SourceStr) Dim ms As New System.IO.MemoryStream(buffer) Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read) Dim sr As New System.IO.StreamReader(cs) DecryptDes = sr.ReadToEnd() End Function #End Region