/// <summary>
/// Des和3Des算法
/// </summary>
public class Des
{
/// <summary>
/// Des加密
/// </summary>
/// <param name="pToEncrypt">明文</param>
/// <param name="sKey">密钥</param>
/// <returns></returns>
public static string DESEnCode(string pToEncrypt, string sKey)
{
pToEncrypt = HttpContext.Current.Server.UrlEncode(pToEncrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.GetEncoding("UTF-8").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, , 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加密(密钥不能每8位重复,例如:123456781234567812345678,如果这样则算法退化为Des,C#会检测,不能使用)
/// </summary>
/// <param name="a_strString">明文</param>
/// <param name="a_strKey">密钥</param>
/// <returns></returns>
public string Decrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(a_strKey);
DES.Mode = CipherMode.ECB;
DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ICryptoTransform DESDecrypt = DES.CreateEncryptor(); string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, , Buffer.Length));
}
catch (Exception e)
{ }
return result;
}
/// <summary>
/// Des解密
/// </summary>
/// <param name="encryptedString">密文</param>
/// <param name="key">密钥</param>
/// <returns></returns>
public string Decrypt(string encryptedString, string key)
{
byte[] btKey = Encoding.UTF8.GetBytes(key); DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.Mode = CipherMode.ECB;
des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Convert.FromBase64String(encryptedString);
try
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inData, , inData.Length); cs.FlushFinalBlock();
} return Encoding.UTF8.GetString(ms.ToArray());
}
catch
{
return encryptedString;
}
}
}
/// <summary>
/// 3Des解密(密钥不能每8位重复,例如:123456781234567812345678,如果这样则算法退化为Des,C#会检测,不能使用)
/// </summary>
/// <param name="a_strString">密文</param>
/// <param name="a_strKey">密钥</param>
/// <returns></returns>
public string Decrypt3DES(string a_strString, string a_strKey)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(a_strKey);
DES.Mode = CipherMode.ECB;
DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ICryptoTransform DESDecrypt = DES.CreateDecryptor(); string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, , Buffer.Length));
}
catch (Exception e)
{ }
return result;
}
}

Des与3Des加密解密的更多相关文章

  1. iOS 3DES加密解密(一行代码搞定)

    3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...

  2. 简进祥==iOS 3DES加密解密

    3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算法.由于计 ...

  3. 【推荐】JAVA基础◆浅谈3DES加密解密

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  4. C语言单片和C#语言服务器端DES及3DES加密的实现

    原文:C语言单片和C#语言服务器端DES及3DES加密的实现 公司最近在做单片机和C#语言的通信.用的是Socket通信.传输的数据是明文,后来 在会上讨论准备用DES加密(对称加密)来做. 双方约定 ...

  5. C#中使用DES和AES加密解密

    C#中使用DES和AES加密解密 2008-01-12 09:37 using System;using System.Text;using System.Security.Cryptography; ...

  6. C# Java 3DES加密解密 扩展及修正\0 问题

    注: C#已亲测及做扩展, Java 部分未做验证 /// <summary> /// 3DES加密解密 /// ------------------------------------- ...

  7. 3DES加密解密

    C#3DES加密解密,JAVA.PHP可用 using System; using System.Security.Cryptography; using System.Text; namespace ...

  8. 使用DES算法实现加密解密

    使用DES算法实现加密解密 我们常见的加密算法有DES.MD5.IDEA.AES等等,这篇随笔介绍使用DES算法实现加密解密 首先介绍一下DES算法: DES算法为密码体制中的对称密码体制,又被称为美 ...

  9. Java中3DES加密解密与其他语言(如C/C++)通信

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

随机推荐

  1. 安装JBOSS

    下载JBOSS 无需安装 修改环境变量: JBOSS_HOME=/root/jboss-as-7.1.1.Finalexport JBOSS_HOME 进入bin下 ./standalone.sh - ...

  2. 聊一聊log4j2配置文件log4j2.xml

    一.背景 最近由于项目的需要,我们把log4j 1.x的版本全部迁移成log4j 2.x 的版本,那随之而来的slf4j整合log4j的配置(使用Slf4j集成Log4j2构建项目日志系统的完美解决方 ...

  3. mysql事务

    1. 事务并不专属于mysql 2. 事务的ACID特性 1)原子性(atomicity) 一个事务必须被视为一个不可分割的最小工作单元,整个事务中得所有操作要么全部提交成功,要么全部失败回滚,对于一 ...

  4. iOS设计 - 一款APP从设计稿到切图过程概述

    这篇文章站在GUI设计师的角度概述了APP从项目启动到切片输出的过程,相当于工作流程的介绍.这里写的不是一种规范,只是一种工作方法,加上技术的更新是非常快的,大家在具体工作中,一定要灵活运用. 这里我 ...

  5. Google Analytics 链接点击次数记录

    <span> <a href="http://protect-your-family.hansaplast.com.my/upload/<?php echo $fil ...

  6. Office文档在线预览

    工具说明:通过传入文档的Web地址,即可进行Office文档的在线预览. 使用方式: 在http://office.qingshanboke.com地址后,通过url参数传入您想预览的文件路径. 如: ...

  7. ABP中单元测试的技巧:Mock和数据驱动

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:虽然ABP为大家提供了测试的脚手架了,不过有些小技巧还是需要自己探索的. ASP.NE ...

  8. java中的throw与throws的区别

    什么时运行时异常?什么是非运行时异常? 通俗的讲: 运行时异常:就是编译通过,运行时就崩了,比如数组越界. 非运行时异常:就是编译不通过,这时就得必须去处理了.不然就没法运行了. 全面的讲: Thro ...

  9. windows 物理内存获取

    由于我一般使用的虚拟内存, 有时我们需要获取到物理内存中的数据(也就是内存条中的真实数据), 按理说是很简单,打开物理内存,读取就可以了.但似乎没这么简单: #include "window ...

  10. Python 格式化输出

    转载 今天写程序又记不清格式化输出细节了--= =索性整理一下. 注意: 与C/C++  不同的是这里括号后面不需要加' , '号. python print格式化输出. 1. 打印字符串 print ...