I would like to decrypt a RSA-encoded blob on iPhone, by having an exponent and modulus as private key. In Java (with javax.crypto), this could be easily achieved by code like this:
// 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
But with the iPhone security API I can’t create a SecKeyRef (key) other than by generating a pair or importing a certificate, which I don’t have/want.
Is there a way to create a key manually having a modulus + exponent? If so, can you give me a clue on how?
Thanks in advance
How are your exponent and modulus encoded? If they’re in a PKCS#12 blob, you can use
SecPKCS12Import()andSecIdentityCopyPrivateKey()to achieve what you want.EDIT: Given that you have the raw keys, you might be interested in looking at the
-[SecKeyWrapper addPeerPublicKey:keyBits:]example provided by Apple.