1、由于公司需要,必须要在Java上运行些东西,以前看过Java但都差不多忘完了,所以安装一下Java记录一下,Java安装在C://Java
文件夹里面的。配置的环境变量如下:
2、将Java生成为Jar包在转为rsa.exe: (需要注意rsa.exe的jdk版本,服务器上配置的时候,一定要与生成的jdk版本保持一致)
Java使用私钥文件对数据进行rsa解密 Java程序//将私钥文件->私钥字符串public static RSAPrivateKey getPrivateKey(String keyPath, String passwd) throws Exception { try { KeyStore ks = KeyStore.getInstance("PKCS12"); FileInputStream fis = new FileInputStream(keyPath); char[] nPassword = null; if ((passwd == null) || passwd.trim().equals("")) { nPassword = null; } else { nPassword = passwd.toCharArray(); } ks.load(fis, nPassword); fis.close(); Enumeration enumq = ks.aliases(); String keyAlias = null; if (enumq.hasMoreElements()) { keyAlias = (String) enumq.nextElement(); } PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword); return (RSAPrivateKey) prikey; } catch (Exception e) { e.printStackTrace(); return null; }}//私钥解密public static String decrypt(String sign_msg, String pfx_path, String pfx_pass) { try { RSAPrivateKey pbk = getPrivateKey(pfx_path, pfx_pass); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); cipher.init(Cipher.DECRYPT_MODE, pbk); byte[] btSrc = cipher.doFinal(Base64.decode(sign_msg)); return new String(btSrc,ENCODING); } catch (Exception e) { e.printStackTrace(); return ""; }}//执行Main函数public static void main(String[] args) throws Exception { //解密密钥 String key_3des_enc = args[0]; String pfx_path = args[1]; String pfx_pass = args[2]; String key_3des = RSA.decrypt(key_3des_enc,pfx_path,pfx_pass); System.out.println(key_3des);}
C#程序
////// 使用私钥解密出一个Java生成的随机密码,此处一般是3DES的加密密码,Base64字符串/// /// 要解密的字符串/// 证书路径/// 证书密码///public static string DecryptRandKeyByPrivateKey(string encryptString,string pfxPath,string password){ System.Diagnostics.Process p = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo("rsa.exe"); ps.Arguments = string.Format("{0} {1} {2}", encryptString, pfxPath, password); ps.RedirectStandardOutput = true; ps.UseShellExecute = false; p.StartInfo = ps; p.Start(); return p.StandardOutput.ReadToEnd();}