在ECC SecKeyGeneratePair
之后,我尝试通过公钥加密明文。 SecKeyEncrypt
返回-4(errSecUnimplemented)。我不确定填充类型是否正确。我在我的xcode中尝试了所有类型,它们也不能正常工作。有人可以解释为什么SecKeyEncrypt
会返回-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;
}
答案 0 :(得分:2)
Apple的实施仅支持ECDSA,可用于签名但不能加密。有关使用其他方案使用椭圆曲线加密的信息,请参阅Is it possible to use elliptic curve cryptography for encrypting data?