What is the padding type for Elliptic Curve Crypto

2019-07-15 17:52发布

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;
}

1条回答
走好不送
2楼-- · 2019-07-15 17:58

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?

查看更多
登录 后发表回答