2012-04-08 22 views
6

Tiếp theo của Apple dụ mã trong: http://developer.apple.com/library/ios/#documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.htmlTạo cặp khóa trên iphone và in để đăng nhập như NSString

tôi có thể tạo ra thành công cặp khóa với đoạn mã dưới đây, nhưng không thể in các phím ...

Hàm SecKeyGeneratePair() - trả về các khóa như kiểu SecKeyRef.

Tôi không biết làm thế nào để xử lý loại này, tôi hiểu rằng đây là biểu diễn keychain nhưng làm thế nào tôi có thể thực sự xem cặp khóa như NSString ?? Cụ thể hơn, cách chuyển SecKeyRef thành 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

Không thể tạo ra các phím cặp ... Nó được đưa ra giá trị cho trạng thái 'OSStatus' ** - 34018 ** – Sujay

Trả lời

7

Bạn có thể sử dụng SecItemCopyMatching để lấy khóa NSData. Kiểm tra phương pháp getPublicKeyBits trong Apple's CryptoExercise, nó thực hiện chính xác những gì bạn cần.

Sau đó, bạn có thể chuyển đổi NSData thành chuỗi. Có lẽ, mã hóa Base64 sẽ đáp ứng nhu cầu của bạn. Here bạn có thể tìm thấy mẫu mã hóa/giải mã Base64 cho iPhone. Ngoài ra, điều này answer cũng có thể hữu ích cho việc mã hóa Base64.

+0

cảm ơn bạn, tôi sử dụng getPublicKeyBits và tôi làm eceive giá trị như NSData, có cái gì đó lạ mặc dù, khi tạo cặp khóa tôi chỉ định kSecAttrKeySizeInBits = 1024, nhưng kích thước NSData (nhận được từ getPublicKeyBits) là 140 byte (thay vì dự kiến ​​128) bất kỳ ý tưởng tại sao ?? –

+1

Điều này có thể do định dạng được sử dụng để lưu trữ khóa. Tôi không chắc lý do bạn đang in các phím. Thanh toán [this] (http://blog.wingsofhermes.org/?p=42) và [this] (http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios /) các liên kết thảo luận các cách để làm việc với các khóa iOS. – tenorsax

+0

Tôi có thể lấy NSData và tạo ra một sử dụng NSString: '[dữ liệu base64EncodedDataWithOptions: NSDataBase64Encoding64CharacterLineLength]; [[NSString alloc] initWithData: mã hóa base64Data: NSUTF8StringEncoding]; '. Nhưng không thể sử dụng NSString này để mã hóa. 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]); 
} 

Bạn có thể tìm thấy toàn bộ mã ở đây https://github.com/ozgurshn/EncryptionForiOS

2

Bạn có thể sử dụng 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 
} 

nhanh chóng 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

Nó không hoạt động với Swift 3. –

+0

đã thêm mã cho swift 3 – phnmnn

Các vấn đề liên quan