Compatibility rsa.js and rsa.php

Asked
Active3 hr before
Viewed126 times

4 Answers

compatibility
90%

The javascript generates a random number which will become the password that i will further use for aes-encryption. The javascript will use the public exponent and the modulus (public) sent from php from the server in order to encrypt the randomly-generated string in javascript (that will become the aes-key), and will send it back to the php server for decryption. ,The php server will receive the encrypted string and using the private exponent generate from start will be able to decrypt the aes-key (randomly generated by the client-side javascript), thus generating a key-agreement between client-server without outside intervention.,PROBLEM: The javascript encrypts the random string....but not PKCS#1 v1.5.....the phpseclib accepts only PKCS#1 v1.5 padding so the php script is unable to decrypt correctly.,Please help me with finding or modifying the JavaScript in order to output to the php script the encrypted string format PKCS# v 1.5 that it expects.

I send the public exponent and modulus (public) to the javascript.

include('Scripts/phpseclib/Crypt/RSA.php');
require_once('Scripts/phpseclib/Math/BigInteger.php');

session_start();

$rsa = new Crypt_RSA();

extract($rsa - > createKey(512));

$priv = $rsa - > _parseKey($privatekey, CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$privExp = $priv['privateExponent'] - > toHex();
$pubExp = $priv['publicExponent'] - > toHex();
$pubMod = $priv['modulus'] - > toHex();

$rsa - > setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$_SESSION['privatekey'] = $privatekey;
$_SESSION['publickey'] = $publickey;
load more v
88%

load more v
72%

i need some help for solve my problem.,Problem : I want to encrypt a number (A) with public RSA Key from Android platform and then decrypt it on PHP Server with the private key. On each platform, i can encrypt and decrypt data (it works well), but when the PHP script try to decrypt data encrypted from ANDROID, it doesn't work !!,Here data_1 is initialized from the encrypt data (A=5) from android with the RSA Public Key (note : decrypt works well on Android), but after decryption in PHP, i get empty String ...,The following code might help you. Using this you can encrypt/decrypt strings between PHP and Android.

Findhere after my PHP Code for decrypt data :

class MyEncryption {

   public $privkey = '';
   public $pubkey = '';
   public
   function __construct() {

   }

   public
   function initialize() {
      $fp = fopen("./encryption/asasap_public.pub", "r");
      $temp = fread($fp, 8192);
      fclose($fp);
      $this - > pubkey = openssl_pkey_get_public($temp);

      $fp = fopen("./encryption/asasap.pem", "r");
      $temp = fread($fp, 8192);
      fclose($fp);
      $this - > privkey = openssl_get_privatekey($temp, '');

   }

   public
   function encrypt($data) {
      if (openssl_public_encrypt($data, $encrypted, $this - > pubkey))
         $data = base64_encode($encrypted);
      else
         throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');

      return $data;
   }

   public
   function decrypt($data) {
      if (openssl_private_decrypt(base64_decode($data), $decrypted, $this - > privkey))
         $data = $decrypted;
      else
         $data = '';

      return $data;
   }

   public
   function hex2bin($hexdata) {
      $bindata = '';

      for ($i = 0; $i < strlen($hexdata); $i += 2) {
         $bindata. = chr(hexdec(substr($hexdata, $i, 2)));
      }

      return $bindata;
   }
}

And i use this class like here :

$enc = new MyEncryption();
$enc - > initialize();
$data_1 = 'K27booXr0zZK4BQlI45MIPJJjPPkpCCPELGvoK/wKYUwShIWE6szlZtrmV83C5eBIrT/3lxWTH3+IOA+5mefurVUvXmQIV7fXEHNHLphyM6L9gQsMAGZMCroPjWKvJM59OMS/d5dwwhiRgzVarxXSKpxBYhEYWJTu7nRJ+bZKjumeoqnCSpmntIiV+tRYgkYflOU6j2QlesjO5tzj/TL6n7vHSO/O1qafJkzHcv8Kn2hTy+IH7QXm7z5vtjXOucHkvBm1xWORXdifh+ChyVvP16dSEmCaCAH6KqtA4viX/HwRFEi4mIWaYSIQk74NdcnQOpFcTgEu2nDwtHaBMqahw==';
$data_2 = $enc - > decrypt($data_1);

Please find here after the code for ANDROID part :

public byte[] encryptRSA(final InputStream publicKeyFile, String in ) throws IOException, NoSuchAlgorithmException,
   InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException,
   BadPaddingException {
      byte[] encodedKey = new byte[5000];
      publicKeyFile.read(encodedKey);
      X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
      KeyFactory kf = KeyFactory.getInstance("RSA");
      PublicKey pkPublic = kf.generatePublic(publicKeySpec);
      // Encrypt
      Cipher pkCipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
      pkCipher.init(Cipher.ENCRYPT_MODE, pkPublic);
      return pkCipher.doFinal( in .getBytes());
   }

Error are in following Lines :

byte[] encodedKey = new byte[5000];
publicKeyFile.read(encodedKey);

We must read exactely the Public Key :

byte[] encodedKey = new byte[ /*lenght of file*/ ];
publicKeyFile.read(encodedKey);
load more v
65%

Example #1 AES Authenticated Encryption in GCM mode example for PHP 7.1+,Example #2 AES Authenticated Encryption example prior to PHP 7.1, The authentication tag passed by reference when using AEAD cipher mode (GCM or CCM). , The length of the authentication tag. Its value can be between 4 and 16 for GCM mode.

load more v