3DES封装类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Configuration;
/// <summary>
/// 3DES加解密类
/// </summary>
public class TriDES
{
//密钥
//sKey必须使用英文字符,区分大小写,且字符数量是8个,不能多也不能少,否则出错。
private const string sKey = "CaeHuGnh";
//矢量,矢量可以为空
private const string sIV = "neCgHahU";
/// <summary>
/// 使用3DES加密字符串
/// </summary>
/// <param name="_ToEntryptString">需要加密的字符串</param>
/// <returns>加密结果字符串</returns>
public static string Encrypt(string _ToEntryptString)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(_ToEntryptString);
//建立加密对象的密钥和偏移量
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
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>
/// 解密3DES加密字符串
/// </summary>
/// <param name="_ToDecryptString">解密字符串</param>
/// <returns>解密结果字符串</returns>
public static string Decrypt(string _ToDecryptString)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//Put the input string into the byte array
byte[] inputByteArray = new byte[_ToDecryptString.Length / 2];
for (int x = 0; x < _ToDecryptString.Length / 2; x++)
{
int i = (Convert.ToInt32(_ToDecryptString.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
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.Default.GetString(ms.ToArray());
}
}
3DES封装类的更多相关文章
- c#生成静态html文件,封装类
由于这段时间比较轻松,于是想到很多的企业网站,新闻网站需要将页面静态化,于是写了个封装类来实现静态文件的生成,思路比较简单,但未完善,网友可根据自己的思路将此类扩展,运用了简单工厂模式(本来刚开始看设 ...
- Des与3Des加密解密
/// <summary> /// Des和3Des算法 /// </summary> public class Des { /// <summary> /// D ...
- 自动创建WIN32下多级子目录的C++封装类
这是 WIN32 自动创建多级子目录的 C++ 封装类,用法简单. 封装没有采用类的静态函数方式,而是在构造函数里面直接完成工作.没什么具体的原因,只是当时做成这样了, ...
- StackExchange.Redis 访问封装类
最近需要在C#中使用Redis,在Redis的官网找到了ServiceStack.Redis,最后在测试的时候发现这是个坑,4.0已上已经收费,后面只好找到3系列的最终版本,最后测试发现还是有BUG或 ...
- StackExchange.Redis通用封装类分享(转)
阅读目录 ConnectionMultiplexer 封装 RedisHelper 通用操作类封 String类型的封装 List类型的封装 Hash类型的封装 SortedSet 类型的封装 key ...
- [转] 对称加密算法DES、3DES
转自:http://www.blogjava.net/amigoxie/archive/2014/07/06/415503.html 1.对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法, ...
- DES & 3DES 加密算法
JAVA坑 跟其他公司java的对接口,一个细节对到吐血,具体: DesUtil.java(别人的反例) //package base_class; import java.io.IOExceptio ...
- 【MongoDB】 基于C#官方驱动2.2版的封装类
一.前言 最近项目中要用到MongoDB,因此实现做了不少的调研.发现网上很多现有关于MongoDB C#官方驱动的调用方法都是基于1.8版本的,已经不是用了最新的2.2版本.因此我在基于C#官方驱动 ...
- 小心Java中封装类的值比较
一般我们使用数值时,都是使用基本类型,如int.long等,但如果你喜欢使用Integer.Long的包装类,那有一点可就得注意了.先来看下这段代码: /** * * @author trytocat ...
随机推荐
- iOS中几种定时器
在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. iOS中定时器NSTimer的使用 1.初始化 + (NSTimer ...
- IE firefox 兼容性整理
1.尽量用jquery操作. 2.jquery取值时要用准确的方法,attr(), val(), text(), html(). 例如: <span value="a"> ...
- Bootstrap3.0学习第二十二轮(JavaScript插件——弹出框)
详情请查看http://aehyok.com/Blog/Detail/28.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- c# 关于浅拷贝和深拷贝
class Program { static void Main(string[] args) { //浅拷贝 Person p1 = new Person(); p1.Name = "张三 ...
- 解决HTML5布局,兼容IE问题
当我们使用h5的新标签,header,footer,aside,section,article...时,会遇到低版本IE不兼容问题,如下图: 解决方案:引入如下JS代码,即可 (这里我就直接放源码了, ...
- @SuppressWarnings含义
J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. @SuppressWarnings 批注允许您选择 ...
- 14.Android之Layout布局学习
Android布局主要有5种,接下来学习总结下. 1) 最常见的线性布局 LinearLayout 线性布局是Android布局中最简单的布局,也是最常用,最实用的布局. android:orient ...
- GridView动态添加列之后,导致PostBack(回发)页面数据丢失问题解决
直入主题,首先声明,这个问题是无法解决的,特此在这说明 一.如何动态添加列,如下: 在页面重写OnInit事件,至于为什么要在这个事件写,根据页面的声明周期和经验可知(不用去别的地方找了,这个我找了之 ...
- POJ1979 Red and Black
速刷一道DFS Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- Hibernate 缓存机制
一.why(为什么要用Hibernate缓存?) Hibernate是一个持久层框架,经常访问物理数据库. 为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能. 缓存内的数据是对物理数 ...