2012-04-08 6 views
6

としてログインするためにiphoneや印刷上の鍵ペアを生成しますは、アップルのサンプルコードに続いてNSStringの

機能SecKeyGeneratePair() - キーをSecKeyRefタイプとして返​​します。

このタイプの処理方法はわかりませんが、これはキーチェーン表現であることを理解していますが、どのようにキーペアを実際にNSStringとして見ることができますか? 具体的には、SecKeyRefをNSStringに変換する方法は?

static const UInt8 publicKeyIdentifier[] = "com.apple.sample.publickey\0"; 
static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey\0"; 
                  // 1 


- (void)generateKeyPairPlease 
{ 
    OSStatus status = noErr; 
    NSMutableDictionary *privateKeyAttr = [[NSMutableDictionary alloc] init]; 
    NSMutableDictionary *publicKeyAttr = [[NSMutableDictionary alloc] init]; 
    NSMutableDictionary *keyPairAttr = [[NSMutableDictionary alloc] init]; 
                   // 2 

    NSData * publicTag = [NSData dataWithBytes:publicKeyIdentifier 
           length:strlen((const char *)publicKeyIdentifier)]; 
    NSData * privateTag = [NSData dataWithBytes:privateKeyIdentifier 
           length:strlen((const char *)privateKeyIdentifier)]; 
                   // 3 

    SecKeyRef publicKey = NULL; 
    SecKeyRef privateKey = NULL;        // 4 

    [keyPairAttr setObject:(id)kSecAttrKeyTypeRSA 
            forKey:(id)kSecAttrKeyType]; // 5 
    [keyPairAttr setObject:[NSNumber numberWithInt:1024] 
          forKey:(id)kSecAttrKeySizeInBits]; // 6 

    [privateKeyAttr setObject:[NSNumber numberWithBool:YES] 
           forKey:(id)kSecAttrIsPermanent]; // 7 
    [privateKeyAttr setObject:privateTag 
          forKey:(id)kSecAttrApplicationTag]; // 8 

    [publicKeyAttr setObject:[NSNumber numberWithBool:YES] 
           forKey:(id)kSecAttrIsPermanent]; // 9 
    [publicKeyAttr setObject:publicTag 
          forKey:(id)kSecAttrApplicationTag]; // 10 

    [keyPairAttr setObject:privateKeyAttr 
           forKey:(id)kSecPrivateKeyAttrs]; // 11 
    [keyPairAttr setObject:publicKeyAttr 
           forKey:(id)kSecPublicKeyAttrs]; // 12 

    status = SecKeyGeneratePair((CFDictionaryRef)keyPairAttr, 
             &publicKey, &privateKey); // 13 
// error handling... 


    if(privateKeyAttr) [privateKeyAttr release]; 
    if(publicKeyAttr) [publicKeyAttr release]; 
    if(keyPairAttr) [keyPairAttr release]; 
    if(publicKey) CFRelease(publicKey); 
    if(privateKey) CFRelease(privateKey);      // 14 
} 
+0

ない鍵ペアを生成することができ...それは与えています'OSStatus'ステータスの値** - 34018 ** – Sujay

答えて

7

SecItemCopyMatchingを使用すると、キーのNSDataを取得できます。 Apple's CryptoExercisegetPublicKeyBitsメソッドをチェックし、必要なものを正確に実装します。

NSDataを文字列に変換できます。おそらく、Base64エンコーディングが必要になります。 Here iPhone用のBase64エンコード/デコードサンプルが見つかります。あるいは、このanswerは、エンコーディングのためにも有用かもしれない。

+0

ありがとうございます、私はgetPublicKeyBitsを使用しています私はkSecAttrKeySizeInBits = 1024を指定するが、getPublicKeyBitsから受け取ったNSDataサイズは140バイト(予想される128ではなく)です。 –

+1

これはおそらく、キーの格納に使用される形式のためです。私はあなたがキーを印刷している理由は何か分かりません。チェックアウト[this](http://blog.wingsofhermes.org/?p=42)と[this](http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios) /)リンクを使用して、iOSキーを操作する方法を説明します。 – tenorsax

+0

私はNSDataを取得し、NSStringを使用することができます: '[data base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength]; [[NSString alloc] initWithData:base64Dataエンコーディング:NSUTF8StringEncoding]; '。しかし、このNSStringを使って暗号化することはできません。 SOS –

0
-(void)writePublicKeyModAndExp 
{ 
    KeyHelper* keyHelper =[[KeyHelper alloc]init]; 
    NSData* pubkeyData= [keyHelper getPublicKeyBitsWithtag:publicTag]; 

    NSLog(@"pubKey :%@",[pubkeyData base64Encoding]); 

    NSData *modData= [keyHelper getPublicKeyModFromKeyData:pubkeyData]; 
    NSLog(@"modulus :%@",[modData base64Encoding]); 

    NSData *expoData= [keyHelper getPublicKeyExpFromKeyData:pubkeyData]; 
    NSLog(@"exponent :%@",[expoData base64Encoding]); 
} 

あなたはあなたが使用することができ、ここでhttps://github.com/ozgurshn/EncryptionForiOS

2

全体のコードを見つけることができますhttps://github.com/henrinormak/Heimdall

let localHeimdall = Heimdall(tagPrefix: "com.example") 

if let heimdall = localHeimdall { 
    let publicKeyData = heimdall.X509PublicKey() 
    var publicKeyString = publicKeyData.base64EncodedStringWithOptions(.allZeros) 

    // If you want to make this string URL safe, 
    // you have to remember to do the reverse on the other side later 
    publicKeyString = publicKeyString.stringByReplacingOccurrencesOfString("/", withString: "_") 
    publicKeyString = publicKeyString.stringByReplacingOccurrencesOfString("+", withString: "-") 

    println(publicKeyString) // Something along the lines of "MIGfMA0GCSqGSIb3DQEBAQUAA..." 

    // Data transmission of public key to the other party 
} 

SWIFT 3:

let localHeimdall = Heimdall(tagPrefix: "com.example") 
if let heimdall = localHeimdall, publicKeyData = heimdall.publicKeyDataX509() { 

    var publicKeyString = publicKeyData.base64EncodedString() 

    // If you want to make this string URL safe, 
    // you have to remember to do the reverse on the other side later 
    publicKeyString = publicKeyString.replacingOccurrences(of: "/", with: "_") 
    publicKeyString = publicKeyString.replacingOccurrences(of: "+", with: "-") 

    println(publicKeyString) // Something along the lines of "MIGfMA0GCSqGSIb3DQEBAQUAA..." 

    // Data transmission of public key to the other party 
} 
+0

まだSwift 3では動作しません。 –

+0

swift 3のコードを追加しました – phnmnn

関連する問題