iOS - 从指数+模数创建SecKeyRef

Has*_*shi 12 security iphone rsa ios

我想通过将指数和模数作为私钥来解密iPhone上的RSA编码的blob.在Java(使用javax.crypto)中,这可以通过以下代码轻松实现:

// 1) key
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(myModulus, myPublicExponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
Key pubKey = fact.generatePublic(keySpec);

// 2) cypher
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec);

// 3) use cypher to decode my block to an output stream
Run Code Online (Sandbox Code Playgroud)

但是使用iPhone安全API我不能创建SecKeyRef(密钥),除了生成一对或导入证书,我没有/想要.

有没有办法手动创建一个具有模数+指数的密钥?如果是这样,你能告诉我一个怎样的线索吗?

提前致谢

Sed*_*ien 1

您的指数和模数是如何编码的?如果它们位于PKCS#12 blob 中,您可以使用SecPKCS12Import()SecIdentityCopyPrivateKey()来实现您想要的目的。

编辑:鉴于您拥有原始密钥,您可能有兴趣查看Apple提供的-[SecKeyWrapper addPeerPublicKey:keyBits:]示例。

  • @SedateAlien 我还是有点困惑。就我而言,这是一个公钥,指数和模量以这种形式出现:`<rsakeyvalue> <modulus> yote0l1/ncbxdzydlis82mite82mite8vd5wd23s4rdsebjofzofzfzb4d </endent> </rsakeyvalue >` 我如何从中获取公钥? (2认同)