zoukankan      html  css  js  c++  java
  • .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());
    
            }
    
        }
    }
    
  • 相关阅读:
    在Asp.Net头部动态加载css和js文件的方法
    关于Ajax开发中Response的ContentType的一些问题
    C#获取存储过程的返回值
    在填写表单中输入全角数字的解决方案
    Eclipse3.7中搭建Android开发环境
    THinkPHP 获取客户端IP 与IP地址查询
    php 操作数组 (合并,拆分,追加,查找,删除等)
    HTML5 LocalStorage 本地存储
    [javascript] IE与火狐下window.event对象的区别
    今天有人问是否可以使用vs2005开发,回答了一下,记录下来
  • 原文地址:https://www.cnblogs.com/xtt321/p/4813783.html
Copyright © 2011-2022 走看看