Java RSA - 使用模数字符串和公共指数字符串构造公钥

时间:2017-06-07 10:30:51

标签: java encryption rsa

我有这段代码:

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(keySize);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
System.out.println("publicKey:" + publicKey);

运行的输出:

publicKey:Sun RSA public key, 1024 bits
    modulus: 91026765002706288867763671707890256444556491085864843097606069886752641850958968402542910033341226293747620290679011468803812135644603615727909010737368199182137517524592490628443269825314162441969081210450684647396205652070736268541584184960515476448786428782291670074320598962297519615175082972099164486927
    public exponent: 65537

现在我想构建解密密钥,如下所示:

RSAPublicKeySpec publicSpec = new RSAPublicKeySpec(new BigInteger(modulusStr, 16), new BigInteger(publicExponentStr, 16));
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey pupKey = factory.generatePublic(publicSpec);

System.out.println("publicKey:" + pupKey);

但输出结果不同:

publicKey:Sun RSA public key, 1232 bits


     modulus: 43045393507044718977987783526170006227729098917076727528691078730555395085202306732993768634230789938109087426142463574995910150627330334608775595737083465581426001237091137474842776120780697084545110477342521432568680506316814949888900358728179483490979805640521767456573703418040730841096209231624036816985022810174297855365613680959673590517874895661273111176382128521
     public exponent: 415031

我不应该得到相同的输出吗?

0 个答案:

没有答案