NPM Crypto-js和PHP加密解密?

时间:2018-01-12 17:14:40

标签: javascript php encryption

我正在使用crypto js node package作为角度2,如下所示:

 var data = [{id: 1}, {id: 2}]

      // Encrypt 
      var ciphertext = CryptoJS.AES.encrypt("My Text", 'PasswortPassword');
      console.log(ciphertext.toString());

      // Decrypt 
      var bytes  = CryptoJS.AES.decrypt(ciphertext, 'PasswortPassword');
      var decryptedData = (bytes.toString(CryptoJS.enc.Utf8));

      console.log(decryptedData);

我正在使用PHP后端进行API

$Pass = "PasswortPassword";
$Clear = "My Text";

$crypted = fnEncrypt($Clear, $Pass);
echo "Encrypred: ".$crypted."</br>";

$newClear = fnDecrypt('U2FsdGVkX19MGni6xAIr66K/zPgDPM0thb50tUHMe8s=', $Pass);
echo "Decrypred: ".$newClear."</br>";

function fnEncrypt($sValue, $sSecretKey)
{
  return rtrim(
      base64_encode(
          mcrypt_encrypt(
              MCRYPT_RIJNDAEL_256,
              $sSecretKey, $sValue,
              MCRYPT_MODE_ECB,
              mcrypt_create_iv(
                  mcrypt_get_iv_size(
                      MCRYPT_RIJNDAEL_256,
                      MCRYPT_MODE_ECB
                  ),
                  MCRYPT_RAND)
              )
          ), "\0"
      );
}

function fnDecrypt($sValue, $sSecretKey)
{
  return rtrim(
      mcrypt_decrypt(
          MCRYPT_RIJNDAEL_256,
          $sSecretKey,
          base64_decode($sValue),
          MCRYPT_MODE_ECB,
          mcrypt_create_iv(
              mcrypt_get_iv_size(
                  MCRYPT_RIJNDAEL_256,
                  MCRYPT_MODE_ECB
              ),
              MCRYPT_RAND
          )
      ), "\0"
  );
}

现在前端和后端都需要在同一页面上进行加密和解密。但不幸的是,事实并非如此。

我也试图实现下面的代码

  class encryption{

    const KEY = "PasswortPassword";
    const KEY_TYPE = 'A*';

    function __construct(){
      $this->key = pack(encryption::KEY_TYPE, encryption::KEY);
      $this->key_size =  strlen($this->key);
      //echo "Key size: " . $this->key_size . "\n";
      $this->iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
      $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND);
    }

    function encrypt($data_string){
      $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $data_string, MCRYPT_MODE_CBC, $this->iv);
      $ciphertext = $this->iv . $ciphertext;

      # encode the resulting cipher text so it can be represented by a string
      $ciphertext_base64 = base64_encode($ciphertext);

      return $ciphertext_base64;
    }

    function decrypt($data_string){
      $ciphertext_dec = base64_decode($data_string);

      # retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
      $iv_dec = substr($ciphertext_dec, 0, $this->iv_size);

      # retrieves the cipher text (everything except the $iv_size in the front)
      $ciphertext_dec = substr($ciphertext_dec, $this->iv_size);

      # may remove 00h valued characters from end of plain text
      $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);

      return  $plaintext_dec;
    }

  }

  $obj = new encryption();
  echo $obj->encrypt("My Text").'<br>';

  $obj2 = new encryption();
  echo $obj2->decrypt($obj->encrypt("My Text"));

令我惊讶的是,所有这3个代码都没有同步。对于相同的盐和示例文本,我在所有3个场景中获得不同的哈希值。

  

问题:这些AES代码有什么问题?

     

问题:如何将它们配置为同步?

     

问题:还有其他可能的方法来实现这一目标吗?

0 个答案:

没有答案