#region 3DES
/// <summary>
/// 3DES加密
/// </summary>
/// <param name="strString">需加密的字符串</param>
/// <param name="strKey">密匙</param>
/// <returns></returns>
public static string DES3Encrypt(string strString, string strKey)
{
strString = strString + "".PadLeft();
byte[] bMsg = Encoding.GetEncoding("GBK").GetBytes(strString); int l = (bMsg.Length / + ) * ;
byte[] btMsg = new byte[l];
Array.Copy(bMsg, btMsg, bMsg.Length); byte[] digestOfPassword = Encoding.Default.GetBytes(strKey);
byte[] keyBytes = new byte[];
Array.Copy(digestOfPassword, keyBytes, digestOfPassword.Length);
for (int j = , k = ; j < ; )
{
keyBytes[k++] = keyBytes[j++];
}
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor(keyBytes, keyBytes);
var dd = DESEncrypt.TransformFinalBlock(btMsg, , btMsg.Length);
return byte2hex(dd).ToString().Substring(, (bMsg.Length / + ) * );
} public static String getAdd(int length, String strKey)
{
byte[] btMsg = new byte[length / ]; byte[] digestOfPassword = Encoding.UTF8.GetBytes(strKey);
//byte[] digestOfPassword = Encoding.Default.GetBytes(strKey);
byte[] keyBytes = new byte[];
Array.Copy(digestOfPassword, keyBytes, digestOfPassword.Length);
for (int j = , k = ; j < ; )
{
keyBytes[k++] = keyBytes[j++];
} TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
ICryptoTransform DESEncrypt = DES.CreateEncryptor(keyBytes, keyBytes);
var dd = DESEncrypt.TransformFinalBlock(btMsg, , btMsg.Length);
String rtn = byte2hex(dd);
return rtn.Substring(length);
} /// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
private static string byte2hex(byte[] dd)
{
StringBuilder ret = new StringBuilder();
foreach (var item in dd)
{
ret.AppendFormat("{0:X2}", item);
}
return ret.ToString();
}
/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % ) != )
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / ];
for (int i = ; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * , ), );
return returnBytes;
}
/// <summary>
/// 3DES解密
/// </summary>
/// <param name="strString">需解密的字符串</param>
/// <param name="strKey">密匙</param>
/// <returns></returns>
public static string DES3Decrypt(string strString, string strKey)
{
strString += getAdd(strString.Length, strKey); byte[] digestOfPassword = Encoding.Default.GetBytes(strKey);
byte[] keyBytes = new byte[];
Array.Copy(digestOfPassword, keyBytes, digestOfPassword.Length);
for (int j = , k = ; j < ; )
{
keyBytes[k++] = keyBytes[j++];
}
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
ICryptoTransform DESDecrypt = DES.CreateDecryptor(keyBytes, keyBytes);
string result = "";
try
{
byte[] Buffer = strToToHexByte(strString);
byte[] bb = DESDecrypt.TransformFinalBlock(Buffer, , Buffer.Length);
result = Encoding.GetEncoding("GBK").GetString(bb);
}
catch (Exception e)
{
throw e;
}
return result;
}
#endregion

2015-1-13 添加

在我传递key时,出现

查资料,原来是key的长度大于24了,(IV则是8位,但我没有传递)

修改后的3DES算法

#region 3DES
/// <summary>
/// 3DES加密
/// </summary>
/// <param name="strString">需加密的字符串</param>
/// <param name="strKey">密匙</param>
/// <returns></returns>
public static string DES3Encrypt(string strString, string strKey)
{
byte[] bMsg = Encoding.UTF8.GetBytes(strString);
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB; string key = strKey;
while (key.Length < )
{
key += strKey;
}
byte[] digestOfPassword = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[];
Array.Copy(digestOfPassword.Take().ToArray(), keyBytes, digestOfPassword.Take().ToArray().Length); DES.Key = keyBytes;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
var dd = DESEncrypt.TransformFinalBlock(bMsg, , bMsg.Length);
return Convert.ToBase64String(dd);
} /// <summary>
/// 3DES解密
/// </summary>
/// <param name="strString">需解密的字符串</param>
/// <param name="strKey">密匙</param>
/// <returns></returns>
public static string DES3Decrypt(string strString, string strKey)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
string key = strKey;
while (key.Length < )
{
key += strKey;
}
byte[] digestOfPassword = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[];
Array.Copy(digestOfPassword.Take().ToArray(), keyBytes, digestOfPassword.Take().ToArray().Length);
DES.Key = keyBytes;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = "";
try
{
byte[] Buffer = Convert.FromBase64String(strString);
byte[] bb = DESDecrypt.TransformFinalBlock(Buffer, , Buffer.Length);
result = Encoding.UTF8.GetString(bb);
}
catch (Exception e)
{
throw e;
}
return result;
}
#endregion

3DES 加解密,对长度不限制的更多相关文章

  1. 3DES 加解密

    using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Comm ...

  2. 3DES加解密【示例】

    代码 /**  * 3DES加解密  */ public class DESedeUtils {     private static final String ALGORITHM_MD5 = &qu ...

  3. 3DES加解密 C语言

    3DES(或称为Triple DES),它相当于是对每个数据块应用三次DES加密算法.3*8字节密钥. 设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代 ...

  4. PHP版3DES加解密类

    <?php /** * * PHP版3DES加解密类 * * 可与java的3DES(DESede)加密方式兼容 * * @Author:蓝凤(ilanfeng.com) * * @versio ...

  5. java 与 c# 3des 加解密

    java 与 c# 3des 加解密   主要差异如下: 1.  对于待加密解密的数据,各自的填充模式不一样 C#的模式有:ANSIX923.ISO10126.None.PKCS7.Zero,而Jav ...

  6. 3DES加解密类

    using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace GT.C ...

  7. PHP和.NET通用的加密解密函数类,均使用3DES加解密 .

    以下为php代码 <PRE class=PHP name="code"> </PRE><PRE class=PHP name="code&q ...

  8. 3des加解密算法

    编号:1003时间:2016年4月1日09:51:11功能:openssl_3des加解密算法http://blog.csdn.net/alonesword/article/details/17385 ...

  9. C# And Java 3DES加解密 ECB模式/PKCS7

    c#: /// <summary>     /// 完整     /// </summary>     public class TripleDESHelper1     { ...

随机推荐

  1. (转)用AGG实现高质量图形输出(一)

    AGG是一个开源.高效的跨平台2D图形库.AGG的功能与GDI+的功能非常类似,但提供了比GDI+更灵活的编程接口,其产生的图形的质量也非常高(自称超过GDI+) 使用前AGG的准备工作 下载AGG库 ...

  2. js密码强度

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. C++红旗之更短形式:500多字符且无法遵守原题规则

    Purpose and Scope 研究五星红旗C++代码生成问题的代码压缩方法. 没有最短,仅仅有更短. 已经尽力了.爱因斯坦的三个小板凳里,我这是第四个. 继续深入压缩代码的方法肯定非常诡异了. ...

  4. 【JavaScript】JavaScript脚本代码的位置及在页面中的执行顺序

    三.如何改变Javascript在页面的执行顺序 利用onload <script type="text/javascript">window.onload = f; ...

  5. iOS开发——动画编程Swift篇&(一)UIView基本动画

    UIView基本动画 // MARK: - UIView动画 ------------------------------------- // MARK: - UIView动画-淡入 @IBActio ...

  6. html+css 知识整理

    1.学网页最好的方法:学习别人的网页. 2.文档结构 <html>(超文本标记语言) <head>  <title>     </title>    & ...

  7. NopCommerce使用Autofac实现依赖注入

    NopCommerce的依赖注入是用的AutoFac组件,这个组件在nuget可以获取,而IOC反转控制常见的实现手段之一就是DI依赖注入,而依赖注入的方式通常有:接口注入.Setter注入和构造函数 ...

  8. 关于android在Service中弹出Dialog对话框

    在创建好AlertDialog类型对象后,要 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 同时还 ...

  9. hive和hbase整合的原因和原理

    为什么要进行hive和hbase的整合? hive是高延迟.结构化和面向分析的: hbase是低延迟.非结构化和面向编程的. Hive集成Hbase就是为了使用hbase的一些特性.或者说是中和它们的 ...

  10. Web安全 之 SQL注入

    随着B/S模式应用开发的发展,使用这种模式编写的应用程序也越来越多.相当大一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码,根据 ...