具有模数和指数的iPhone RSA算法 [英] iPhone RSA algorithm with modulus and exponent

查看:151
本文介绍了具有模数和指数的iPhone RSA算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有模数和指数。如何在iPhone上使用RSA algotithm对数据进行编码/解码?或者如何从模数和指数生成DER格式?

I have modulus and exponent. How can I encode/decode data with RSA algotithm on iPhone? Or how can I generate DER format from modulus and exponent?

推荐答案

如果你想在iPhone上生成DER数据,我有GitHub上的代码可以让你这样做:

If you want to generate DER data on the iPhone, I have code on GitHub that will let you do this:

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

实现基本编码规则,以便使用exponent将RSA密钥导入iOS
KeyChain。代码针对带有ARC的iOS 5。

Implementation of Basic Encoding Rules to enable import of RSA keys to iOS KeyChain using exponent. Code targets iOS 5 with ARC.

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

Let's say you already have a modulus and exponent from an RSA public key as an NSData in variables named pubKeyModData and pubKeyModData. Then the following code will create an NSData containing that RSA public key, which you can then insert into the iOS or 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()。

This would allow you to store the key using the addPeerPublicKey:keyBits: method from SecKeyWrapper in the Apple CryptoExercise example. Or, from the perspective of the low-level API, you can use 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);

这篇关于具有模数和指数的iPhone RSA算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆