博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# DES加密与解密·· 转载
阅读量:5259 次
发布时间:2019-06-14

本文共 3025 字,大约阅读时间需要 10 分钟。

  public string Encrypt(string stringToEncrypt, string sKey)

    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(stringToEncrypt);
        des.Key = ASCIIEncoding.UTF8.GetBytes(sKey);
        des.IV = ASCIIEncoding.UTF8.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();
    }

    public string Decrypt(string stringToDecrypt, string sKey)

    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        byte[] inputByteArray = new byte[stringToDecrypt.Length / 2];

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

        des.Key = ASCIIEncoding.UTF8.GetBytes(sKey);

        des.IV = ASCIIEncoding.UTF8.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());

    }

 

 

//

 

publicsealedclass DESEncrypt

    ...{
        private DESEncrypt()
        ...{
           
//
           
// TODO: 在此处添加构造函数逻辑
           
//
        }
       
privatestaticstring key ="zhoufoxcn";
       
/**////<summary>
       
/// 对称加密解密的密钥
       
///</summary>
        publicstaticstring Key
        ...{
           
get
            ...{
               
return key;
            }
           
set
            ...{
                key
= value;
            }
        }
       
/**////<summary>
       
/// DES加密
       
///</summary>
       
///<param name="encryptString"></param>
       
///<returns></returns>
        publicstaticstring DesEncrypt(string encryptString)
        ...{
           
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
           
byte[] keyIV = keyBytes;
           
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider provider
=new DESCryptoServiceProvider();
            MemoryStream mStream
=new MemoryStream();
            CryptoStream cStream
=new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray,
0, inputByteArray.Length);
            cStream.FlushFinalBlock();
           
return Convert.ToBase64String(mStream.ToArray());
        }
       
/**////<summary>
       
/// DES解密
       
///</summary>
       
///<param name="decryptString"></param>
       
///<returns></returns>
        publicstaticstring DesDecrypt(string decryptString)
        ...{
           
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
           
byte[] keyIV = keyBytes;
           
byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider provider
=new DESCryptoServiceProvider();
            MemoryStream mStream
=new MemoryStream();
            CryptoStream cStream
=new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray,
0, inputByteArray.Length);
            cStream.FlushFinalBlock();
           
return Encoding.UTF8.GetString(mStream.ToArray());
        }
    }

转载于:https://www.cnblogs.com/VVWW/p/3444048.html

你可能感兴趣的文章
移动、尺寸改变
查看>>
poj2255Tree Recovery【二叉树重构】
查看>>
tcpcopy 流量复制工具
查看>>
vue和react的区别
查看>>
第十一次作业
查看>>
负载均衡策略
查看>>
微信智能开放平台
查看>>
ArcGIS Engine 中的绘制与编辑
查看>>
Oracle--通配符、Escape转义字符、模糊查询语句
查看>>
子网划分讲解及练习(一)
查看>>
c# 文件笔记
查看>>
第一页 - 工具的使用(webstorm)
查看>>
Linux 进程资源用量监控和按用户设置进程限制
查看>>
IE浏览器整页截屏程序(二)
查看>>
D3.js 之 d3-shap 简介(转)
查看>>
制作满天星空
查看>>
类和结构
查看>>
CSS3选择器(二)之属性选择器
查看>>
adidas crazylight 2018 performance analysis review
查看>>
typeset shell 用法
查看>>