Porting .Net RSA xml keys to Java详解编程语言

byte[] expBytes = Base64.decodeBase64(exponentElem.getText().trim())); 
byte[] modBytes = Base64.decodeBase64(modulusElem.getText().trim()); 
byte[] dBytes = Base64.decodeBase64(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));

 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/15842.html

(0)
上一篇 2021年7月19日 18:46
下一篇 2021年7月19日 18:46

相关推荐

发表回复

登录后才能评论