How to use ECC in iOS

前端 未结 2 1308
误落风尘
误落风尘 2021-01-17 19:03

Is there any example for use ECC in iOS?

I noticed that the kSecAttrKeyTypeEC in Apple Developer Documents, but I can\'t use it to generic Key pair.

Below co

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-17 19:52

    NSDictionary *parameters = @{
                                 (__bridge id)kSecAttrKeyType: (__bridge id)kSecAttrKeyTypeEC,
                                 (__bridge id)kSecAttrKeySizeInBits: @256,
                                 (__bridge id)kSecPrivateKeyAttrs: @{
                                         (__bridge id)kSecAttrIsPermanent: @YES,
                                         (__bridge id)kSecAttrApplicationTag: [@"my.key.tag" dataUsingEncoding:NSUTF8StringEncoding],
                                         },
                                 (__bridge id)kSecPublicKeyAttrs: @{
                                         (__bridge id)kSecAttrIsPermanent: @YES,
                                         (__bridge id)kSecAttrApplicationTag: [@"my.key.pubtag" dataUsingEncoding:NSUTF8StringEncoding],
                                         }
                                 };
    
    SecKeyRef publicKey, privateKey;
    OSStatus status = SecKeyGeneratePair((__bridge CFDictionaryRef)parameters, &publicKey, &privateKey);
    

    This works, double check your key size parameter.

    Just a note, currently EC keys can only be used for signing/verifying data. Encryption/decryption returns errSecUnimplemented = -4.

提交回复
热议问题