#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. NBUT 1225 NEW RDSP MODE I

    找出循环周期即可了 #include<bits/stdc++.h> using namespace std; int N,M,X; int time(int x,int y,int z) ...

  2. 给指针malloc分配空间后就等于数组吗?

    首先回答这个的问题:严格的说不等于数组,但是可以认为它是个数组一样的使用而不产生任何问题.不过既然这样,那它应该算是个数组吧.所以,一般我们都用“动态数组”这种名字来称呼这种东西. 要讲清楚这个东西, ...

  3. 检测MYSQL不同步发邮件通知的脚本

    脚本代码如下:#!/bin/bash                                                                                   ...

  4. jquery ajax请求了两次问题

    页面有一个请求form: <form method="post" id="signin"> <label for="orderid& ...

  5. WinServer 之 访问同网段服务器 或 同一服务器多虚拟机间的访问

    今天在发布网站时遇到不能访问同网段数据库服务器.具体情况为:web服务端部署在192.168.10.1下,而数据库服务端部署在192.168.10.2下,数据库连接直接配置为192.168.10.2会 ...

  6. 使用get方式提交数据

    get提交代码 import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLE ...

  7. [Java] String.Split 方法的6个重载函数

    String.Split 方法有6个重载函数: 程序代码 1) public string[] Split(params char[] separator) 2) public string[] Sp ...

  8. 【AsyncTask整理 2】 AsyncTask方法代码演示

    Android SDK为我们提供了一个后台任务的处理工具AsyncTask.AsyncTask就是一个封装过的后台任务类顾名思义就是异步任务,方便我们维护,Android开发网提示这样的好处可以解决一 ...

  9. web前端开发(6)

    为了避免全局变量泛滥导致冲突,最简单有效的办法是用匿名函数将脚本包起来,让变量的作用域控制在函数之内.

  10. netbeans 调试 php

    修改php.ini文件 原来配置 [XDebug];zend_extension = "E:\xampp\php\ext\php_xdebug.dll";xdebug.profil ...