iPhone/Objective-c RSA 加密

发布于 2024-08-23 00:56:36 字数 179 浏览 3 评论 0原文

我一直在 Google 搜索并研究如何在 iPhone 上使用 Cbjective-C 进行简单的 RSA 加密的答案。我遇到的主要问题是,我已将指数和模数作为 NSData 对象提供,然后我需要将它们转换为 SecKeyRef 对象才能执行 RSA加密。

有谁知道如何做到这一点或有任何有用的提示?

非常感谢!

I have been Google-ing and researching for an answer on how to do a simple RSA encryption using Cbjective-C on an iPhone. The main problem I have is that I have been supplied the Exponent and Modulus as an NSData object and i need to then convert them to a SecKeyRef object in order to perform the RSA encryption.

Does anyone have any idea how to do that or have any useful hints?

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏尔 2024-08-30 00:56:36

我最终在我的项目中使用了 OpenSSL。导入密钥并使用该库(而不是 iPhone 的库)进行加密。

I ended up using OpenSSL in my project. and import the keys and encrypt using that library instead of the iPhone ones.

辞取 2024-08-30 00:56:36

我对此实现了一个解决方案并将其放在 GitHub 上。现在,您可以使用 Apple 批准的 Keychain API,而不是 OpenSSL。 (不幸的是,他们在文献中不鼓励使用 OpenSSL。)

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

SCZ-BasicEncodingRules-iOS

实现基本编码规则以支持将 RSA 密钥导入到 iOS
使用指数的钥匙串。代码针对带有 ARC 的 iOS 5。

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

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

I implemented a solution to this and put it on GitHub. Now you can use the Apple-sanctioned APIs for the Keychain instead of OpenSSL. (They discourage OpenSSL in their literature, unfortunately.)

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

SCZ-BasicEncodingRules-iOS

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

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];

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文