java - 是否可以像在 Java 端那样使用模数和指数生成 RSA 私钥?

标签 java ios swift rsa private-key

我在 iOS 解决方案上使用 RSA 解密。我想使用在 Java 端使用的相同参数来创建 privateKey,但我找不到方法。 有没有办法做到这一点,或者是否可以使用 Java 导出此私钥,然后将其导入 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");

我的 iOS 应用程序将使用此算法解密 QRCode。 QRCode 通过如下公钥使用 Java 加密。为了解密这段代码,我们在 Java 端使用上面的代码。那么我们如何生成能够解密的相同私钥呢?如果没有相同的模数和指数,是否可以做到这一点?

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);

最佳答案

这不是它应该的工作方式。您可以在每一方生成一对私钥和公钥,并交换公钥 key 。双方将使用对方的公钥加密他们希望发送的消息(前提是它们不太长),并使用自己的私钥解密收到的消息。但是私钥是并且应该保持私有(private)

关于java - 是否可以像在 Java 端那样使用模数和指数生成 RSA 私钥?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54407219/

相关文章:

Java 正则表达式匹配之前和之后的所有内容

java - 为什么没有定义 Java 的 boolean 原始大小?

ios 快速播放从cloudkit上传的视频

ios - 为什么在 Swift 中写入 plist 会失败?

ios - 擦洗时跳动的 UISlider - 将 UISlider 与 AVPlayer 一起使用

ios - MMDrawerController 和约束

java - Java 中是否存在假数据生成器?

java - 使用 Java 和 XPath 提取网页内容

ios - 在iOS中设置动态UI

Swift Sprite 套件碰撞位掩码