- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在客户端(移动设备),我使用 CryptoJS 加密用户密码:
var lib_crypt = require('aes');
$.loginButton.addEventListener('click', function(e){
var key = lib_crypt.CryptoJS.enc.Hex.parse('bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3');
var iv = lib_crypt.CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
var encrypted = lib_crypt.CryptoJS.AES.encrypt($.passwordInput.value, key, { iv: iv });
var password_base64 = encrypted.ciphertext.toString(lib_crypt.CryptoJS.enc.Base64);
return password_base64;
});
在服务器端我想用 mcrypt_decrypt 解密它:
function decryptPassword($password)
{
$key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
$ciphertext_dec = base64_decode($password);
$iv_dec = "101112131415161718191a1b1c1d1e1f";
$ciphertext_dec = substr($ciphertext_dec, 16);
$decryptedPassword = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
return trim($decryptedPassword);
}
我使用相同的 key 和 IV,我做错了什么?
最佳答案
你好,
为了实现这一点,应该考虑使用 key 和 iv,每个都有 32 个十六进制数字,我必须在做我的事情时解决这个问题,这就是它的方式
<!--
This reach.your.crypto.js is just a ficticious placeholder,
that was used replaced by http://crypto-js.googlecode.com/svn/tags/3.1.2/build/,
which does not exist anymore,
which is the path to your CryptoJS library,
that can be downloaded through
https://code.google.com/archive/p/crypto-js/downloads?page=1
-->
<script src="reach.your.crypto.js/rollups/aes.js">
</script>
<script type="text/javascript">
//The key and iv should be 32 hex digits each, any hex digits you want,
//but it needs to be 32 on length each
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 = "Hi, this will be seen uncrypted later on";
//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", "decrypt_in_php.php", true);
xh.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xh.send("encrypted="+encodeURIComponent(encrypted));
</script>
现在用 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 $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
?>
有了这个我们就会得到“嗨,稍后会看到未加密的”:)
关于javascript - 使用 CryptoJS 加密并使用 PHP 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29509934/
我想在我的 AngularJS 应用程序中使用 CryptoJS,但我收到此错误:找不到变量:CryptoJS。 我将其包含在我的 index.html 中: 并尝试加密一些东西: var encr
这是我的 Anguarjs 代码 $httpProvider.defaults.headers.common['key'] = CryptoJS.AES.encrypt('', '=', {
我正在尝试在 CryptoJS 上解密并在 PyCrypto 上加密。 我看到 this 一个很好的答案,就像魅力一样,唯一的问题是它调整 PyCrypto 以与 CryptoJS 一起工作,我更喜欢
我正在尝试使用 CryptoJS AES,如下所示: var msg = "café"; var key = "something"; var c = CryptoJS.AES.encrypt(msg
我需要解密用 CryptoJS 加密的东西。我有用于加密的函数、加密对象的结构和用于加密的数据,但我需要知道该对象的一些值。 函数是: var c = CryptoJS.enc.Utf8.parse(
我正在使用 CryptoJS 手动解密具有一组提供的值的字符串。提供了 secret ,然后采用 SHA256。消息和初始化向量是 base 64 编码的。这就是我正在尝试的,但每次我运行它时,输出都
我有一个包含 SHA1 散列数据的密码表。所以我想用 CryptoJS 进行验证输入。它出错了..但是当我尝试显示它时,结果匹配..我编写了错误的函数吗? 这是我的代码 function checkC
我想使用类似于 SpeakEasy 的 javascript 创建一个 HOTP 客户端 上面的库是为服务器端 javascript 使用而设计的,它使用了 NodeJS。 我想在浏览器的前端 jav
我正在尝试编写一个应用程序,以在浏览器中使用 JS 对文件进行端到端加密。但是我似乎无法正确解密所有文件。 TL;DR 由于整体加密大于 1MB 的文件是不切实际的,因此我尝试逐 block 加密它们
我想连接到蓝牙设备。通信仅通过十六进制字符串。我需要编码一个 16 字节的值。因此,我还期望有一个 16 字节的值。在我的实现中,CryptoJS 总是返回更长的结果。根据文档,不需要 IV。 (“所
我在使用 CryptoJS 时遇到问题。 我尝试加密字符串,但它给出了不同的输出。 字符串:“测试消息” key :“123456” 这是我尝试运行的测试代码 var encrypted =
我正在尝试使用 CryptoJS 加密 32 个字符的字符串: var string = '12345678901234567890123456789012'. 我使用 AES256 的 32 个字符
我需要解密 JavaScript 中的十六进制消息,其结果与用 Java 编写的代码完全相同。然而,使用 CryptoJs 的 Javascript 版本返回一个空结果 Java中的代码: priva
我无法使用 jQuery 访问 encryptedData 变量,但在 alert 中它可以工作!!! $( document ).ready(function() { var te
我已经为 RSASHA256 编写了 javascript,但出现错误 crypto.js 未定义。 function aaa(message,secret) { ; ;
我正在尝试使用 CryptoJS 的 PBKDF2 和盐散列来存储我的密码。这是我对注册中的用户密码进行哈希处理的部分: var passwordInput = document.getElement
我正在学习各种哈希技术,并发现了有趣的库,从 cryptoJs 开始 在文档中,有多个选项定义如下散列哈马卡PBKDF2密码编码器 我理解散列是关于生成密文。 HMAC 的作用是生成消息验证代码。但我
我已经使用以下库很多年了,现在它不可用了。 http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js 有人可以帮忙寻找替代
我已经使用以下库很多年了,现在它不可用了。 http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js 有人可以帮忙寻找替代
我正在尝试将Node.js脚本 转换为Golang。但是,我在解码Base64时总是收到"error: illegal base64 data at input byte 40"错误。有人可以帮我,已
我是一名优秀的程序员,十分优秀!