zoukankan      html  css  js  c++  java
  • Java记录

    1、由于公司需要,必须要在Java上运行些东西,以前看过Java但都差不多忘完了,所以安装一下Java记录一下,Java安装在C://Java文件夹里面的。配置的环境变量如下:
    首先 下载JDK
    然后如下图所示进行配置即可


    成功后如下所示

    2、将Java生成为Jar包在转为rsa.exe: (需要注意rsa.exe的jdk版本,服务器上配置的时候,一定要与生成的jdk版本保持一致)
    jar->exe 博客文章
    下载jar2exe
    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#程序

    /// <summary>
    /// 使用私钥解密出一个Java生成的随机密码,此处一般是3DES的加密密码,Base64字符串
    /// </summary>
    /// <param name="encryptString">要解密的字符串</param>
    /// <param name="pfxPath">证书路径</param>
    /// <param name="password">证书密码</param>
    /// <returns></returns>
    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();
    }
    
  • 相关阅读:
    潜入ICU的四逆汤
    经方医的视角
    黄连解毒汤治疗月经过多
    柳暗花明又一方
    PHP 相关性系数计算
    备忘-VSCODE、apache配置
    c# 基于文件系统实现的队列处理类
    notepad++ 快速运行PHP代码
    dat.gui stats.js 通用参数配置及图像统计工具
    AutoHotkey 自动化脚本工具实例
  • 原文地址:https://www.cnblogs.com/xuxuzhaozhao/p/6702522.html
Copyright © 2011-2022 走看看