2010-02-24 97 views
3

我一直在Google上搜索並研究如何在iPhone上使用Cbjective-C進行簡單的RSA加密。我的主要問題是我已經提供了指數和模數作爲NSData對象,然後我需要將它們轉換爲SecKeyRef對象才能執行RSA加密。iPhone/Objective-c RSA加密

有沒有人有任何想法如何做到這一點或有任何有用的提示?

非常感謝!

回答

2

我最終在我的項目中使用了OpenSSL。並導入密鑰並使用該庫而不是iPhone進行加密。

0

我實現了這個解決方案,並把它放在GitHub上。現在,您可以將Apple授權的API用於Keychain而不是OpenSSL。 (他們勸阻的OpenSSL在他們的文學,很可惜。)

https://github.com/StCredZero/SCZ-BasicEncodingRules-iOS

SCZ-BasicEncodingRules-IOS

基本編碼規則

實施,使利用指數RSA密鑰到iOS 鑰匙鏈的導入。代碼以ARC爲目標iOS 5。

假設您已經有一個模數和指數,從 一個RSA公共密鑰作爲名爲pubKeyModData和 pubKeyModData的變量中的NSData。然後,下面的代碼將創建一個NSData,其中包含RSA 公鑰,然後您可以將其插入到iOS或OS X鑰匙串中。

NSMutableArray *testArray = [[NSMutableArray alloc] init]; 
[testArray addObject:pubKeyModData]; 
[testArray addObject:pubKeyExpData]; 
NSData *testPubKey = [testArray berData]; 

這將允許你存儲使用addPeerPublicKey的關鍵在於:從SecKeyWrapper在蘋果CryptoExercise實例方法:keyBits。或者,從低級API的角度來看,您可以使用SecItemAdd()。

NSString * peerName = @"Test Public Key"; 

NSData * peerTag = 
    [[NSData alloc] 
     initWithBytes:(const void *)[peerName UTF8String] 
     length:[peerName length]]; 

NSMutableDictionary * peerPublicKeyAttr = [[NSMutableDictionary alloc] init]; 

[peerPublicKeyAttr 
    setObject:(__bridge id)kSecClassKey 
    forKey:(__bridge id)kSecClass]; 
[peerPublicKeyAttr 
    setObject:(__bridge id)kSecAttrKeyTypeRSA 
    forKey:(__bridge id)kSecAttrKeyType]; 
[peerPublicKeyAttr 
    setObject:peerTag 
    forKey:(__bridge id)kSecAttrApplicationTag]; 
[peerPublicKeyAttr 
    setObject:testPubKey 
    forKey:(__bridge id)kSecValueData]; 
[peerPublicKeyAttr 
    setObject:[NSNumber numberWithBool:YES] 
    forKey:(__bridge id)kSecReturnPersistentRef]; 

sanityCheck = SecItemAdd((__bridge CFDictionaryRef) peerPublicKeyAttr, (CFTypeRef *)&persistPeer);