Skip to content

Instantly share code, notes, and snippets.

@lkdocs
Last active December 22, 2015 20:29
Show Gist options
  • Save lkdocs/6526862 to your computer and use it in GitHub Desktop.
Save lkdocs/6526862 to your computer and use it in GitHub Desktop.
+(NSString *)encryptRSA:(NSString *)plainTextString key:(SecKeyRef)publicKey {
size_t cipherBufferSize = SecKeyGetBlockSize(publicKey);
uint8_t *cipherBuffer = malloc(cipherBufferSize);
uint8_t *nonce = (uint8_t *)[plainTextString UTF8String];
SecKeyEncrypt(publicKey,
kSecPaddingOAEP,
nonce,
strlen( (char*)nonce ),
&cipherBuffer[0],
&cipherBufferSize);
NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];
return [encryptedData base64EncodedString];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment