zoukankan      html  css  js  c++  java
  • 转摘一个可逆加密的类(使用3DES加密)

    一、提要

    命名空间:System.Security.Cryptography.TripleDES 类

    简单说明: 表示三重数据加密标准算法的基类,TripleDES 的所有实现都必须从此基类派生。是从 SymmetricAlgorithm 类里继承出来。TripleDES 使用 DES 算法的三次连续迭代。它可以使用两个或三个 56 位密钥。

           使用目的:比较安全的加密一种方式,密钥和矢量的不同,会生产不同的加密字串。因为是DES算法的三次连续迭代,而且算法可逆,这样对于数据保密性和可恢复性都不错。

    二、代码示例

    本代码参照了部分MSDN上的代码示例,再根据自己的实际情况,补充了一部分MSDN上没有提到的内容

    1. using System; 
    2. using System.Security; 
    3. using System.Security.Cryptography; 
    4. using System.IO; 
    5. using System.Text; 
    6. using System.Threading; 
    7. namespace TRIP3DES 
    8.     /// <summary> 
    9.     /// Class1 的摘要说明。 
    10.     /// </summary> 
    11.     public class dllEncrypt 
    12.     { 
    13.       //密钥 
    14.       private const string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3"
    15.       //矢量,矢量可以为空 
    16.       private const string sIV = "qcDY6X+aPLw="
    17.       //构造一个对称算法 
    18.       private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); 
    19.         public dllEncrypt(){} 
    20.       #region public string EncryptString(string Value) 
    21.       /// <summary> 
    22.       /// 加密字符串 
    23.       /// </summary> 
    24.       /// <param name="Value">输入的字符串</param> 
    25.       /// <returns>加密后的字符串</returns> 
    26.       public string EncryptString(string Value) 
    27.       { 
    28.          ICryptoTransform ct; 
    29.          MemoryStream ms; 
    30.          CryptoStream cs; 
    31.          byte[] byt; 
    32.          mCSP.Key = Convert.FromBase64String(sKey); 
    33.          mCSP.IV = Convert.FromBase64String(sIV); 
    34.          //指定加密的运算模式 
    35.          mCSP.Mode = System.Security.Cryptography.CipherMode.ECB; 
    36.         //获取或设置加密算法的填充模式 
    37.          mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7; 
    38.          ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV); 
    39.          byt = Encoding.UTF8.GetBytes(Value); 
    40.          ms = new MemoryStream(); 
    41.          cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); 
    42.          cs.Write(byt, 0, byt.Length); 
    43.          cs.FlushFinalBlock(); 
    44.          cs.Close(); 
    45.          return Convert.ToBase64String(ms.ToArray()); 
    46.       } 
    47.       #endregion 
    48.       #region public string DecryptString(string Value) 
    49.       /// <summary> 
    50.       /// 解密字符串 
    51.       /// </summary> 
    52.       /// <param name="Value">加过密的字符串</param> 
    53.       /// <returns>解密后的字符串</returns> 
    54.       public string DecryptString(string Value) 
    55.       { 
    56.          ICryptoTransform ct; 
    57.          MemoryStream ms; 
    58.          CryptoStream cs; 
    59.          byte[] byt; 
    60.          mCSP.Key = Convert.FromBase64String(sKey); 
    61.          mCSP.IV = Convert.FromBase64String(sIV); 
    62.          mCSP.Mode = System.Security.Cryptography.CipherMode.ECB; 
    63.          mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7; 
    64.          ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV); 
    65.          byt = Convert.FromBase64String(Value); 
    66.          ms = new MemoryStream(); 
    67.          cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); 
    68.          cs.Write(byt, 0, byt.Length); 
    69.          cs.FlushFinalBlock(); 
    70.          cs.Close(); 
    71.          return Encoding.UTF8.GetString(ms.ToArray()); 
    72.       } 
    73.       #endregion 
    74.     } 

    三、总结

    做成类库对于密钥和矢量的保管比较方便,输入输出全部是string型变量,这样也比较方便,密钥的生成可以用mSCP. GenerateKey()来生成,矢量的生成也可以用mCSP.GenerateIV()来生成。大家也可以自己灵活的编写符合自己的3DES算法。

  • 相关阅读:
    Leetcode: Palindrome Permutation
    Leetcode: Ugly Number
    Leetcode: Ugly Number II
    Leetcode: Single Number III
    Leetcode: 3Sum Smaller
    Leetcode: Factor Combinations
    Leetcode: Different Ways to Add Parentheses
    Leetcode: Add Digits
    GigE IP地址配置
    Ubuntu 关闭触摸板
  • 原文地址:https://www.cnblogs.com/ahuang1118/p/1388917.html
Copyright © 2011-2022 走看看