zoukankan      html  css  js  c++  java
  • Code-EncryptDecrypt:DES

    ylbtech-Code-EncryptDecrypt:DES 
    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;
                }
            }
        }
    }
    2、
    2.返回顶部
     
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
     
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Construct Binary Tree from Preorder and Inorder Traversal
    Construct Binary Tree from Inorder and Postorder Traversal
    Maximum Depth of Binary Tree
    Sharepoint 2013 创建TimeJob 自动发送邮件
    IE8 不能够在Sharepoint平台上在线打开Office文档解决方案
    TFS安装与管理
    局域网通过IP查看对方计算机名,通过计算机名查看对方IP以及查看在线所有电脑IP
    JS 隐藏Sharepoint中List Item View页面的某一个字段
    SharePoint Calculated Column Formulas & Functions
    JS 两个一组数组转二维数组
  • 原文地址:https://www.cnblogs.com/storebook/p/12684319.html
Copyright © 2011-2022 走看看