RSA decrypt with Java

别等时光非礼了梦想. 提交于 2020-01-15 10:18:06

问题


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?

Thanks in advance.


回答1:


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


来源:https://stackoverflow.com/questions/1236481/rsa-decrypt-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!