RSA加密公钥?

时间:2011-08-31 11:00:41

标签: iphone ios4 cryptography rsa public-key-encryption

如何从iOS中的“模数”和“指数”创建RSA加密公钥。 我已经从钥匙串创建了公钥。是否可以从字符串'模数'和'指数'值?

1 个答案:

答案 0 :(得分:2)

在此处查看此答案

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

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

SCZ-BasicEncodingRules-IOS

实施基本编码规则以启用将RSA密钥导入iOS KeyChain使用指数。代码针对带有ARC的iOS 5。

假设您已经拥有模数和指数 RSA公钥作为名为pubKeyModData和变量的变量中的NSData pubKeyModData。然后,以下代码将创建包含该RSA的NSData 公钥,然后您可以将其插入iOS或OS X Keychain。

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

这将允许您使用Apple CryptoExercise示例中的SecKeyWrapper中的addPeerPublicKey: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);