zoukankan      html  css  js  c++  java
  • C#简单的加密类

    1.加密

     1 public class EncryptHepler {
     2         // 验值 
     3         static string saltValue = "XXXX";
     4         // 密码值 
     5         static string pwdValue = "XXXX";
     6  
     7         /// <summary>
     8         /// 加密
     9         /// </summary>
    10         public static string Encrypt( string input ) {
    11             byte[ ] data = System.Text.UTF8Encoding.UTF8.GetBytes( input );
    12             byte[ ] salt = System.Text.UTF8Encoding.UTF8.GetBytes( saltValue );
    13  
    14             // AesManaged - 高级加密标准(AES) 对称算法的管理类 
    15             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );
    16             // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数) 
    17             // 通过 密码 和 salt 派生密钥 
    18             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt );
    19  
    20             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
    21             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
    22             aes.Key = rfc.GetBytes( aes.KeySize / 8 );
    23             aes.IV = rfc.GetBytes( aes.BlockSize / 8 );
    24  
    25             // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象 
    26             System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor( );
    27             // 加密后的输出流 
    28             System.IO.MemoryStream encryptStream = new System.IO.MemoryStream( );
    29             // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接 
    30             System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
    31                 ( encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );
    32  
    33             // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
    34             encryptor.Write( data, 0, data.Length );
    35             encryptor.Close( );
    36             // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串 
    37             string encryptedString = Convert.ToBase64String( encryptStream.ToArray( ) );
    38             return encryptedString;
    39         }
    View Code

    2.解密

     1 /// <summary>
     2         /// 解密
     3         /// </summary>
     4         public static string Decrypt( string input ) {
     5             byte[ ] encryptBytes = Convert.FromBase64String( input );
     6             byte[ ] salt = Encoding.UTF8.GetBytes( saltValue );
     7             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );
     8             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt );
     9  
    10             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
    11             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
    12             aes.Key = rfc.GetBytes( aes.KeySize / 8 );
    13             aes.IV = rfc.GetBytes( aes.BlockSize / 8 );
    14  
    15             // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象 
    16             System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor( );
    17             // 解密后的输出流 
    18             System.IO.MemoryStream decryptStream = new System.IO.MemoryStream( );
    19  
    20             // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接 
    21             System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
    22                 decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );
    23             // 将一个字节序列写入当前 CryptoStream (完成解密的过程) 
    24             decryptor.Write( encryptBytes, 0, encryptBytes.Length );
    25             decryptor.Close( );
    26  
    27             // 将解密后所得到的流转换为字符串 
    28             byte[ ] decryptBytes = decryptStream.ToArray( );
    29             string decryptedString = UTF8Encoding.UTF8.GetString( decryptBytes, 0, decryptBytes.Length );
    30             return decryptedString;
    31         }
    32     }//class end
    View Code
  • 相关阅读:
    笔记:Struts2 的 JSON 插件
    笔记:Struts2 拦截器
    笔记:Struts2 文件上传和下载
    笔记:Struts2 文件上传和下载
    【学习总结】推荐系统-协同过滤原理
    【刷题】牛客网看到的鹅厂ML面筋-部分问题RecSys相关
    【刷题】【LeetCode】000-十大经典排序算法
    【刷题】【LeetCode】总
    【问题解决方案】pygame生成的窗口点右上角关闭按钮未响应问题的解决
    【刷题】若串 =’software’ ,其子串数目为:37
  • 原文地址:https://www.cnblogs.com/zxbzl/p/3874940.html
Copyright © 2011-2022 走看看