2011-08-31 192 views
1

如何从iOS中的'Modulus'和'Exponent'创建RSA加密公钥? 我从钥匙串创建了公钥。是否可以从字符串'Modulus'和'Exponent'的值?RSA加密公钥?

回答

2

看到这个答案在这里

https://stackoverflow.com/a/10643894/584616

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); 
+0

什么是输入NSData对象pubKeyModData和pubKeyExpData的格式?他们应该是base64 – prodos

+0

编码格式是NSUTF8StringEncoding – Luke