zoukankan      html  css  js  c++  java
  • (原创)常用加密解密

    加密
    代码
    public static string Encrypt(string toEncrypt, bool useHashing)
        {
            
    byte[] keyArray;
            
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            System.Configuration.AppSettingsReader settingsReader 
    = new AppSettingsReader();

            
    string key = (string)settingsReader.GetValue("SecurityKey"typeof(String));

            
    if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 
    = new MD5CryptoServiceProvider();
                keyArray 
    = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));

                hashmd5.Clear();
            }
            
    else
                keyArray 
    = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes 
    = new TripleDESCryptoServiceProvider();

            tdes.Key 
    = keyArray;

            tdes.Mode 
    = CipherMode.ECB;
            tdes.Padding 
    = PaddingMode.PKCS7;

            ICryptoTransform cTransform 
    = tdes.CreateEncryptor();

            
    byte[] resultArray = cTransform.TransformFinalBlock
                    (toEncryptArray, 
    0, toEncryptArray.Length);
            tdes.Clear();
            
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

    解密:

    代码
    public static string Decrypt(string cipherString, bool useHashing)
        {
            
    byte[] keyArray;


            
    byte[] toEncryptArray = Convert.FromBase64String(cipherString);

            System.Configuration.AppSettingsReader settingsReader 
    = new AppSettingsReader();
            
    string key = (string)settingsReader.GetValue("SecurityKey"typeof(String));

            
    if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 
    = new MD5CryptoServiceProvider();
                keyArray 
    = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));

                hashmd5.Clear();
            }
            
    else
            {
                keyArray 
    = UTF8Encoding.UTF8.GetBytes(key);
             }

            TripleDESCryptoServiceProvider tdes 
    = new TripleDESCryptoServiceProvider();

            tdes.Key 
    = keyArray;
            

            tdes.Mode 
    = CipherMode.ECB;

            tdes.Padding 
    = PaddingMode.PKCS7;

            ICryptoTransform cTransform 
    = tdes.CreateDecryptor();
            
    byte[] resultArray = cTransform.TransformFinalBlock
                    (toEncryptArray, 
    0, toEncryptArray.Length);

            tdes.Clear();

            
    return UTF8Encoding.UTF8.GetString(resultArray);
        }
  • 相关阅读:
    读取radio的value值
    Bootstrap初学(一)
    移动测试用例
    Python 打包成exe执行文件
    Python 模块导入
    Sublime Text2编辑器
    发送Email
    读写TXT文档
    JS与Jquery
    自动化测试摸索
  • 原文地址:https://www.cnblogs.com/ghfsusan/p/1620377.html
Copyright © 2011-2022 走看看