ios - 如何在 iOS 中使用 ECC

标签 ios elliptic-curve

有没有在iOS中使用ECC的例子?

我注意到 Apple Developer Documents 中的 kSecAttrKeyTypeEC,但我不能将它用于通用 key 对。

以下代码修改自示例 CryptoExercise

// Container dictionaries.
NSMutableDictionary * privateKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary * publicKeyAttr = [[NSMutableDictionary alloc] init];
NSMutableDictionary * keyPairAttr = [[NSMutableDictionary alloc] init];

// Set top level dictionary for the keypair.
[keyPairAttr setObject:(id)kSecAttrKeyTypeEC forKey:(id)kSecAttrKeyType];
[keyPairAttr setObject:[NSNumber numberWithUnsignedInteger:keySize] forKey:(id)kSecAttrKeySizeInBits];

// Set the private key dictionary.
[privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecAttrIsPermanent];
[privateKeyAttr setObject:privateTag forKey:(id)kSecAttrApplicationTag];
// See SecKey.h to set other flag values.

// Set the public key dictionary.
[publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecAttrIsPermanent];
[publicKeyAttr setObject:publicTag forKey:(id)kSecAttrApplicationTag];
// See SecKey.h to set other flag values.

// Set attributes to top level dictionary.
[keyPairAttr setObject:privateKeyAttr forKey:(id)kSecPrivateKeyAttrs];
[keyPairAttr setObject:publicKeyAttr forKey:(id)kSecPublicKeyAttrs];

// SecKeyGeneratePair returns the SecKeyRefs just for educational purposes.
sanityCheck = SecKeyGeneratePair((CFDictionaryRef)keyPairAttr, &publicKeyRef, &privateKeyRef);
LOGGING_FACILITY( sanityCheck == noErr && publicKeyRef != NULL && privateKeyRef != NULL, @"Something really bad went wrong with generating the key pair." );

sanityCheck 始终返回 -50,这意味着“errSecParam”。

我真的不知道怎么用,谢谢你看完。

最佳答案

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

这有效,请仔细检查您的 key 大小参数。

请注意,目前 EC key 只能用于签署/验证数据。加密/解密返回errSecUnimplemented = -4.

关于ios - 如何在 iOS 中使用 ECC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11563229/

相关文章:

iphone - 当 alertview 显示时不要连接 segue

ios - 在设备上测试时,PhoneGap 应用程序上未找到 Index.html

ios - 使用 Parse 的 undefined symbol

c - 如何使用 OpenSSL 从 wolfSSL 验证 ECC 签名

java - 无法使用椭圆曲线 (EC) 加密技术签署 JWT

android - 根据情况显示不同的启动画面

ios - 在 iOS 中上推和交换导航栏

php - 无法使用 javascript 桥接椭圆曲线 Diffie-Hellman

.net-core - 为什么Curve25519即使参数错误也能正确计算 key 对?

python - Charm Crypto 中椭圆曲线上的恒等元素是什么?