兼容PHP和Java的des加密解密代码分享
程序员文章站
2023-11-05 21:25:34
php代码:
php代码:
<?php class des { var $key; var $iv; //偏移量 function des($key, $iv=0) { $this->key = $key; if($iv == 0) { $this->iv = $key; } else { $this->iv = $iv; } } //加密 function encrypt($str) { $size = mcrypt_get_block_size ( mcrypt_des, mcrypt_mode_cbc ); $str = $this->pkcs5pad ( $str, $size ); $data=mcrypt_cbc(mcrypt_des, $this->key, $str, mcrypt_encrypt, $this->iv); //$data=strtoupper(bin2hex($data)); //返回大写十六进制字符串 return base64_encode($data); } //解密 function decrypt($str) { $str = base64_decode ($str); //$strbin = $this->hex2bin( strtolower($str)); $str = mcrypt_cbc(mcrypt_des, $this->key, $str, mcrypt_decrypt, $this->iv ); $str = $this->pkcs5unpad( $str ); return $str; } function hex2bin($hexdata) { $bindata = ""; for($i = 0; $i < strlen ( $hexdata ); $i += 2) { $bindata .= chr(hexdec(substr($hexdata, $i, 2))); } return $bindata; } function pkcs5pad($text, $blocksize) { $pad = $blocksize - (strlen ( $text ) % $blocksize); return $text . str_repeat ( chr ( $pad ), $pad ); } function pkcs5unpad($text) { $pad = ord ( $text {strlen ( $text ) - 1} ); if ($pad > strlen ( $text )) return false; if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad) return false; return substr ( $text, 0, - 1 * $pad ); } } $str = 'abcd'; $key= 'asdfwef5'; $crypt = new des($key); $mstr = $crypt->encrypt($str); $str = $crypt->decrypt($mstr); echo $str.' <=> '.$mstr; ?>
java代码:
package com.test; import it.sauronsoftware.base64.base64; import java.security.key; import java.security.securerandom; import java.security.spec.algorithmparameterspec; import javax.crypto.cipher; import javax.crypto.secretkeyfactory; import javax.crypto.spec.deskeyspec; import javax.crypto.spec.ivparameterspec; public class main { public static final string algorithm_des = "des/cbc/pkcs5padding"; /** * des算法,加密 * * @param data 待加密字符串 * @param key 加密私钥,长度不能够小于8位 * @return 加密后的字节数组,一般结合base64编码使用 * @throws cryptexception 异常 */ public static string encode(string key,string data) throws exception { return encode(key, data.getbytes()); } /** * des算法,加密 * * @param data 待加密字符串 * @param key 加密私钥,长度不能够小于8位 * @return 加密后的字节数组,一般结合base64编码使用 * @throws cryptexception 异常 */ public static string encode(string key,byte[] data) throws exception { try { deskeyspec dks = new deskeyspec(key.getbytes()); secretkeyfactory keyfactory = secretkeyfactory.getinstance("des"); //key的长度不能够小于8位字节 key secretkey = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(algorithm_des); ivparameterspec iv = new ivparameterspec(key.getbytes()); algorithmparameterspec paramspec = iv; cipher.init(cipher.encrypt_mode, secretkey,paramspec); byte[] bytes = cipher.dofinal(data); // return byte2hex(bytes); return new string(base64.encode(bytes)); } catch (exception e) { throw new exception(e); } } /** * des算法,解密 * * @param data 待解密字符串 * @param key 解密私钥,长度不能够小于8位 * @return 解密后的字节数组 * @throws exception 异常 */ public static byte[] decode(string key,byte[] data) throws exception { try { securerandom sr = new securerandom(); deskeyspec dks = new deskeyspec(key.getbytes()); secretkeyfactory keyfactory = secretkeyfactory.getinstance("des"); //key的长度不能够小于8位字节 key secretkey = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(algorithm_des); ivparameterspec iv = new ivparameterspec(key.getbytes()); algorithmparameterspec paramspec = iv; cipher.init(cipher.decrypt_mode, secretkey,paramspec); return cipher.dofinal(data); } catch (exception e) { throw new exception(e); } } /** * 获取编码后的值 * @param key * @param data * @return * @throws exception */ public static string decodevalue(string key,string data) { byte[] datas; string value = null; try { datas = decode(key, base64.decode(data.getbytes())); value = new string(datas); } catch (exception e) { value = ""; } return value; } public static void main(string[] args) throws exception { system.out.println("明:abcd ;密:" + main.encode("asdfwef5","abcd")); } }
ps:关于加密技术,本站还提供了如下加密工具供大家参考使用:
md5在线加密工具:http://tools.jb51.net/password/createmd5password
escape加密/解密工具:
在线sha1加密工具:
短链(短网址)在线生成工具:
短链(短网址)在线还原工具:
高强度密码生成器:http://tools.jb51.net/password/createstrongpassword