数据加密(MD5,DES,RSA)简析

来源:互联网 发布:查了啊高清网络电视 编辑:程序博客网 时间:2024/05/20 05:59

         由于涉及到功能是数据加密。所以,在经过小伙伴的查询和测试,我也查询了一些资料来总结一下常用的数据加密算法。

MD5

         MD5加密算法的全称是Message-Digest Algorithm 5,MD5将任意长度的“字符串”变换成一个128bit的大整数,并且它是一个不可逆的字符串变换算法,换句话说就是,即使你看到源程序和算法描述,也无法将一个MD5的值变换会原始的字符串。常用的是在网站注册的时候将密码加密后存储到数据库中,然后在登录的时候将加密的密码和数据库中的加密密码进行比对,如果正确则登录系统,在一定程度上保证了用户信息安全。
         具体是如何实现对字符串加密的,我们通过下面的例子来看一下:

        
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.

DES

         DES全称为Data Encryption Standard,即为数据加密标准,是一种使用密钥加密的块算法,具体的加密原理我们不用深入的了解,会用就行了。拿来主义嘛!DES算法的入口参数有三个:Key、Data、Mode。其中Key为7个字节共56位,是DES算法的工作秘钥;Data为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
             具体的使用方法请看下面的例子(vb.net)。
    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



RSA

            RSA算法是第一个技能用户数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。但是RSA的安全性一直未能得到理论上的证明。它经历了各种攻击,至今未被完全攻破。由于是进行的是大数计算,使得RSA最快的情况也比DES慢上数倍,无论是软件还是硬件实现。速度一直是RSA的缺陷。一般来说只用于少量数据加密。

总结:数据加密是以后我们必修的一项技能,数据安全越来越重要。我们要重视起来。
         


0 0
原创粉丝点击