简体   繁体   English

是否可以像Java一样使用模数和指数生成RSA私钥?

[英]is it possible to generate RSA privatekey using modulus and exponent like did on Java side?

I am using RSA decryption on an iOS solution. 我在iOS解决方案上使用RSA解密。 I want to use the same parameters used on Java side to create the privateKey, but i am not able to find how. 我想使用Java端使用的相同参数来创建privateKey,但是我找不到方法。 is there a way to do it or is it possible to export this privateKey using Java and then import it on the iOS solutions ? 有没有办法做到这一点,还是可以使用Java导出此privateKey,然后将其导入iOS解决方案中?

byte[] modulusBytes = Base64.decode("base64EncodedString");
byte[] DBytes = Base64.decode("anotherBase64EncodedString");
BigInteger modulus = new BigInteger(1, modulusBytes );
BigInteger exponent = new BigInteger(1, DBytes);

RSAPrivateKeySpec rsaPrivKey = new RSAPrivateKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privKey = fact.generatePrivate(rsaPrivKey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

My iOS application will decrypt a QRCode using this algorithme. 我的iOS应用程序将使用此算法解密QRCode。 the QRCode is encrypted using Java by a public key like below. QRCode使用Java通过如下所示的公钥加密。 to decrypt this code, we use the code above on Java side. 要解密此代码,我们在Java端使用上面的代码。 so how we can generate the same privatekey that will be able to decrypt ? 那么我们如何生成可以解密的相同私钥? is it possible to do it without the same modulus and exponent ? 是否可以在没有相同模数和指数的情况下做到这一点?

byte[] modulusBytes = Base64.decode("base64EncodedString");

byte[] exponentBytes = Base64.decode("AQAB");
BigInteger modulus = new BigInteger(1, modulusBytes );
BigInteger exponent = new BigInteger(1, exponentBytes);

RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(rsaPubKey);

This is not the way it is supposed to work. 这不是应该的工作方式。 You could generate a pair of private and public keys on each side, and exchange the public keys. 您可以生成每边一对私钥和公钥,并交换公共密钥。 The each side would encrypt the messages they wish to send (provided they are not too long) with the other side's public key, and decrypt the received messages with their own private key. 双方将使用对方的公钥对希望发送的消息进行加密(前提是它们不太长),并使用自己的私钥对接收到的消息进行解密。 But the private key is and should remain just that, private . 但是私钥是并且应该保持为私钥

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

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