将.Net RSA xml密钥移植到Java

将.Net RSA xml密钥移植到Java,第1张

将.Net RSA xml密钥移植到Java

deprer
在您的示例中,这是进行base64解码吗?看起来您可能正在依赖
sun.misc.base64Deprer
它,并且依赖那些内部类通常不是一个好主意(例如,其他JVM不会拥有它)。您可以使用具有base64类进行解码的Apache
Commons Codec
。这是RSA加密和解密所需要的其余内容:

byte[] expBytes = base64.deprebase64(exponentElem.getText().trim()));byte[] modBytes = base64.deprebase64(modulusElem.getText().trim());byte[] dBytes = base64.deprebase64(dElem.getText().trim());BigInteger modules = new BigInteger(1, modBytes);BigInteger exponent = new BigInteger(1, expBytes);BigInteger d = new BigInteger(1, dBytes);KeyFactory factory = KeyFactory.getInstance("RSA");Cipher cipher = Cipher.getInstance("RSA");String input = "test";RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponent);PublicKey pubKey = factory.generatePublic(pubSpec);cipher.init(Cipher.ENCRYPT_MODE, pubKey);byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));System.out.println("encrypted: " + new String(encrypted));RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);PrivateKey privKey = factory.generatePrivate(privSpec);cipher.init(Cipher.DECRYPT_MODE, privKey);byte[] decrypted = cipher.doFinal(encrypted);System.out.println("decrypted: " + new String(decrypted));


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5488698.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存