使用 Java 进行 RSA 解密 [英] RSA decrypt with Java

查看:54
本文介绍了使用 Java 进行 RSA 解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RSA 解密字符串.它在 iPhone 上用 C# 加密,我有私钥.这似乎是一个愚蠢的问题,但我看到的所有示例都显示了生成私钥.我有私钥(它是一个字节 [] 的十六进制).它使用 PKCS#1 填充.我不知道该怎么做的部分是使用我已经拥有的私钥创建一个 java.security.Key 对象.

I am trying to decrypt a string with RSA. It was encrypted in C# on the iPhone and I have the private key. This seems like a silly problem, but all of the examples I have seen show generating the private key. I have the private key (it is a byte[] of hex). It using PKCS#1 padding. The part I cannot figure out how to do is create a java.security.Key object with the private key I already have.

我需要让他们把私钥分成两部分...模数和指数吗?

Do I need to have them give me the private key in 2 parts...modulus and exponent?

提前致谢.

推荐答案

您需要通过 RSAPrivateKeySpec.这是一个示例(基于this):

You need to go through a RSAPrivateKeySpec. Here's an example (based on this):

        BigInteger n = new BigInteger(nBytes);
        BigInteger p = new BigInteger(pBytes);
        RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(n, p);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        Key privateKey = kf.generatePrivate(privateSpec);

这篇关于使用 Java 进行 RSA 解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆