JAVA和PHP通用的加解密整理版
2016-08-28 11:47
489 查看
日常开放中 平台中通常不会只有单一的环境,因此跨平台的通讯 通常会使用标准的AES,DES等加密规则 公司的项目开发中 遇到了JAVA和PHP的加密解密跨平台的问题 经过多方查找资料以及研究找出一个通用的基础加解密方案如下 1:JAVA代码 (3DES版)
2:PHP版本(3DES) 3des的已经不再使用了,因此没有专门整理成类 凑活看吧哈哈
3:JAVA版本(AES) 将代码1中的如下行修改
4:PHP版本(AES) 开发中选择了AESclass CryptAES
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import org.apache.log4j.Logger; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * Java版3DES加密解密,适用于PHP版3DES加密解密(PHP语言开发的MCRYPT_3DES算法、MCRYPT_MODE_ECB模式、PKCS7填充方式) * @author G007N */ public class DesBase64Tool { private static SecretKey secretKey = null;//key对象 private static Cipher cipher = null; //私鈅加密对象Cipher private static String keyString = "AKlMU89D3FchIkhKyMma6FiE";//密钥 private static Logger log = Logger.getRootLogg 4000 er(); static{ try { secretKey = new SecretKeySpec(keyString.getBytes(), "DESede");//获得密钥 /*获得一个私鈅加密类Cipher,DESede是算法,ECB是加密模式,PKCS5Padding是填充方式*/ cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); } catch (Exception e) { log.error(e.getMessage(), e); } } /** * 加密 * @param message * @return */ public static String desEncrypt(String message) { String result = ""; //DES加密字符串 String newResult = "";//去掉换行符后的加密字符串 try { cipher.init(Cipher.ENCRYPT_MODE, secretKey); //设置工作模式为加密模式,给出密钥 byte[] resultBytes = cipher.doFinal(message.getBytes("UTF-8")); //正式执行加密操作 BASE64Encoder enc = new BASE64Encoder(); result = enc.encode(resultBytes);//进行BASE64编码 newResult = filter(result); //去掉加密串中的换行符 } catch (Exception e) { log.error(e.getMessage(), e); } return newResult; } /** * 解密 * @param message * @return * @throws Exception */ public static String desDecrypt(String message) throws Exception { String result = ""; try { BASE64Decoder dec = new BASE64Decoder(); byte[] messageBytes = dec.decodeBuffer(message); //进行BASE64编码 cipher.init(Cipher.DECRYPT_MODE, secretKey); //设置工作模式为解密模式,给出密钥 byte[] resultBytes = cipher.doFinal(messageBytes);//正式执行解密操作 result = new String(resultBytes,"UTF-8"); } catch (Exception e) { e.printStackTrace(); } return result; } /** * 去掉加密字符串换行符 * @param str * @return */ public static String filter(String str) { String output = ""; StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) { int asc = str.charAt(i); if (asc != 10 && asc != 13) { sb.append(str.subSequence(i, i+1)); } } output = new String(sb); return output; } /** * 加密解密测试 * @param args */ public static void main(String[] args) { try { String strText = "Hello world!"; String deseResult = desEncrypt(strText);//加密 System.out.println("加密结果:"+deseResult); String desdResult = desDecrypt(deseResult);//解密 System.out.println("解密结果:"+desdResult); } catch (Exception e) { e.printStackTrace(); } } } |
function pkcs5_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } function pkcs5_unpad($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); } $key = "AKlMU89D3FchIkhKyMma6FiE"; //$key = pack("H48", $key); $iv = "0102030405060708"; $iv = pack("H16", $iv); $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, ''); mcrypt_generic_init($td, $key, $iv); $str = base64_encode(mcrypt_generic($td,pkcs5_pad("1qaz2ws",8))); echo $str .""; mcrypt_generic_deinit($td); mcrypt_module_close($td); $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, ''); mcrypt_generic_init($td, $key, $iv); $ttt = pkcs5_unpad(mdecrypt_generic($td, base64_decode($str))); mcrypt_generic_deinit($td); mcrypt_module_close($td); echo $ttt; exit; |
/*密钥为16的倍数*/ private static String keyString = "AKlMU89D3FchIkhK";//密钥 /*AES算法*/ secretKey = new SecretKeySpec(keyString.getBytes(), "AES");//获得密钥 /*获得一个私鈅加密类Cipher,DESede-》AES算法,ECB是加密模式,PKCS5Padding是填充方式*/ cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); |
{ protected $cipher = MCRYPT_RIJNDAEL_128; protected $mode = MCRYPT_MODE_ECB; protected $pad_method = NULL; protected $secret_key = ''; protected $iv = ''; public function set_cipher($cipher) { $this->cipher = $cipher; } public function set_mode($mode) { $this->mode = $mode; } public function set_iv($iv) { $this->iv = $iv; } public function set_key($key) { $this->secret_key = $key; } public function require_pkcs5() { $this->pad_method = 'pkcs5'; } protected function pad_or_unpad($str, $ext) { if ( is_null($this->pad_method) ) { return $str; } else { $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad'; if ( is_callable($func_name) ) { $size = mcrypt_get_block_size($this->cipher, $this->mode); return call_user_func($func_name, $str, $size); } } return $str; } protected function pad($str) { return $this->pad_or_unpad($str, ''); } protected function unpad($str) { return $this->pad_or_unpad($str, 'un'); } public function encrypt($str) { $str = $this->pad($str); $td = mcrypt_module_open($this->cipher, '', $this->mode, ''); if ( empty($this->iv) ) { $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); } else { $iv = $this->iv; } mcrypt_generic_init($td, $this->secret_key, $iv); $cyper_text = mcrypt_generic($td, $str); $rt=base64_encode($cyper_text); //$rt = bin2hex($cyper_text); mcrypt_generic_deinit($td); mcrypt_module_close($td); return $rt; } public function decrypt($str){ $td = mcrypt_module_open($this->cipher, '', $this->mode, ''); if ( empty($this->iv) ) { $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); } else { $iv = $this->iv; } mcrypt_generic_init($td, $this->secret_key, $iv); //$decrypted_text = mdecrypt_generic($td, self::hex2bin($str)); $decrypted_text = mdecrypt_generic($td, base64_decode($str)); $rt = $decrypted_text; mcrypt_generic_deinit($td); mcrypt_module_close($td); return $this->unpad($rt); } public static function hex2bin($hexdata) { $bindata = ''; $length = strlen($hexdata); for ($i=0; $i < $length; $i += 2) { $bindata .= chr(hexdec(substr($hexdata, $i, 2))); } return $bindata; } public static function pkcs5_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } public static function pkcs5_unpad($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); } } $aes = new CryptAES(); //密钥修改成了16位 和JAVA的一致 $aes->set_key('AKlMU89D3FchIkhK'); //$aes->set_key('AKlMU89D3FchIkhKyMma6FiE'); $aes->require_pkcs5(); $rt = $aes->encrypt('1qaz2ws'); echo $rt . '<br/>'; echo $aes->decrypt($rt) . '<br/>'; exit; |
相关文章推荐
- Java和PHP通用的加解密整理版
- JAVA和PHP通用的加解密整理版(AES,DES…)
- JAVA和PHP通用的加解密整理版(AES,DES…)
- Java和Php通用的加解密整理版
- 【JAVA和PHP通用的加解密整理版】
- java和php通用的AES加密解密
- php与java通用AES加密解密算法
- TripleDES加解密Java、C#、php通用代码
- C++与java、php通用解密,openssl
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现【多种语言AES/CBC/PKCS5Padding通用加解密数据】
- DES加密和解密PHP,Java,ObjectC统一的方法
- java 和 C# 通用加密解密
- java之php、Android、JAVA、C# 3DES加密解密
- 通用权限管理系统多语言开发接口 - java,php 调用接口程序,多业务子系统集成
- PHP 识别 java 8位 des 加密和 解密方式
- Php AES加密、解密与Java互操作的问题
- flex-php--des加密解密通用代码
- java php DES 加密解密
- Perl、PHP、Python、Java 和 Ruby 比较【转载+整理】