• .NET DES 加密


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Security;
    using System.IO;
    using System.Security.Cryptography;
    
    namespace DLProject
    {
        class EncryptAndDecrypt
        {
            /// <summary>
            /// 对字符串进行DES加密
            /// </summary>
            /// <param >将要加密的字符串</param>
            /// <param >密钥值(需为8位字符串)</param>
            /// <returns></returns>
            public string EncryptString(string sInputString, string sKey)
            {
                byte[] data = Encoding.ASCII.GetBytes(sInputString);
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                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(data, 0, data.Length);
                cs.FlushFinalBlock();  //用缓冲区的当前状态更新基础数据源或储存库,随后清除缓
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }
            // DES解密字符串
            public string DecryptString(string sInputString, string sKey)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
                //Put  the  input  string  into  the  byte  array  
                byte[] inputByteArray = new byte[sInputString.Length / 2];
                for (int x = 0; x < sInputString.Length; x += 2)
                {
                    int i = Convert.ToInt32(sInputString.Substring(x, 2), 16);
                    inputByteArray[x / 2] = (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);
                //Flush  the  data  through  the  crypto  stream  into  the  memory  stream  
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
    
                //Get  the  decrypted  data  back  from  the  memory  stream  
                //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象  
                StringBuilder ret = new StringBuilder();
    
                return System.Text.Encoding.UTF8.GetString(ms.ToArray());
    
            }
    
        }
    }
    
  • 相关阅读:
    使用python三方库xlrd解析excel数据
    testng之listener
    使用Junit实现批量运行
    gojs绘流程图
    sqlserver
    Android学习笔记之 android:collapseColumns ,android:shrinkColumns 和stretchColumns
    myBatis oracle 与mysql自增问题
    Oracle总结
    Oracle 树操作(select…start with…connect by…prior)
    Oracle 获取当前日期及日期格式
  • 原文地址:https://www.cnblogs.com/xtt321/p/4813783.html
Copyright © 2020-2023  润新知