0

Unable to convert NSString/NSData to SecKeyRef. I have googled for it, but could not find the right answer.

i need to do RSA encryption using public key. I am getting public key from server as NSData. Need to do the conversion to pass as parameter to below code.

- (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 base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return [DJUtility base64forData:encryptedData];
}
suneel
  • 103
  • 8

0 Answers0