# TripleDES加密解密

DES(Data Encryption Standard)和TripleDES是对称加密的两种实现。

DES和TripleDES基本算法一致，只是TripleDES算法提供的key位数更多，加密可靠性更高。

DES使用的密钥key为8字节，初始向量IV也是8字节。

TripleDES使用24字节的key，初始向量IV也是8字节。

TripleDES加密：

 /// <summary>
/// TripleDES 加密
/// </summary>
/// <param name="toEncrypt"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
private static string DesEncrypt(string toEncrypt, string key) {
var toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
var keyArray = Convert.FromBase64String(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider {
Key = keyArray,
Mode = CipherMode.ECB,
};
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);}

TripleDES解密：

/// <summary>
/// TripleDES解密
/// </summary>
/// <param name="toDecrypt"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string DesDecrypt(string toDecrypt, string key) {
try {
//先base64解密 因为加密的时候最后走了一道base64加密
var enBytes = Convert.FromBase64String(toDecrypt);
var keyArray = Convert.FromBase64String(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider {
Key = keyArray,
Mode = CipherMode.ECB,
};
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(enBytes, 0, enBytes.Length);
tdes.Clear();
return Encoding.UTF8.GetString(resultArray);
} catch {
return "";
}
}

c#具体实现如下：

using System;
using System.Security.Cryptography;
using System.Text;

namespace TripleDes
{
class Program
{
static void Main(string[] args)
{
//加密的字符串
string source = "Happy Father's Day!";

//用来加密的key 但是最终用在des加密的key
//是这个key的 md5 hash
string sourceKey = "home";

//获取原始key值的 md5 hash
byte[] keyBytes = GetKeyMd5Hash(sourceKey);

string encryptedStr = DesEncrypt(source, keyBytes);
Console.WriteLine("encrypted string: "+ encryptedStr);

string decryptedStr = DesDecrypt(encryptedStr, keyBytes);
Console.WriteLine("Decrypted String: " + decryptedStr);
}

/// <summary>
/// 获取加密key的 md5 hash，最终DES加密的时候使用这个hash值
/// </summary>
/// <param name="key">原始key值</param>
/// <returns></returns>
public static byte[] GetKeyMd5Hash(string key)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyBytes = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();

return keyBytes;
}

/// <summary>
/// TripleDES 加密
/// </summary>
/// <param name="toEncrypt"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string DesEncrypt(string toEncrypt, byte[] privateKey)
{
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider
{
Key = privateKey,
Mode = CipherMode.ECB,
};

ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();

return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

/// <summary>
/// TripleDES解密
/// </summary>
/// <param name="toDecrypt"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string DesDecrypt(string toDecrypt, byte[] privateKey)
{
//先base64解密 因为加密的时候最后走了一道base64加密
byte[] enBytes = Convert.FromBase64String(toDecrypt);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider
{
Key = privateKey,
Mode = CipherMode.ECB,
};

ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(enBytes, 0, enBytes.Length);
tdes.Clear();

return Encoding.UTF8.GetString(resultArray);

}
}
}

PHP实现如下：

<?php

/*
TripleDES加密
*/
function DesEncrypt($data) { //Pad for PKCS7$blockSize = mcrypt_get_block_size('tripledes', 'ecb');
$len = strlen($data);
$pad =$blockSize - ($len %$blockSize);
$data .= str_repeat(chr($pad), $pad);$key = "home";
$key = md5($key,TRUE);
$key .= substr($key,0,8); //comment this if you use 168 bits long key

//Encrypt data
$encData = mcrypt_encrypt('tripledes',$key, $data, 'ecb'); return base64_encode($encData);
}

/*
TripleDES解密
*/
function DesDecrypt($data) {$key = "home";
$key = md5($key, TRUE);
$key .= substr($key, 0, 8);

//Decrypt data
$fromBase64Str = base64_decode($data);
$decData = mcrypt_decrypt('tripledes',$key, $fromBase64Str, 'ecb'); return$decData;
}

/*
测试
*/
$encryptStr = DesEncrypt("Happy Father's Day!"); echo "encrypted string:$encryptStr</br>";
$decryptStr = DesDecrypt($encryptStr);
echo "decrypted string: \$decryptStr";
?>

js加密：

function desEncrypt(str, key){
key = CryptoJS.enc.Base64.parse(key);
// Triple DES 加密
var encrypted = CryptoJS.TripleDES.encrypt(str, key, {
mode: CryptoJS.mode.ECB,
}