美文网首页程序员
通过Python调用java中的Jar文件

通过Python调用java中的Jar文件

作者: Adiaoyc | 来源:发表于2020-05-10 15:04 被阅读0次

    一些特殊场景需要使用Python调用java,一下是对这个过程的简单的封装。

    1.准备java的jar文件

    其中的内容如下:

    package cn.diaoyc.aes;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    /**
     * @author Tony     
     * @projectName  AESCB
     * @title  AecCBCUtil   
     * @package  cn.diaoyc.aes   
     * @date 2020/5/10 -- 10:06
     * @version v1.1
     *  
     */
    public class AecCBCUtil {
        // 加密方式
        private static String ALGORITHM = "AES";
        //算法数据填充方式
        private static String ALGORITHM_FILL_TYPE = "AES/CBC/PKCS5Padding";
        //字符编码(String转byte[] 使用UTF-8的编码格式)
        private static String ENCODING_FORMAT = "UTF-8";
    
        // 内部实例参数
        private static AecCBCUtil instance = null;
    
        private AecCBCUtil() {
    
        }
    
        //采用单例模式,此静态方法供外部直接访问
        public static AecCBCUtil getInstance() {
            if (instance == null) {
                instance = new AecCBCUtil();
            }
            return instance;
        }
    
        /**
         * 加密
         * @param originalContent 明文
         * @param encryptKey 密钥
         * @param ivParameter 初始偏移量
         * @return 返回加密后的字符串
         */
        public String encrypt(String originalContent, String encryptKey, String ivParameter) {
            try {
                //处理传进来的明文
                byte[] originalContentBytes = originalContent.getBytes(ENCODING_FORMAT);
    
                //处理传进来的密钥(String转成byte[])
                byte[] enKeyBytes = encryptKey.getBytes();
    
                //处理传进来的偏移量(String转成byte[])
                byte[] ivParameterBytes = ivParameter.getBytes();
    
                //根据传入的密钥按照AEC方式构造密钥
                SecretKeySpec sKeySpec = new SecretKeySpec(enKeyBytes, ALGORITHM);
    
                //根据传入的偏移量指定一个初始化偏移量
                IvParameterSpec iv = new IvParameterSpec(ivParameterBytes);
                //根据数据填充方式生成一个加解密对象
                Cipher cipher = Cipher.getInstance(ALGORITHM_FILL_TYPE);
    
                //初始化 传入类型(加密/解密)、构造过的密钥、指定的初始偏移量
                cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
    
                //加密操作
                byte[] encrypted = cipher.doFinal(originalContentBytes);
    
                //base64转码
                String cipherString = new BASE64Encoder().encode(encrypted);
                return cipherString;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    
        }
    
        /**
         * 解密
         * @param cipherStr 加密过的密文
         * @param encryptKey 密钥
         * @param ivParameter 偏移量
         * @return 返回解密过后的字符串
         */
        public String decrypt(String cipherStr, String encryptKey, String ivParameter) {
            try {
                //处理传进来的密文 使用base64解密
                byte[] cipherStrByte = new BASE64Decoder().decodeBuffer(cipherStr);
    
                //处理传进来的密钥(String转成byte[])   可以指定编码格式为:ASCII
                byte[] enKeyBytes = encryptKey.getBytes();
    
                //处理传进来的偏移量(String转成byte[])
                byte[] ivParameterBytes = ivParameter.getBytes();
    
                //根据传入的密钥按照AEC方式构造密钥
                SecretKeySpec sKeySpec = new SecretKeySpec(enKeyBytes, ALGORITHM);
                Cipher cipher = Cipher.getInstance(ALGORITHM_FILL_TYPE);
                IvParameterSpec iv = new IvParameterSpec(ivParameterBytes);
                cipher.init(Cipher.DECRYPT_MODE, sKeySpec, iv);
    
                //获得解密的明文数组
                byte[] original = cipher.doFinal(cipherStrByte);
                return new String(original, ENCODING_FORMAT);
    
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    
        }
    }
    
    

    2.首先需要使用pip命令安装jpype

    pip install jpype
    

    3.新建Python文件导入jpype,代码如下

    import jpype
    import os
    from jpype import *
    
    
    class PyCallJava(object):
        def __init__(self, jPath, jClassName):
            self.jPath = jPath  # jar 路径
            self.jClassName = jClassName  # java类的全路径
    
        # 加载类
        def toCallJava(self):
            # 第二个参数是jar包的路径
            jarpath = os.path.join(os.path.abspath('.'), self.jPath)
            # 启动jvm
            jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % jarpath)
            # 类名
            jd_class = JClass(self.jClassName)
            return jd_class
    
        def closeJvm(self):
            # 关闭jvm
            jpype.shutdownJVM()
    
        # 使用类中的方法
        def test(self, param):
            jd_class = self.toCallJava()
            # 调用类中的方法
            # enString = jd_class.getInstance().encrypt(originalContent, encryptKey, ivParameter)
            enString = jd_class.getInstance().encrypt(param["originalContent"], param["encryptKey"], param["ivParameter"])
            print(enString)
            # 调用关闭方法
            self.closeJvm()
    
    
    if __name__ == '__main__':
    
        # 参数
        param = {"gdfgfa": "{\"fadsfads\":{\"fasdfds\":\"fasdfdsfa\",\"adsfdsa\":{\"adsf\":\"\",\"fadsfd\":\"\",\"fasdfd\":\"\",\"OSType\":\"\"},\"Name\":\"fadf\",\"afdf\":1,\"fadsfdsfdsa\":\"fasdfdsfa\"},\"P\":\"adminfafdsfadsmanager-123456\",\"U\":\"f-masdfdsfnager\"}", "fasdfds": "NV20fEAD55uEtWx5", "ivParameter": "IpaQo57JVO0GkU7s"}
        # jar路径
        Path = r'C:\Users\Tony\IdeaProjects\AESCB\target\AESCB-1.0-SNAPSHOT.jar'
        # 类的名称
        classname = "cn.diaoyc.aes.AecCBCUtil"
    
        PyCallJava(Path, classname,).test(param)
    
    

    相关文章

      网友评论

        本文标题:通过Python调用java中的Jar文件

        本文链接:https://www.haomeiwen.com/subject/yisjnhtx.html