将 XML Dsig 格式转换为 DER ASN.1 公钥
我正在开发一个 iPhone 应用程序,它从 ASP.NET Web 服务中检索 RSA 公钥,格式如下:
<RSAKeyValue>
<Modulus>qdd0paiiBJ+xYaN4TKDdbEzrJJw9xlbRAltb5OPdegjLoW60yOjL/sni52WVsGC9QxpNitZR33dnUscmI0cTJoxkXypPjbD94UpH+p4el2tuKBypHlE7bERApuUp55y8BiRkbQNFH8smZFWDwtIc/PsJryeGf8fAryel8c5V3PU=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
然后我需要将此响应转换为适当格式的 NSData *
(来自一些密集的格式)谷歌搜索,很可能是“ASN.1 DER”二进制格式,我已经有了将这两个部分从 Base64 表示形式转换为原始二进制值的代码,但我一生都找不到合理的方法来转换。创建一件式二进制密钥。
等待密钥的代码是 Apple 的 SecKeyWrapper
类的 -addPeerPublicKey:(NSString *) keyBits:(NSData *)
方法。 CryptoExercise
示例项目(代码此处< /a>)。
我非常乐意以另一种方式实现这一点——我所需要的只是加密单个字符串(不需要解密)。不过,据我所知,内置的安全框架具有什么功能。我需要,如果我能缩小这种格式差距的话。如果有一种方法可以转换密钥并从 Web 服务以 Base64 编码发送它,这对我来说也有效,但我也找不到任何方法对其进行 ASN.1 编码。
I am working on an iPhone app that retrieves an RSA public key from an ASP.NET web service in the form:
<RSAKeyValue>
<Modulus>qdd0paiiBJ+xYaN4TKDdbEzrJJw9xlbRAltb5OPdegjLoW60yOjL/sni52WVsGC9QxpNitZR33dnUscmI0cTJoxkXypPjbD94UpH+p4el2tuKBypHlE7bERApuUp55y8BiRkbQNFH8smZFWDwtIc/PsJryeGf8fAryel8c5V3PU=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
I need to then convert this response into an NSData *
of the appropriate format (from some intense Googling, most likely 'ASN.1 DER' binary format. I've got code in place to convert both parts from their Base64 representations to the original binary values, but I can't for the life of me figure out a reasonable way to create the one-piece binary key.
The code waiting for the key is the -addPeerPublicKey:(NSString *) keyBits:(NSData *)
method of the SecKeyWrapper
class from Apple's CryptoExercise
example project (Code here).
I would be more than happy to implement this another way--all I need is to encrypt a single string (no decryption required). As far as I can tell, though, the built-in Security framework has what I need, if I could just close this format gap. If there is a way to convert the key and send it Base64-encoded from the webservice, that works for me as well--but I couldn't find any way to ASN.1-encode it there, either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我使用 SecKeyWrapper 类生成随机密钥,然后使用 -getPublicKeyBits 方法获取公钥的二进制表示形式(以内部使用的任何格式) 。假设它是 DER ASN.1 的某种形式,我将其作为十六进制 NSLog'd 到控制台并将其加载到 此程序中。果然,内部表示是 DER ASN.1,但它是我通常发现的 RSA 密钥表示的一个非常简化的版本:
从二进制表示即时构建应该不会太困难。模数和指数,因为 DER 编码只是
为了简单起见,这是我的代码。它使用了一些用于 XML+base64 的 Google 库,请注意;还有Apple的演示代码SecKeyWrapper。有关实现此功能的说明,请参阅我的其他问题。另请注意,它不兼容 ARC;这是留给读者的练习(我几年前写的,现在)。
So, I used the
SecKeyWrapper
class to generate a random key, then used the-getPublicKeyBits
method to get the binary representation of the public key (in whatever format is used internally). Presuming it is some form of DER ASN.1, I NSLog'd it to the console as hex and loaded it into this program. Sure enough, the internal representation is DER ASN.1, but it is a very simplified version of what I normally found for RSA key representations:Shouldn't be too tough to construct on the fly from a binary rep. of the modulus and exponent, since the DER encoding is just
Here's my code, for simplicity. It uses a few Google libs for XML+base64, just heads up; also Apple's demo code SecKeyWrapper. See my other question for a note on making this work. Also, note that it is not ARC-compatible; this is left as an exercise for the reader (I wrote this years ago, now).