zoukankan      html  css  js  c++  java
  • Code-Helper:EncryptHelper.cs

    ylbtech-Code-Helper:EncryptHelper.cs
    1.返回顶部
    1、
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace Sp.Common
    {
        public class EncryptHelper
        {
            private static string so = "1234567890abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNM";
    
            /// <summary>
            /// 生成内容随机key
            /// </summary>
            /// <returns></returns>
            private static string GetContent()
            {
                Random rand = new Random();
                string str = null;
                for (int i = 0; i < 8; i++)
                {
                    str += so.Substring(rand.Next(62), 1);
                }
                return str;
            }
    
            private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            /// <summary> 
            /// DES加密字符串 
            /// </summary> 
            /// <param name="encryptString">待加密的字符串</param> 
            /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> 
            public static string Encrypt(string encryptString)
            {
                try
                {
                    Random rand = new Random();
                    int r = rand.Next(9);
                    string encryptKey = GetContent();
                    string first = encryptKey.Substring(0, r);
                    string last = encryptKey.Substring(r);
    
                    byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                    byte[] rgbIV = Keys;
                    byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                    DESCryptoServiceProvider dCsp = new DESCryptoServiceProvider();
                    MemoryStream mStream = new MemoryStream();
                    CryptoStream cStream = new CryptoStream(mStream, dCsp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                    cStream.Write(inputByteArray, 0, inputByteArray.Length);
                    cStream.FlushFinalBlock();
                    string str = Convert.ToBase64String(mStream.ToArray());
                    int i = 0;
                    for (int j = str.Length - 1; j > 0; j--)
                    {
                        if (str[j] != '=')
                        {
                            break;
                        }
                        else
                        {
                            i++;
                        }
                    }
                    str = str.Substring(0, str.Length - i);
                    return string.Format("{0}{1}{2}{3}{4}", i.ToString(), r.ToString(), first, str, last);
                }
                catch
                {
                    return encryptString;
                }
            }
    
            /// <summary> 
            /// DES解密字符串 
            /// </summary> 
            /// <param name="decryptString">待解密的字符串</param> 
            /// <returns>解密成功返回解密后的字符串,失败返源串</returns> 
            public static string Decrypt(string decryptString)
            {
                try
                {
                    int code = Int32.Parse(decryptString.Substring(1, 1));
                    int i = Int32.Parse(decryptString.Substring(0, 1));
    
                    string first = decryptString.Substring(2, code);
                    string last = decryptString.Substring(decryptString.Length - 8 + code);
                    string encryptKey = first + last;
                    decryptString = decryptString.Substring(code + 2, decryptString.Length - 10);
                    for (int j = 0; j < i; j++)
                    {
                        decryptString += "=";
                    }
                    byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey);
                    byte[] rgbIV = Keys;
                    byte[] inputByteArray = Convert.FromBase64String(decryptString);
                    DESCryptoServiceProvider dcsp = new DESCryptoServiceProvider();
                    MemoryStream mStream = new MemoryStream();
                    CryptoStream cStream = new CryptoStream(mStream, dcsp.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                    cStream.Write(inputByteArray, 0, inputByteArray.Length);
                    cStream.FlushFinalBlock();
                    return Encoding.UTF8.GetString(mStream.ToArray());
                }
                catch
                {
                    return decryptString;
                }
            }
    
            const string AES_IV = "1234567890000000";//16位
            /// <summary>  
            /// AES加密算法  
            /// </summary>  
            ///<param name="input">明文字符串  
            ///<param name="key">密钥(32位)  
            /// <returns>字符串</returns>  
            public static string EncryptByAES(string input, string key)
            {
                byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32));
                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                {
                    ICryptoTransform encryptor = aesAlg.CreateEncryptor(keyBytes, Encoding.UTF8.GetBytes(AES_IV));
                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                swEncrypt.Write(input);
                            }
    
                            return Convert.ToBase64String(msEncrypt.ToArray());
                        }
                    }
                }
            }
    
            /// <summary>  
            /// AES解密  
            /// </summary>  
            ///<param name="input">密文字节数组  
            ///<param name="key">密钥(32位)  
            /// <returns>返回解密后的字符串</returns>  
            public static string DecryptByAES(string input, string key)
            {
                byte[] inputBytes = Convert.FromBase64String(input);
                byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 32));
                using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
                {
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(keyBytes, Encoding.UTF8.GetBytes(AES_IV));
                    using (MemoryStream msEncrypt = new MemoryStream(inputBytes))
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srEncrypt = new StreamReader(csEncrypt))
                            {
                                return srEncrypt.ReadToEnd();
                            }
                        }
                    }
                }
            }
        }
    }
    2、
    2.返回顶部
     
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
     
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Python 猜数小程序(练习)
    Mysql 字符串日期互转
    MaxCompute 语句笔记
    数据仓库架构
    Python 比较两个字符串的相似度
    Python print
    Python简单计算器
    HashMap为什么线程不安全(死循环+数据丢失过程分析)
    浅谈ArrayList、Vector和LinkedList
    JAVA对象的浅克隆和深克隆
  • 原文地址:https://www.cnblogs.com/storebook/p/12684235.html
Copyright © 2011-2022 走看看