1.java
[code lang=”java”]<br />
package com.egame.fee.sdk.pc.util;
</p>
<p>
import java.io.IOException;<br />
import java.io.UnsupportedEncodingException;<br />
import java.security.InvalidKeyException;<br />
import java.security.NoSuchAlgorithmException;<br />
import java.security.SecureRandom;<br />
import java.security.spec.InvalidKeySpecException;
</p>
<p>
import javax.crypto.BadPaddingException;<br />
import javax.crypto.Cipher;<br />
import javax.crypto.IllegalBlockSizeException;<br />
import javax.crypto.NoSuchPaddingException;<br />
import javax.crypto.SecretKey;<br />
import javax.crypto.SecretKeyFactory;<br />
import javax.crypto.spec.DESKeySpec;
</p>
<p>
import sun.misc.BASE64Decoder;<br />
import sun.misc.BASE64Encoder;
</p>
<p>
public class SecretUtilTools {
</p>
<p>
public static String encryptForDES(String souce, String key) throws InvalidKeyException,<br />
NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,<br />
IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {<br />
// DES算法要求有一个可信任的随机数源<br />
SecureRandom sr = new SecureRandom();<br />
// 从原始密匙数据创建DESKeySpec对象<br />
DESKeySpec dks = new DESKeySpec(key.getBytes("UTF-8"));<br />
// 创建一个密匙工厂,然后用它把DESKeySpec转换成 一个SecretKey对象<br />
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");<br />
SecretKey key1 = keyFactory.generateSecret(dks);<br />
// Cipher对象实际完成加密操作<br />
Cipher cipher = Cipher.getInstance("DES");<br />
// 用密匙初始化Cipher对象<br />
cipher.init(Cipher.ENCRYPT_MODE, key1, sr);<br />
// 现在,获取数据并加密<br />
byte encryptedData[] = cipher.doFinal(souce.getBytes("UTF-8"));<br />
// 通过BASE64位编码成字符创形式<br />
String base64Str = new BASE64Encoder().encode(encryptedData);
</p>
<p>
return base64Str;<br />
}
</p>
<p>
public static String decryptForDES(String souce, String key) throws InvalidKeyException,<br />
NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IOException,<br />
IllegalBlockSizeException, BadPaddingException {<br />
// DES算法要求有一个可信任的随机数源<br />
SecureRandom sr = new SecureRandom();<br />
// 从原始密匙数据创建DESKeySpec对象<br />
DESKeySpec dks = new DESKeySpec(key.getBytes());<br />
// 创建一个密匙工厂,然后用它把DESKeySpec转换成 一个SecretKey对象<br />
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");<br />
SecretKey key1 = keyFactory.generateSecret(dks);<br />
// Cipher对象实际完成加密操作<br />
Cipher cipher = Cipher.getInstance("DES");<br />
// 用密匙初始化Cipher对象<br />
cipher.init(Cipher.DECRYPT_MODE, key1, sr);<br />
// 将加密报文用BASE64算法转化为字节数组<br />
byte[] encryptedData = new BASE64Decoder().decodeBuffer(souce);<br />
// 用DES算法解密报文<br />
byte decryptedData[] = cipher.doFinal(encryptedData);<br />
return new String(decryptedData,"UTF-8");<br />
}
</p>
<p>
}<br />
[/code]
2.php
[code lang=”php”]<br />
<?php
</p>
<p>
class SecretUtilTools<br />
{<br />
//加密算法<br />
function encryptForDES(input,key)<br />
{<br />
size = mcrypt_get_block_size('des','ecb');<br />input = this-&gt;pkcs5_pad(input, size);<br />td = mcrypt_module_open('des', '', 'ecb', '');<br />
iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size(td), MCRYPT_RAND);<br />
@mcrypt_generic_init(td,key, iv);<br />data = mcrypt_generic(td,input);<br />
mcrypt_generic_deinit(td);<br />
mcrypt_module_close(td);<br />
data = base64_encode(data);<br />
return data;<br />
}<br />
//解密算法<br />
function decryptForDES(encrypted,key)<br />
{<br />encrypted = base64_decode(encrypted);<br />td = mcrypt_module_open('des','','ecb','');<br />
//使用MCRYPT_DES算法,cbc模式<br />
iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size(td), MCRYPT_RAND);<br />
ks = mcrypt_enc_get_key_size(td);<br />
@mcrypt_generic_init(td,key, iv);<br />
//初始处理<br />decrypted = mdecrypt_generic(td,encrypted);<br />
//解密<br />
mcrypt_generic_deinit(td);<br />
//结束<br />
mcrypt_module_close(td);<br />
y=this-&gt;pkcs5_unpad(decrypted);<br />
returny;<br />
}
</p>
<p>
function pkcs5_pad (text,blocksize)<br />
{<br />
pad =blocksize – (strlen(text) %blocksize);<br />
return text . str_repeat(chr(pad), pad);<br />
}
</p>
<p>
function pkcs5_unpad(text)<br />
{<br />
pad = ord(text{strlen(text)-1});<br />
if (pad &gt; strlen(text))<br />
{<br />
return false;<br />
}<br />
if (strspn(text, chr(pad), strlen(text) – pad) !=pad)<br />
{<br />
return false;<br />
}<br />
return substr(text, 0, -1 *pad);<br />
}<br />
}<br />
?>[/code]
3.c#
[code lang=”csharp”]<br />
using System;<br />
using System.IO;<br />
using System.Security.Cryptography;<br />
using System.Text;<br />
using System.Threading;
</p>
<p>
public class SecretUtilTools<br />
{<br />
public string encryptForDES(string message, string key)<br />
{<br />
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())<br />
{<br />
byte[] inputByteArray = Encoding.UTF8.GetBytes(message);<br />
des.Key = UTF8Encoding.UTF8.GetBytes(key);<br />
des.IV = UTF8Encoding.UTF8.GetBytes(key);<br />
des.Mode = System.Security.Cryptography.CipherMode.ECB;<br />
System.IO.MemoryStream ms = new System.IO.MemoryStream();<br />
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))<br />
{<br />
cs.Write(inputByteArray, 0, inputByteArray.Length);<br />
cs.FlushFinalBlock();<br />
cs.Close();<br />
}<br />
string str = Convert.ToBase64String(ms.ToArray());<br />
ms.Close();<br />
return str;<br />
}<br />
}
</p>
<p>
public string decryptForDES(string message, string key)<br />
{<br />
byte[] inputByteArray = Convert.FromBase64String(message);<br />
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())<br />
{<br />
des.Key = UTF8Encoding.UTF8.GetBytes(key);<br />
des.IV = UTF8Encoding.UTF8.GetBytes(key);<br />
des.Mode = System.Security.Cryptography.CipherMode.ECB;<br />
System.IO.MemoryStream ms = new System.IO.MemoryStream();<br />
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))<br />
{<br />
cs.Write(inputByteArray, 0, inputByteArray.Length);<br />
cs.FlushFinalBlock();<br />
cs.Close();<br />
}<br />
string str = Encoding.UTF8.GetString(ms.ToArray());<br />
ms.Close();<br />
return str;<br />
}<br />
}
</p>
<p>
}<br />
[/code]