久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 CryptoJS 加密并使用 PHP 解密

Encrypt with CryptoJS and decrypt with PHP(使用 CryptoJS 加密并使用 PHP 解密)
本文介紹了使用 CryptoJS 加密并使用 PHP 解密的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在客戶端(移動(dòng)設(shè)備),我使用 CryptoJS 加密用戶密碼:

On the client side (mobile device) I encrypt a users password with 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; 
});

在服務(wù)器端,我想用 mcrypt_decrypt 解密:

On the server side i want to decrypt it with 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);
}

我使用相同的密鑰和 IV,我做錯(cuò)了什么?

I use the same key and IV, what do I do wrong?

推薦答案

雙方的做法不同.

您確實(shí)在 CryptoJS 中解析了 IV,但忘記在 PHP 中解析:

You did parse the IV in CryptoJS, but forgot to do it in PHP:

$iv_dec = pack('H*', "101112131415161718191a1b1c1d1e1f");

為了修復(fù)您的 IV 錯(cuò)誤,您可能注意到前 16 個(gè)字節(jié)是亂碼.當(dāng) IV 錯(cuò)誤時(shí)就會(huì)發(fā)生這種情況.注意 CryptoJS 默認(rèn)使用 CBC 模式,所以 IV 只影響解密時(shí)的第一個(gè)塊.刪除:

To fix that your IV is wrong, you probably noticed that the first 16 bytes are gibberish. That happens when the IV is wrong. Note that CryptoJS uses CBC mode by default, so the IV has only influence on the first block during decryption. Remove this:

$ciphertext_dec = substr($ciphertext_dec, 16);

填充

您可能注意到大多數(shù)明文都沒有正確輸出.它們以一些奇怪的重復(fù)字符結(jié)尾.這是 CryptoJS 中默認(rèn)應(yīng)用的 PKCS#7 填充.您必須自己在 PHP 中刪除填充.好消息是 Maarten Bodewes 為這個(gè)這里提供了適當(dāng)?shù)膹?fù)制粘貼解決方案.

Padding

You probably noticed that most plaintexts don't come out right. They end with some strange repeated characters at the end. This is the PKCS#7 padding that is applied by default in CryptoJS. You have to remove the padding yourself in PHP. Good thing is that Maarten Bodewes has provided a proper copy paste solution for this here.

trim() 可能適用于 ZeroPadding,但不適用于使用 PKCS#7 中定義的適當(dāng)填充方案時(shí).您可以完全刪除 trim() 調(diào)用,因?yàn)樗鼪]有用,并且可能會(huì)導(dǎo)致意外的明文,因?yàn)榱阕止?jié)和空格從頭到尾都被修剪了.

trim() might be appropriate for ZeroPadding, but not when a proper padding scheme like the one defined in PKCS#7 is used. You may remove the trim() call altogether, because it is not useful and may result in unexpected plaintext, becauses zero bytes and whitespace is trimmed from the beginning and end.

這篇關(guān)于使用 CryptoJS 加密并使用 PHP 解密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個(gè)表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設(shè)置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數(shù)組自動(dòng)填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產(chǎn)生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 亚洲自拍偷拍视频 | 日本一区二区高清不卡 | 国产美女在线观看 | 成人h动漫亚洲一区二区 | 日韩精品视频在线 | 亚洲成人精品在线 | 成人国产精品视频 | 久久久av | 羞羞视频在线观看免费观看 | 成人av大全| 久久久日韩精品一区二区三区 | 亚洲一区免费 | 亚洲综合色视频在线观看 | 一级毛片免费完整视频 | 亚洲一区二区三区在线 | 欧美日韩亚洲视频 | 亚洲国产精品久久久久秋霞不卡 | 日韩三级电影一区二区 | 久久精品国产久精国产 | 久久视频精品 | 成人午夜网站 | 午夜精品三区 | 亚洲视频一区在线观看 | 久久高清免费视频 | 亚洲福利一区 | 欧美一级二级在线观看 | 成人在线免费观看 | 天堂一区二区三区四区 | 久久精品一区二区三区四区 | 国内自拍偷拍一区 | 国产亚洲精品久久久久动 | www.久久| 999免费视频| 日韩一二区在线观看 | 日本一卡精品视频免费 | 一区二区三区视频在线观看 | 国产黄色精品 | 欧美五月婷婷 | 欧美日韩不卡合集视频 | 久久久久久国产 | 女人一区 |