本文介绍了用PHP加密;用CryptoJS解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用用PHP加密的CryptoJS解密数据时遇到了一些麻烦。

I am having some trouble decrypting data using CryptoJS that was encrypted in PHP. Maybe somebody can advise me on where I am going wrong?

我正在加密如下:


  1. 获取哈希密码

  2. 将(0,16)的子字符串作为键

  3. 加密(MCRYPT_RIJNDAEL_128)

  4. 将密文编码为base64

  1. Get hashed password
  2. Take substring of (0,16) as the key
  3. Encrypt (MCRYPT_RIJNDAEL_128)
  4. Encode ciphertext as base64

解密时,我也这样做:


  1. 获取哈希密码

  2. 将(0,16)的子字符串作为键

  3. Base64解码密文

  4. 解密

  1. Get hashed password
  2. Take substring of (0,16) as the key
  3. Base64 decode the ciphertext
  4. Decrypt

PHP:

public function encrypt($input, $key) {
    $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
    $input = $this->_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;
}

JavaScript:

JavaScript:

function decrypt(ciphertext, hashedPsw) {
        var key =  hashedPsw.substring(0, 16);

        var key = CryptoJS.enc.Hex.parse(key);

        var options = { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7, keySize:128 / 32 };

        ciphertext = CryptoJS.enc.Base64.parse(ciphertext);
        var decrypted = CryptoJS.AES.decrypt(ciphertext, key);
        return decrypted;
    }


推荐答案

我刚刚找到了答案 :证明问题出在关键编码上。

I just discovered the answer in a previous thread: Turns out that the problem was the key encoding.

这篇关于用PHP加密;用CryptoJS解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:20