与PHP解密相匹配的Javascript加密
Javascript encryption that match with PHP decryption
我有下面的php代码,我想创建一个与下面php代码相同的Javascript函数。此外,在Javascript中加密的数据也可以在php中解密。`
<?php
class Security {
public static function encrypt($input, $key) {
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$input = Security::pkcs5_pad($input, $size);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
private static function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
public static function decrypt($sStr, $sKey) {
$decrypted= mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$sKey,
base64_decode($sStr),
MCRYPT_MODE_ECB
);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s-1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
}
?>
`
在javascript端使用crypto.js。此处的参考
Javascript
var key = CryptoJS.enc.Hex.parse("0123456789abcdef0123456789abcdef");
var iv = CryptoJS.enc.Hex.parse("abcdef9876543210abcdef9876543210");
/*
if you wish to have a more friendly key, you can convert letters to Hex this way:
var a = "D";
var hex_D = a.charCodeAt(0).toString(16);
just to mention,
if it were to binary, it would be:
var binary_D = a.charCodeAt(0).toString(2);
*/
var secret = "secret key goes here";
//crypted
var encrypted = CryptoJS.AES.encrypt(secret, key, {iv : iv});
//and the ciphertext put to base64
encrypted = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
//Assuming you have control on the server side, and know the key and iv hexes(we do),
//the encrypted var is all you need to pass through ajax,
//Let's follow with welcomed pure JS style, to reinforce one and other concept if needed
var xh = new XMLHttpRequest();
xh.open("POST", "php.php", true);
xh.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xh.send("encrypted=" + encodeURIComponent(encrypted));
xh.onreadystatechange = function() {
if (xh.readyState == 4 && xh.status == 200) {
var myArr = (xh.responseText);
console.log(myArr);
}
};
在PHP
<?php
//Here we have the key and iv which we know, because we have just chosen them on the JS,
//the pack acts just like the parse Hex from JS
$key = pack("H*", "0123456789abcdef0123456789abcdef");
$iv = pack("H*", "abcdef9876543210abcdef9876543210");
//Now we receive the encrypted from the post, we should decode it from base64,
$encrypted = base64_decode($_POST["encrypted"]);
$shown = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv);
echo trim($shown);
//Although the decrypted is shown, there may be needed to trim and str_replace some 'r 'n 'x06 'x05, if there is not a better "trim" way to do it though
?>
您也可以使用JXG压缩机
相关文章:
- javascript加密实现,包括可信否认
- 用JavaScript加密字符串,用RSA技术用PHP解密
- 与PHP解密相匹配的Javascript加密
- AES JavaScript加密和Java解密
- 安全的Javascript加密库
- 基于 Java 生成的 RSA 密钥的 JavaScript 加密
- 使用 JavaScript 加密字符串并从服务器端 (Java) 解密
- JavaScript加密工具
- Javascript 加密库(客户端加密 | 服务器端解密)
- 用javascript加密字符串,可以用密码解密,可以'Don’不要轻易被粗暴地逼迫
- 使用Javascript加密HTML的优点和缺点
- C#解密用Javascript加密的数据
- 客户端数据结构的JavaScript加密(或模糊处理)
- 通过javascript加密查询字符串的参数
- RNCryptor IOS+Javascript加密/解密AES 256
- 使用内联JavaScript加密电子邮件地址
- Javascript加密功能-它是如何工作的
- Javascript加密哈希保护加载器
- Javascript加密和安全:避免检查内部请求url
- JavaScript加密字符串没有密钥