title: Asymmetric public / private key encryption RSA with nodeJS and Objective-C
body: <p>I want to encrypt the communication between my server (written in nodeJS) and the iOS app (written in objective-C) using asymmetric encryption (RSA).</p>
  
  <p>What I am looking to do is to only encrypt the data that I send from the iOS app <strong>(using the public key)</strong> to the server, where the server needs to decrypt the data <strong>(using the private key)</strong> in order to read it.</p>
  
  <p>Using the following library from GitHub, for testing purposes, I was able to successfully encrypt and then decrypt the string "hello world!" using objective-C. <strong>Note:</strong> NSString* encWithPubKey is the encrypted base64 string</p>
  
  <ul>
  <li><p><a href="https://github.com/ideawu/Objective-C-RSA" rel="nofollow">https://github.com/ideawu/Objective-C-RSA</a></p>
  
  <pre><code>NSString *pubkey = @"-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI2bvVLVYrb4B0raZgFP60VXY\ncvRmk9q56QiTmEm9HXlSPq1zyhyPQHGti5FokYJMzNcKm0bwL1q6ioJuD4EFI56D\na+70XdRz1CjQPQE3yXrXXVvOsmq9LsdxTFWsVBTehdCmrapKZVVx6PKl7myh0cfX\nQmyveT/eqyZK1gYjvQIDAQAB\n-----END PUBLIC KEY-----";
  NSString *privkey = @"-----BEGIN PRIVATE KEY-----\nMIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMMjZu9UtVitvgHS\ntpmAU/rRVdhy9GaT2rnpCJOYSb0deVI+rXPKHI9Aca2LkWiRgkzM1wqbRvAvWrqK\ngm4PgQUjnoNr7vRd1HPUKNA9ATfJetddW86yar0ux3FMVaxUFN6F0KatqkplVXHo\n8qXubKHRx9dCbK95P96rJkrWBiO9AgMBAAECgYBO1UKEdYg9pxMX0XSLVtiWf3Na\n2jX6Ksk2Sfp5BhDkIcAdhcy09nXLOZGzNqsrv30QYcCOPGTQK5FPwx0mMYVBRAdo\nOLYp7NzxW/File//169O3ZFpkZ7MF0I2oQcNGTpMCUpaY6xMmxqN22INgi8SHp3w\nVU+2bRMLDXEc/MOmAQJBAP+Sv6JdkrY+7WGuQN5O5PjsB15lOGcr4vcfz4vAQ/uy\nEGYZh6IO2Eu0lW6sw2x6uRg0c6hMiFEJcO89qlH/B10CQQDDdtGrzXWVG457vA27\nkpduDpM6BQWTX6wYV9zRlcYYMFHwAQkE0BTvIYde2il6DKGyzokgI6zQyhgtRJ1x\nL6fhAkB9NvvW4/uWeLw7CHHVuVersZBmqjb5LWJU62v3L2rfbT1lmIqAVr+YT9CK\n2fAhPPtkpYYo5d4/vd1sCY1iAQ4tAkEAm2yPrJzjMn2G/ry57rzRzKGqUChOFrGs\nlm7HF6CQtAs4HC+2jC0peDyg97th37rLmPLB9txnPl50ewpkZuwOAQJBAM/eJnFw\nF5QAcL4CYDbfBKocx82VX/pFXng50T7FODiWbbL4UnxICE0UBFInNNiWJxNEb6jL\n5xd0pcy9O2DOeso=\n-----END PRIVATE KEY-----";
  
  NSString *originString = @"hello world!";
  
  // Demo: encrypt with public key
  NSString *encWithPubKey = [RSA encryptString:originString publicKey:pubkey];
  NSLog(@"Enctypted with public key: %@", encWithPubKey);// prints the encrypted string in base64 format
  
  
  // Demo: decrypt with private key
  NSString *decWithPrivKey = [RSA decryptString:encWithPubKey privateKey:privkey];
  NSLog(@"Decrypted with private key: %@", decWithPrivKey);// prints -&gt; hello world!
  </code></pre></li>
  </ul>
  
  <p>Using the following link, for testing purposes as well, I was also able to encrypt and then decrypt the string "hello world!" using nodeJs:</p>
  
  <ul>
  <li><p><a href="https://coolaj86.com/articles/asymmetric-public--private-key-encryption-in-node-js" rel="nofollow">https://coolaj86.com/articles/asymmetric-public--private-key-encryption-in-node-js</a></p>
  
  <pre><code>var fs = require('fs');
  var ursa = require('/PATH/ursa');
  
  var pubkeyAlice = ursa.createPublicKey(fs.readFileSync("/PATH/public.pem"));
  var privkeyAlice = ursa.createPrivateKey(fs.readFileSync("/PATH/private.pem"));
  
  var msg = "hello world!";
  var enc = pubkeyAlice.encrypt(msg, 'utf8', 'base64');
  console.log('encrypted string:'+enc);
  
  var rcv = privkeyAlice.decrypt(enc, 'base64', 'utf8');
  console.log('decrypted', rcv, '\n');
  </code></pre></li>
  </ul>
  
  <p>Both of the following examples printed "hello world!" successfully. Next, I took the encrypted string, <strong>NSString*encWithPubKey</strong>, from what I got in objective-C (which is in base64 format) and then I tried to decrypt it with nodeJS:</p>
  
  <pre><code>    var privkeyAlice = ursa.createPrivateKey(fs.readFileSync("/PATH/private.pem"));
  
      var enc = &lt;  HERE IS THE ENCRYPTED STRING RECEIVED FROM Objective-C : encWithPubKey  &gt;
      var rcv = privkeyAlice.decrypt(enc, 'base64', 'utf8');
      console.log('decrypted', rcv, '\n');
  </code></pre>
  
  <p>However, when doing so, I receive the following error in nodeJS:</p>
  
  <ul>
  <li>Error: error:040A1079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error</li>
  </ul>
  
  <p>From my understanding, this error means that the encrypted string was invalid. However, I am unable to understand why I am getting this error. I am able to encrypt and decrypt the same string if I do it with the same language, however, I am unable to communicate between the two languages.</p>
  
  <p>Please note that BOTH the iOS app and the nodeJS are using the following public and private keys respectively:</p>
  
  <pre><code>-----BEGIN PUBLIC KEY-----
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI2bvVLVYrb4B0raZgFP60VXY
  cvRmk9q56QiTmEm9HXlSPq1zyhyPQHGti5FokYJMzNcKm0bwL1q6ioJuD4EFI56D
  a+70XdRz1CjQPQE3yXrXXVvOsmq9LsdxTFWsVBTehdCmrapKZVVx6PKl7myh0cfX
  QmyveT/eqyZK1gYjvQIDAQAB
  -----END PUBLIC KEY-----
  
  
  -----BEGIN PRIVATE KEY-----
  MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMMjZu9UtVitvgHS
  tpmAU/rRVdhy9GaT2rnpCJOYSb0deVI+rXPKHI9Aca2LkWiRgkzM1wqbRvAvWrqK
  gm4PgQUjnoNr7vRd1HPUKNA9ATfJetddW86yar0ux3FMVaxUFN6F0KatqkplVXHo
  8qXubKHRx9dCbK95P96rJkrWBiO9AgMBAAECgYBO1UKEdYg9pxMX0XSLVtiWf3Na
  2jX6Ksk2Sfp5BhDkIcAdhcy09nXLOZGzNqsrv30QYcCOPGTQK5FPwx0mMYVBRAdo
  OLYp7NzxW/File//169O3ZFpkZ7MF0I2oQcNGTpMCUpaY6xMmxqN22INgi8SHp3w
  VU+2bRMLDXEc/MOmAQJBAP+Sv6JdkrY+7WGuQN5O5PjsB15lOGcr4vcfz4vAQ/uy
  EGYZh6IO2Eu0lW6sw2x6uRg0c6hMiFEJcO89qlH/B10CQQDDdtGrzXWVG457vA27
  kpduDpM6BQWTX6wYV9zRlcYYMFHwAQkE0BTvIYde2il6DKGyzokgI6zQyhgtRJ1x
  L6fhAkB9NvvW4/uWeLw7CHHVuVersZBmqjb5LWJU62v3L2rfbT1lmIqAVr+YT9CK
  2fAhPPtkpYYo5d4/vd1sCY1iAQ4tAkEAm2yPrJzjMn2G/ry57rzRzKGqUChOFrGs
  lm7HF6CQtAs4HC+2jC0peDyg97th37rLmPLB9txnPl50ewpkZuwOAQJBAM/eJnFw
  F5QAcL4CYDbfBKocx82VX/pFXng50T7FODiWbbL4UnxICE0UBFInNNiWJxNEb6jL
  5xd0pcy9O2DOeso=
  -----END PRIVATE KEY-----
  </code></pre>
  
  <p>I've tried to look online, but I wasn't able to find enough resources to solve the problem. Any help will be appreciated! Thank you!</p>