加密算法—MD5、RSA、DES

最近因为要做一个加密的功能,简单了解了一下加密算法,现在比较常用的有三个加密算法MD5加密算法、RSA加密算法、DES加密算法。

 

    MD5加密算法

    定义:MD5算法是将任意长度的“字节串”变换成一个128bit的大整数,并且它是一个不可逆的字符串变换算法,即使你看到源程序和算法描述,也无法将MD5的值变回原始的字符串。

    应用:在有些操作系统中,用户的密码是以MD5的方式保存的,用户登录时,系统吧用户输入的密码计算成MD5中,然后和数据库中保存的MD5值进行比较,如果相同则登录成功,如果不同则登录失败,而系统并不知道用户的密码是什么。

 

    RSA加密算法

    定义:RSA既能用于数据加密,也能用于数字签名。

    原理:1、找出三个数p、q、r    p、q是相异的中质数, r 是与 (p-1)(q-1) 互质的数,p, q, r 这三个数便是 private key

                2、找出 m, 使得 rm = 1 mod (p-1)(q-1)计算 n = pq  m, n 这两个数便是 public key 

           3、a是要加密的资料, b = a^m mod n, (0 <= b < n),b是加密后的资料

           4、 解密过程 a = b^r mod pq (0 <= c < pq)

    局限性:由于进行的都是大数计算,速度一直是RSA的缺陷。一般来说只用于少量数据加密。 

 

    DES加密算法

    有点:DES密码体制的安全性应该不依赖于算法的保密,其安全性仅以加密密钥的保密为基础(动态的生成key)

   原理:DES算法有三个参数:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。 如Mode为加密,则用Key 去把数据Data进行加密, 生成Data的密码形式(64位)作为DES的输出结果;如Mode为解密,则用Key去把密码形式的数据Data解密,还原为Data的明码形式(64位)作为DES的输出结果。

    应用:我们最后使用的就是DES加密算法,但是由于用户对数据的保密性并没有高度的要求,所以我们没有使用动态的key,而是使用了静态的key.

    加密程序

[html] view plain copy
 
  1. Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法  
  2. Dim inputByteArray As Byte()  
  3. inputByteArray = System.Text.Encoding.Default.GetBytes(SourceStr)  
  4. des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8个字符,TripleDES要24个字符  
  5. des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8个字符,TripleDES要24个字符  
  6. Dim ms As New System.IO.MemoryStream  
  7. Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryp            tography.CryptoStreamMode.Write)  
  8. Dim sw As New System.IO.StreamWriter(cs)  
  9. sw.Write(SourceStr)  
  10. sw.Flush()  
  11. cs.FlushFinalBlock()  
  12. ms.Flush()  
  13. EncryptDes = Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)  



    调用 

[html] view plain copy
 
  1. EDes = EncryptDes(DataData(j, k).Value, "12345678", "abcdefgh")  

    解密

[html] view plain copy
 
  1. <span style="font-size:18px;"><strong>        Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法  
  2.         des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8个字符,TripleDES要24个字符  
  3.         des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8个字符,TripleDES要24个字符  
  4.         Dim buffer As Byte() = Convert.FromBase64String(SourceStr)  
  5.         Dim ms As New System.IO.MemoryStream(buffer)  
  6.         Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read)  
  7.         Dim sr As New System.IO.StreamReader(cs)  
  8.         DecryptDes = sr.ReadToEnd()</strong></span>  

 

    调用

[html] view plain copy
 
  1. <span style="font-size:18px;"><strong>        UserEntity.userId = DecryptDes(mydataset.Tables(0).Rows(Count).Item(0), "12345678", "abcdefgh")  
  2.         UserEntity.userName = DecryptDes(mydataset.Tables(0).Rows(Count).Item(1), "12345678", "abcdefgh")  
  3.         UserEntity.passWord = DecryptDes(mydataset.Tables(0).Rows(Count).Item(2), "12345678", "abcdefgh")  
  4.         UserEntity.realyName = DecryptDes(mydataset.Tables(0).Rows(Count).Item(3), "12345678", "abcdefgh")  
  5.         UserEntity.phoneNumber = DecryptDes(mydataset.Tables(0).Rows(Count).Item(4), "12345678", "abcdefgh")  
  6.         UserEntity.unitName = DecryptDes(mydataset.Tables(0).Rows(Count).Item(5), "12345678", "abcdefgh")  
  7.         UserEntity.unitType = DecryptDes(mydataset.Tables(0).Rows(Count).Item(6), "12345678", "abcdefgh")  
  8.         UserEntity.isDelete = DecryptDes(mydataset.Tables(0).Rows(Count).Item(7), "12345678", "abcdefgh")</strong></span>  

 

    这里,我们只是对这三个算法的简单了解,这三个算法涉及的内容是非常多的,有不对的地方请大牛们多多斧正。

原文地址:https://www.cnblogs.com/miercler/p/5471692.html