简体   繁体   English

解密 PHP 中的 crypt-js 加密字符串

[英]Decrypt crypt-js encrypted string in PHP

I have encrypted mutiple strings one-by-one in using crypto-js in react .'react中使用crypto-js时,我已经一一加密了多个字符串。

For encryption I used -对于我使用的加密 -

encryptAES = (text, key) => {
    return CryptoJS.AES.encrypt(text, key).toString();
};

For decryption, I used function like following -对于解密,我使用 function 如下 -

decryptAES = (encryptedBase64, key) => {
    const decrypted = CryptoJS.AES.decrypt(encryptedBase64, key);
    if (decrypted) {
      try {
        console.log(decrypted);
        const str = decrypted.toString(CryptoJS.enc.Utf8);
        if (str.length > 0) {
          return str;
        } else {
          return 'error 1';
        } 
      } catch (e) {
        return 'error 2';
      }
    }
    return 'error 3';
  };

I have uploaded a working sample project of this encryption - decryption here .我在这里上传了这个加密解密的工作示例项目。

For eg, if I encrypt "I live in India" using key - "earth", it would output as - "U2FsdGVkX1+cBvU9yH5fIGVmliJYPXsv4AIosUGH4tA=", and similary it would decrypt successfully with the correct key.例如,如果我使用密钥“地球”加密“我住在印度”,它会将 output 转换为 -“U2FsdGVkX1+cBvU9yH5fIGVmliJYPXsv4AIosUGH4tA=”,类似地,它会使用正确的密钥成功解密。

Now I have multiple encrypted strings stored in my database, but now require them to store un-encrypted, so I wanted to decrypt them in PHP .现在我的数据库中存储了多个加密字符串,但现在要求它们存储未加密的字符串,所以我想在 PHP 中解密它们 I can decrypt them in js using the function mentioned above but I am unable to figure out how to do so in PHP .我可以使用上面提到的 function 在js中解密它们,但我无法弄清楚如何在PHP中这样做。 I have tried this github repository but I couldn't customize it for my use case.我已经尝试过这个github 存储库,但我无法为我的用例定制它。

For decryption, salt and ciphertext must first be determined.对于解密,必须首先确定盐和密文。 To do this, the encrypted data of the CryptoJS code must be Base64 decoded.为此,必须对 CryptoJS 代码的加密数据进行 Base64 解码。 The salt are the second 8 bytes of the Base64 decoded data, followed by the actual ciphertext (the first 8 bytes are the ASCII encoding of Salted__ and can be ignored). salt是Base64解码数据的后8个字节,后面是实际密文(前8个字节是Salted__的ASCII编码,可以忽略)。

After determining the salt, key and IV are to be derived with EVP_BytesToKey() .确定盐之后,将使用EVP_BytesToKey()导出密钥和 IV。 You can find various PHP implementations on the web, eg here .您可以在 web 上找到各种 PHP 实现,例如这里 Note that CryptoJS uses MD5 as digest, so the digest in the linked code must be modified accordingly.请注意,CryptoJS 使用 MD5 作为摘要,因此必须对链接代码中的摘要进行相应修改。

Once key and IV have been determined, the actual ciphertext can be decrypted.一旦确定了密钥和 IV,就可以解密实际的密文。

All together:全部一起:

<?php
// Separate salt and actual ciphertext
$ctOpenSSL = base64_decode("U2FsdGVkX1+cBvU9yH5fIGVmliJYPXsv4AIosUGH4tA=");
$salt = substr($ctOpenSSL, 8, 8);
$ciphertext = substr($ctOpenSSL, 16);

// Derive key and IV
$keyIv = EVP_BytesToKey($salt, "earth");
$key = substr($keyIv, 0, 32);
$iv = substr($keyIv, 32, 16);

// Decrypt
$decrypted = openssl_decrypt($ciphertext, "aes-256-cbc", $key, OPENSSL_RAW_DATA, $iv);
print($decrypted . PHP_EOL); // I live in India

function EVP_BytesToKey($salt, $password) {
    $bytes = '';
    $last = '';
    while(strlen($bytes) < 48) {
        $last = hash('md5', $last . $password . $salt, true);
        $bytes.= $last;
    }
    return $bytes;
} 
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM