2

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;
}
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263

1 Answers1

2

Apple's implementation only supports ECDSA, which can be used for signing but not encryption. For information about encrypting using elliptic curves using other schemes see Is it possible to use elliptic curve cryptography for encrypting data?

Community
  • 1
  • 1
Paul Kehrer
  • 13,466
  • 4
  • 40
  • 57