2012-07-19 8 views
10

iOS에서 ECC를 사용하는 예가 있습니까?iOS에서 ECC를 사용하는 방법

Apple 개발자 문서의 kSecAttrKeyTypeEC에 주목했지만 일반 키 쌍에 사용할 수 없습니다. 코드 아래

는 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 항상 'errSecParam'을 의미 -50를 반환의 예에서 수정됩니다.

정말 사용 방법을 모르겠습니다. 읽어 주셔서 감사합니다.

+1

keysize = 256이지만 또 다른 문제는 내가 SecKeyRawSign을 사용할 때 -1을 반환하고 -1 반환 값에 대한 설명을 찾을 수없는 경우 코드가 전달 된 것입니다. 문제? – Cylon

+0

이 문제에 대한 해결책을 찾았습니까? – Gunhan

답변

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

이렇게하면 키 크기 매개 변수를 다시 확인하십시오.

현재 메모는 현재 EC 키는 데이터 서명/확인에만 사용할 수 있습니다. 암호화/암호 해독 반환 errSecUnimplemented = -4.

+0

[bugreport] (http://bugreport.apple.com)하시기 바랍니다. Apple은 iOS 용 암호화 기능을 제공하는 방법에 뒤떨어져 있습니다. – zaph

관련 문제