zoukankan      html  css  js  c++  java
  • C# DES加密和解密

         /// <summary>
            /// DES加密
            /// </summary>
            /// <param name="pToEncrypt"></param>
            /// <param name="sKey"></param>
            /// <returns></returns>
            public static string DESEncrypt(string pToEncrypt, string sKey)
            {
                if (sKey == string.Empty)
                    sKey = "pccweb12".PadLeft(8, 'X');
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }
            /// <summary>
            /// DES解密
            /// </summary>
            /// <param name="pToDecrypt"></param>
            /// <param name="sKey"></param>
            /// <returns></returns>
            public static string DESDecrypt(string pToDecrypt, string sKey)
            {
                if (sKey == string.Empty)
                    sKey = "pccweb12".PadLeft(8, 'X');
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                StringBuilder ret = new StringBuilder();

                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }

  • 相关阅读:
    #《Essential C++》读书笔记# 第四章 基于对象的编程风格
    visual studio 2019:error c2760
    #《Essential C++》读书笔记# 第三章 泛型编程风格
    #《Essential C++》读书笔记# 第二章 面向过程的编程风格
    #《Essential C++》读书笔记# 第一章 C++ 编程基础
    Hello, world!
    Linux基础(五)Linux下的文件操作
    Linux基础(四)新手大礼包,Linux需要掌握的基础命令大合集
    Linux基础(三)安装Linux系统中遇到的问题
    Linux基础(二)在vmwaer虚拟机 上安装Linux操作系统
  • 原文地址:https://www.cnblogs.com/jnhe/p/3189121.html
Copyright © 2011-2022 走看看