After SecKeyGeneratePair
for ECC, I try to encrypt plaintext by the public key. SecKeyEncrypt
returns -4(errSecUnimplemented). I am not sure about the padding type is correct. I tried all the types in my xcode, they don't work as well. Can somebody explain why SecKeyEncrypt
returns -4?
(NSData *)encrypt:(NSString *)plainTextString key:(SecKeyRef)publicKey {
NSData *data = [plainTextString dataUsingEncoding:NSUTF8StringEncoding];
size_t encryptedDataLength = SecKeyGetBlockSize(publicKey);
NSMutableData *encryptedData = [[NSMutableData alloc]
initWithLength:encryptedDataLength];
OSStatus err = SecKeyEncrypt(publicKey,
kSecPaddingOAEP,
[data bytes],
[data length],
[encryptedData mutableBytes],
&encryptedDataLength);
NSLog(@"errEXX: %ld", err);
[encryptedData setLength:encryptedDataLength];
return encryptedData;
}