首先要明确3des加密的模式,是ecb还是cbc还是其他。
以下是php代码:
<?php /** * @author Connor caokang@foxmail.com * @abstract 3des */ class DES { private $key = 'DE2JCADLNF85E890H887Wa6'; //只有CBC模式下需要iv,其他模式下iv会被忽略 //private $iv = 'B500290096000A007600E3005700C4003800A40018008500'; private $iv = '12345678'; /** * 加密 */ public function encrypt($value) { //先确定加密模式,此处以ECB为例 $td = mcrypt_module_open ( MCRYPT_3DES,'', MCRYPT_MODE_ECB,''); //$iv = pack ( 'H16', $this->iv ); $value = $this->PaddingPKCS7 ( $value ); //填充 //$key = pack ( 'H48', $this->key ); mcrypt_generic_init ( $td, $this->key, $this->iv); $ret = base64_encode ( mcrypt_generic ( $td, $value ) ); mcrypt_generic_deinit ( $td ); mcrypt_module_close ( $td ); return $ret; } /** * 解密 */ public function decrypt($value) { $td = mcrypt_module_open ( MCRYPT_3DES, '', MCRYPT_MODE_ECB, '' ); //$iv = pack ( 'H16', $this->iv ); //$key = pack ( 'H48', $this->key ); mcrypt_generic_init ( $td, $this->key,$this->iv ); $ret = trim ( mdecrypt_generic ( $td, base64_decode ( $value ) ) ); $ret = $this->UnPaddingPKCS7 ( $ret ); mcrypt_generic_deinit ( $td ); mcrypt_module_close ( $td ); return $ret; } private function PaddingPKCS7($data) { $padlen = 8 - strlen( $data ) % 8 ; for($i = 0; $i < $padlen; $i ++)$data .= chr( $padlen ); return $data; } private function UnPaddingPKCS7($data) { $padlen = ord (substr($data, (strlen( $data )-1), 1 ) ); if ($padlen > 8 )return $data; for($i = -1*($padlen-strlen($data)); $i < strlen ( $data ); $i ++) { if (ord ( substr ( $data, $i, 1 ) ) != $padlen)return false; } return substr ( $data, 0, -1*($padlen-strlen ( $data ) ) ); } } $d = new DES(); echo '原文:', $str = '10$35521913397$2011-10-271'; echo '<br/>'; echo '密文:', $a = $d->encrypt ( $str ); echo '<br/>'; echo '解密:', $d->decrypt ($a); echo '<br/>';
一下是对应的java样例:
package name; import javax.crypto.spec.SecretKeySpec; import javax.crypto.Cipher; import javax.crypto.SecretKey; import java.security.MessageDigest; import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; import java.text.SimpleDateFormat; import java.util.*; import java.net.URLEncoder; import java.net.URLDecoder; import java.io.UnsupportedEncodingException; public class SecurityUtil { private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish private static final String ENCODODING = "UTF-8"; /** * SHA1 加密,此处返回 Base64( SHA1( str )) * @param sourceString String * @return String */ public String toSHA1(String targetString ) throws Exception{ return encryptSHA1( targetString ); } /** * Base64加密 * @param targetString String * @return String */ public String toBase64(String targetString) throws Exception{ return enCodesBase64( targetString.getBytes() ); } /** * Base64解密,返回 utf-8 String * @param targetString String * @return String */ public String fromBase64(String targetString) throws Exception{ byte[] tmp = deCodebase64( targetString ); if( tmp != null ){ return new String(tmp,ENCODODING); }else{ return targetString; } } /** * ThreeDES加密,此处 toThreeDES = Base64( 3DES( str )) * @param sKey String * @param targetString String * @return String */ public String toThreeDES(String sKey,String targetString) throws Exception{ return encryptMode( sKey.getBytes(),targetString); } /** * ThreeDES解密,此处 fromTreeDES = 3DES( Base64( str )) * @param sKey String * @param targetString String * @return String */ public String fromThreeDES(String sKey,String targetString) throws Exception{ return decryptMode( sKey.getBytes(),targetString); } public static String encryptMode(byte[] keybyte, String inputStr) throws Exception { SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, deskey); return enCodesBase64(c1.doFinal(inputStr.getBytes())); } public static String decryptMode(byte[] keybyte, String inputStr) throws Exception { byte[] input = deCodebase64(inputStr); SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE, deskey); return new String(c1.doFinal(input), ENCODODING); } public static String encryptSHA1(String inputStr) throws Exception { MessageDigest md = MessageDigest.getInstance("SHA1"); md.update(inputStr.getBytes()); byte[] sha = md.digest(); String shaStr = ""; for (int i = 0; i < sha.length; i++) { String hexStr = Integer.toHexString(sha[i] & 0xFF); if (hexStr.length() == 1) hexStr = "0" + hexStr; hexStr = hexStr.toUpperCase(); shaStr = shaStr + hexStr; } //String str=new String(sha).toUpperCase(); return enCodesBase64(shaStr.getBytes()); } private static String enCodesBase64(byte[] input) throws Exception { BASE64Encoder encoder=new BASE64Encoder(); return encoder.encode(input); } private static byte[] deCodebase64(String inputStr) throws Exception { BASE64Decoder decoder = new BASE64Decoder(); return decoder.decodeBuffer(inputStr); //return Base64.decode(inputStr); } }
