title: Encrypting a string with RSA using only the modulus and exponent in iOS
body: <p>I know there is a lot of questions and answers similar on here, but I have searched and cannot find one that works for me.</p>
  
  <p>I have a service I want to consume in IOS (6), provided by a third party of which I have no control.</p>
  
  <p>In order to authenticate with the service I need to send my user credentials as an RSA encrypted string, encrypted with their RSA public key.</p>
  
  <p>They have supplied me with an XML file with the following format</p>
  
  <pre><code>&lt;BitStrength&gt;1024&lt;/BitStrength&gt;
  &lt;RSAKeyValue&gt;
  &lt;Modulus&gt;xxxxxxxxxxxxxxxxxxxxx&lt;/Modulus&gt;
  &lt;Exponent&gt;xxxx&lt;/Exponent&gt;
  &lt;/RSAKeyValue&gt;
  </code></pre>
  
  <p>What do I need to do in order to encrypt the string? I am from a DOTNET background so most of the complexity has been obscured for me up to now.</p>
  
  <p>I have tried examples such as this:
  <a href="https://stackoverflow.com/questions/10222524/rsa-implementations-in-objective-c">RSA implementations in Objective C</a>
  but there is no way to build the objects from what I have, they seem to need a cert</p>
  
  <p>i have tried using this tool to convert it to a PEM file, but again the code will not build the cert object.
  <a href="https://superdry.apphb.com/tools/online-rsa-key-converter" rel="nofollow noreferrer">https://superdry.apphb.com/tools/online-rsa-key-converter</a></p>
  
  <p>Thanks in advance for any help.</p>
  
  <p>**** EDIT ****
  This is part of a method I have created using the examples provided, it runs without error but I cant decode the output:</p>
  
  <pre><code>   SStatus status = noErr;
  
  size_t cipherBufferSize;
  uint8_t *cipherBuffer;
  
  // [cipherBufferSize]
  size_t dataSize = [plainTextString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  const uint8_t* textData = [[plainTextString dataUsingEncoding:NSUTF8StringEncoding] bytes];
  
  NSAssert(publicKey, @"The public key being referenced by tag must have been stored in the keychain before attempting to encrypt data using it!");
  
  //  Allocate a buffer
  
  cipherBufferSize = SecKeyGetBlockSize(publicKey);
  // plain text block size must be 11 less than cipher buffer size because of
  // the PKSC1 padding used:
  const size_t blockSizeMinusPadding = cipherBufferSize - 11;
  cipherBuffer = malloc(cipherBufferSize);
  
  NSMutableData* accumulatedEncryptedData = [NSMutableData dataWithCapacity:0];
  
  for (int ii = 0; ii*blockSizeMinusPadding &lt; dataSize; ii++) {
      const uint8_t* dataToEncrypt = (textData+(ii*blockSizeMinusPadding));
      const size_t subsize = (((ii+1)*blockSizeMinusPadding) &gt; dataSize) ? blockSizeMinusPadding-(((ii+1)*blockSizeMinusPadding) - dataSize) : blockSizeMinusPadding;
  
      // Encrypt using the public key.
      status = SecKeyEncrypt(publicKey,
                             kSecPaddingOAEP,
                             dataToEncrypt,
                             subsize,
                             cipherBuffer,
                             &amp;cipherBufferSize
                             );
  
      [accumulatedEncryptedData appendBytes:cipherBuffer length:cipherBufferSize];
  }
  
  if (publicKey) CFRelease(publicKey);
  
  free(cipherBuffer);
  </code></pre>
  
  <p>// return accumulatedEncryptedData;
   return [accumulatedEncryptedData base64EncodedString];</p>