2012-04-08 11 views
6

के रूप में लॉग इन करने के iphone और प्रिंट पर कुंजी युग्म उत्पन्न करें: http://developer.apple.com/library/ios/#documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.htmlएप्पल उदाहरण कोड में बाद NSString

मैं कुंजी मुद्रित करने के लिए सफलतापूर्वक नीचे कोड स्निपेट के साथ कुंजी युग्म उत्पन्न करने में सक्षम है, लेकिन में असमर्थ हूँ ...

फ़ंक्शन SecKeyGeneratePair() - चाबियाँ SecKeyRef प्रकार के रूप में लौटाती है।

मुझे नहीं पता कि इस प्रकार को कैसे संभालना है, मैं समझता हूं कि यह चाबी का प्रतिनिधित्व है लेकिन मैं वास्तव में कुंजी-जोड़ी को एनएसएसटींग के रूप में कैसे देख सकता हूं ?? अधिक विशेष रूप से, 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

आप कुंजी के NSData प्राप्त करने के लिए SecItemCopyMatching का उपयोग कर सकते हैं। में getPublicKeyBits विधि की जांच करें, यह आपको वही आवश्यक बनाता है जो आपको चाहिए।

फिर आप स्ट्रिंग में NSData को परिवर्तित कर सकते हैं। शायद, Base64 एन्कोडिंग आपकी आवश्यकताओं को सुइट करेगा। Here आप आईफोन के लिए Base64 एन्कोडिंग/डिकोडिंग नमूना पा सकते हैं। वैकल्पिक रूप से, यह answerBase64 एन्कोडिंग के लिए भी उपयोगी हो सकता है।

+0

धन्यवाद, मैं getPublicKeyBits का उपयोग करता हूं और मैं करता हूं एनएसडीटा के रूप में मूल्य को प्राप्त करें, हालांकि कुछ अजीब बात है, जब कीपैयर बनाते समय मैं kSecAttrKeySizeInBits = 1024 निर्दिष्ट करता हूं, लेकिन NSData आकार (GetPublicKeyBits से प्राप्त) 140 बाइट्स (अपेक्षित 128 की बजाय) किसी भी विचार क्यों है ?? –

+1

शायद यह उस प्रारूप के कारण है जिसका उपयोग कुंजी को स्टोर करने के लिए किया जाता है। मुझे यकीन नहीं है कि आप कुंजी प्रिंट क्यों कर रहे हैं। चेकआउट [यह] (http://blog.wingsofhermes.org/?p=42) और [यह] (http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios /) लिंक जो आईओएस कुंजी के साथ काम करने के तरीकों पर चर्चा करते हैं। – tenorsax

+0

मैं एनएसडीटा प्राप्त कर सकता हूं और एनएसएसटींग उपयोग उत्पन्न कर सकता हूं: '[डेटा बेस 64 एन्कोडेडडेटाविथऑप्शन: एनएसडीटाबेसबेस 64 एनकोडिंग 64 कैरेक्टरलाइन लम्बाई]; [[एनएसएसटींग आवंटन] initWithData: base64 डेटा एन्कोडिंग: NSUTF8StringEncoding]; '। लेकिन एन्क्रिप्ट करने के लिए इस एनएसएसटींग का उपयोग नहीं कर सकते हैं। एसओएस –

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 
} 

तेज 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

यह अभी तक स्विफ्ट 3 के साथ काम नहीं करता है। –

+0

तेजी से 3 के लिए कोड जोड़ा गया – phnmnn