简体   繁体   English

在iOS中创建RSA私钥

[英]Creating an RSA private key in iOS

I'm trying to rewrite some Java (Android) code in ObjC on the iPhone. 我正在尝试在iPhone的ObjC中重写一些Java(Android)代码。 The code will do a basic web service call and needs to set some headers with authentication information. 该代码将进行基本的Web服务调用,并且需要设置一些带有身份验证信息的标头。

One part of that information is an encrypted hash of the data I am sending over. 该信息的一部分是我正在发送的数据的加密哈希。

The Java version calculates an SHA256 signature using an RSA private key that is generated on the phone. Java版本使用手机上生成的RSA私钥来计算SHA256签名。 The private key is generated using a seed that I have available. 私钥是使用我可用的种子生成的。

The (simplified) java code is as follows: (简化的)Java代码如下:

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Signature sig = Signature.getInstance("SHA256WithRSAEncryption");

// I get the private key bytes from an outside source
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
sig.initSign(keyFactory.generatePrivate(privateKeySpec));

sig.update(/* insert my data here */);
return sig.sign();

Now I'm trying to recreate this in iOS and ObjC. 现在,我试图在iOS和ObjC中重新创建它。 Doing the SHA256 signature calculation is easy, but I don't see how to create a private RSA key easily. 进行SHA256签名计算很容易,但是我看不出如何轻松创建私有RSA密钥。 I would prefer to use the built-in API's if there are any available, but if I must use a third party library like OpenSSL then I can live with that as well. 如果有可用的API,我宁愿使用内置的API,但是如果我必须使用第三方库(例如OpenSSL),那么我也可以使用它。

Most people (citation needed) elect to go with the third party OpenSSL library, not only because rolling your own crypto is hard, but also because their is a good chance you'll create bad crypto if you're not already experienced with it. 大多数人(需要引用)选择使用第三方OpenSSL库,这不仅是因为难以编写自己的加密货币,而且还因为他们有很大的机会(如果您还没有使用过加密货币的话)会创建不良的加密货币。

That said, nothing prevents you from writing your own SHA256 hash, in straight C or C++ if you like, although I think you'll find your PRNG options lacking and find yourself spending altogether way too much time on entropy pools and the like. 就是说,没有什么可以阻止您根据自己的喜好用C或C ++直接编写SHA256哈希的,尽管我认为您会发现PRN​​G选项缺乏,并且发现自己在熵池等上花费了太多时间。

If you do come across a good SHA256 primitive without all the extra baggage of OpenSSL, I'd love to learn about it too! 如果您确实遇到了一个不错的SHA256原语而又没有OpenSSL的额外负担,我也很想了解它! But so far I haven't seen one. 但是到目前为止,我还没有看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM