php加解密的数据 php des解密

php 加密解密如果这前后四位数都是估计的四位,那么就可以实现用一个方法
把这个字符串穿进去,
然后截取,前面的四位和后面的四位,
然后加密好了,再重新拼接好返回即可 。
前端JS AES加密 后端PHP AES加解密!DOCTYPEhtml
html
head
titleaes demo/title
/head
body
script type="text/javascript" src="/images/defaultpic.gif"/script
script type="text/javascript" src="/images/defaultpic.gif"/script
script type="text/javascript"
text = 'did=12345dgversion=1';
var key = '123454536f667445454d537973576562';
key = CryptoJS.enc.Utf8.parse(key);
var iv = "1234577290ABCDEF1264147890ACAE45";
iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt(text, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
}).toString()
document.write(encrypted);
/script
/body
/html
后端
public function encrypt2($input = ''){
$pk = "123454536f667445454d537973576562";
$iv = substr("1234577290ABCDEF1264147890ACAE45", 0, 16);
// $t = 'T10515';
$encrypted = ( mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $pk, $input, MCRYPT_MODE_CBC,$iv) );
return base64_encode($encrypted);
}
public function decrypt2($encrypted){
$pk = "123454536f667445454d537973576562";
$iv = substr("1234577290ABCDEF1264147890ACAE45", 0, 16);
// $t = 'T10515';
// $encrypted = "b7y/JPJFNTfxNVR8H4NNtw==";
returnmcrypt_decrypt(MCRYPT_RIJNDAEL_128, $pk, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv);
}
可以推断,js 部分实际只使用了 iv 的 16 位长度
ps:
js加密后默认会base64_encode
使用php解密需要base64_decode后才一致
所以使用js加密和php解密时候需要注意,php端要先base64_decode再解密,例:
js加密的串是$t=b7y/JPJFNTfxNVR8H4NNtw==
php需要
$password= trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $pk,base64_decode( $t), MCRYPT_MODE_CBC, $iv));
则$password才能正确解析出来
需要引入的两个js文件
ase.js
[javascript] view plain copy
/*
CryptoJS v3.1.2
code.google.com/p/crypto-js
(c) 2009-2013 by Jeff Mott. All rights reserved.
code.google.com/p/crypto-js/wiki/License
*/
var CryptoJS=CryptoJS||function(u,p){var d={},l=d.lib={},s=function(){},t=l.Base={extend:function(a){s.prototype=this;var c=new s;ac.mixIn(a);c.hasOwnProperty("init")||(c.init=function(){c.$super.init.apply(this,arguments)});c.init.prototype=c;c.$super=this;return c},create:function(){var a=this.extend();a.init.apply(a,arguments);return a},init:function(){},mixIn:function(a){for(var c in a)a.hasOwnProperty(c)(this[c]=a[c]);a.hasOwnProperty("toString")(this.toString=a.toString)},clone:function(){return this.init.prototype.extend(this)}},
r=l.WordArray=t.extend({init:function(a,c){a=this.words=a||[];this.sigBytes=c!=p?c:4*a.length},toString:function(a){return(a||v).stringify(this)},concat:function(a){var c=this.words,e=a.words,j=this.sigBytes;a=a.sigBytes;this.clamp();if(j%4)for(var k=0;ka;k++)c[j+k2]|=(e[k2]24-8*(k%4)255)24-8*((j+k)%4);else if(65535e.length)for(k=0;ka;k+=4)c[j+k2]=e[k2];else c.push.apply(c,e);this.sigBytes+=a;return this},clamp:function(){var a=this.words,c=this.sigBytes;a[c2]=4294967295
32-8*(c%4);a.length=u.ceil(c/4)},clone:function(){var a=t.clone.call(this);a.words=this.words.slice(0);return a},random:function(a){for(var c=[],e=0;ea;e+=4)c.push(4294967296*u.random()|0);return new r.init(c,a)}}),w=d.enc={},v=w.Hex={stringify:function(a){var c=a.words;a=a.sigBytes;for(var e=[],j=0;ja;j++){var k=c[j2]24-8*(j%4)255;e.push((k4).toString(16));e.push((k15).toString(16))}return e.join("")},parse:function(a){for(var c=a.length,e=[],j=0;jc;j+=2)e[j3]|=parseInt(a.substr(j,

推荐阅读