title: Encrypt on JS, decrypt in PHP?
body: <p>I'm currently stuck here right now, this is from client side:</p>
  
  <pre><code>$scope.encryptFormData = function() {
      // Get payment data
      var paymentData = JSON.stringify($scope.payment);
  
      // Generate AES secret key
      var secret_key = $scope.generateKey(paymentData);
      var iv  = CryptoJS.lib.WordArray.random(16);
  
      // Encrypt form data using AES secret key
      var cipherPaymentData = CryptoJS.AES.encrypt(paymentData, secret_key, {iv: iv});
  
      // Encrypt AES secret key with RSA public key from server
      var secret_key_rsa = null;
      var encrypt = new JSEncrypt();
      encrypt.setPublicKey($scope.public_key);
      secret_key_rsa = encrypt.encrypt(secret_key);
  
      // Assign values to form object
      var iv_hex = CryptoJS.enc.Hex.stringify(iv);
      var payment_data_hex = CryptoJS.enc.Hex.stringify(cipherPaymentData.ciphertext);
  
      $scope.payment_data.secret_key = secret_key_rsa;
      $scope.payment_data.iv = iv_hex;
      $scope.payment_data.payment = payment_data_hex;
  
      // Send form data to server
  };
  
  $scope.generateKey = function(p) {
      var salt = CryptoJS.lib.WordArray.random(128/8);
  
      return CryptoJS.PBKDF2(p, salt, { keySize: 512/32, iterations: 1000 });     
  }
  </code></pre>
  
  <p>Now, I send this payload to the server for decryption:</p>
  
  <pre><code>{
      iv: "ae3bafa370bdc0c8a6b47ab1b792ec58"
      payment: "e12f9db635984ec2f146bd34e433ef912580c4b3c7c1efe0a8e3fa6981abefa860630752539a9af88a9db0c198a63b804855d4b56357f75456785d7313908c6e"
      secret_key: "emMKQ2QOWtzQAHhur4FLQCyBSK9zzPzomjWyDbeOM3VpjIKb1aNs9SL4P1nhuizEwuM2os/FXsN6MJz/cwxQWakK3tnVFvmt..."
  }
  </code></pre>
  
  <p>This is my controller function in the server:</p>
  
  <pre><code>public function storePayment(Request $request) {
      $postData = $request-&gt;data;
  
      // Decrypt AES secret key, using private key
      $iv = $postData['iv'];
      $secretKey = $this-&gt;EncryptionService-&gt;decryptData($postData['secret_key']);
  
      // Decrypt AES client form data with secret key
      $paymentData = $this-&gt;EncryptionService-&gt;decryptSecretData($postData['payment'], $secretKey, $iv);
  
      // Print decrypted data
  }
  </code></pre>
  
  <p>From my <code>EncryptionService</code>:</p>
  
  <p>For RSA decryption:</p>
  
  <pre><code>public function decryptData($data) 
  {
      $privateKey = $this-&gt;getPrivateKey();
  
      if (empty($privateKey)) {
          $this-&gt;regenerateKeypair();
      }
  
      $privateKey = $this-&gt;getPrivateKey();
      $binaryData = hex2bin($data);
      openssl_private_decrypt($binaryData, $decrypted, $privateKey);
  
      return $decrypted;
  }
  </code></pre>
  
  <p>For AES decryption:</p>
  
  <pre><code>public function decryptSecretData($data, $secretKey, $iv) 
  {
      $cipher = "aes-256-cbc";
      if (in_array($cipher, openssl_get_cipher_methods()))
      {
          $iv = hex2bin($iv);
          $originalData = openssl_decrypt($data, $cipher, $secretKey, $options=0, $iv);
  
          return $originalData;
      }
  }
  </code></pre>
  
  <p>But right now I'm just getting this error:</p>
  
  <pre><code>file: "...\Services\EncryptionService.php"
  line: 101
  message: "hex2bin(): Input string must be hexadecimal string"
  </code></pre>
  
  <p>It points to this line inside <code>decryptData()</code>:</p>
  
  <pre><code>$binaryData = hex2bin($data);
  </code></pre>
  
  <p>How do I properly send the data from the client so the server can properly decrypt it?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I'm using these libraries:</p>
  
  <p><a href="https://github.com/travist/jsencrypt" rel="nofollow noreferrer">https://github.com/travist/jsencrypt</a></p>
  
  <p><a href="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js" rel="nofollow noreferrer">https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js</a></p>