title: RSA Encryption on iOS (RSA/ECB/PKCS1Padding)
body: <p>I need to encrypt a NSString using a public key from a webserver certificate on iOS. This is what I am doing on Android (works fine):</p>
  
  <pre><code>public byte[] Encrypt(String plain) throws NoSuchAlgorithmException,
              NoSuchPaddingException, InvalidKeyException,
              IllegalBlockSizeException, BadPaddingException {
  
          publicKey = "MyPublicKeyStringExtractedFromACertificate"
  
          cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          cipher.init(Cipher.ENCRYPT_MODE, publicKey);
          encryptedBytes = cipher.doFinal(plain.getBytes());
  
          return encryptedBytes;
  }
  </code></pre>
  
  <p>This is what I am trying on iOS:</p>
  
  <pre><code>NSString *publicKey = @"MyPublicKeyStringExtractedFromACertificate"; // Base64 encoded key from my webserver certificate
  NSData *keyData = [[NSData alloc] initWithBase64EncodedString:publicKey options:NSDataBase64DecodingIgnoreUnknownCharacters];
  SecCertificateRef certificate = SecCertificateCreateWithData(kCFAllocatorDefault, ( __bridge CFDataRef) keyData); // this is returning nil
  </code></pre>
  
  <p>The publickey comes from a webservice certificate (on my app bundle).</p>
  
  <p>What I am doing wrong? How could i use SecKeyEncrypt?</p>