在python中引⽤jar包在本⽂中,⾸先创建⼀个maven项⽬,然后打包成jar包,在python中调⽤jar包,执⾏java类⽅法。
1.在eclipse中创建maven项⽬
我这⾥创建⼀个普通java项⽬,择选择 “maven-archtype-quickstart” 即可
2.创建后,在项⽬根⽬录下会有⼀个l
3.编写pom⽂件,加载项⽬所需要的依赖,我的l 如下:
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rfchina</groupId>
<artifactId>utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>utils</name>
<url></url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
</dependencies>
<!--  maven打包构建时,使⽤参数 assembly:assembly 则会加载依赖包  -->
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<!-- 我运⾏这个jar所运⾏的主类 -->
<mainClass>com.rfchina.utils.RsaSign</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>
<!-- 必须是这样写 -->
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.编写java代码前,这⾥我们先安装⼀下依赖包:
运⾏后,可以在Maven Dependencies中看到已经下载并且引⼊的maven依赖包
5.开始编写java代码
Base64Utils.java
package com.rfchina.utils;
import dec.binary.Base64;
public class Base64Utils
{
/**
* 使⽤Base64加密字符串
* @return加密之后的字符串
* @exception Exception
*/
public static String encode(byte[] data){
Base64 base64 = new Base64();
String encodedData = deAsString(data);
return encodedData;
}
/
**
python转java代码* 使⽤Base64解密
* @return解密之后的字符串
* @exception Exception
*/
public static byte[] decode(String data){
Base64 base64 = new Base64();
byte[] decodedData = base64.decodeBase64(data);
return decodedData;
}
}
RsaSign.java
package com.rfchina.utils;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
/**
* @ClassName: RsaSign
* @Description: RSA 私钥签名
* @author Milton
* @date 2018年9⽉4⽇
*
*/
public class RsaSign {
private static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
private static final String KEYPAIR_GENERATOR_ALGORITHM = "RSA";
// 商户私钥加密接⼝请求参数
final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMJSignj27blOez6i6YCM1b4AC9CtgubD0B9gmTskENJNxzg9i59hCpIibkTg1rvMDdSuKFvlHtxkOgSID8Qgm1h3AxpMKpYT58FnApqY7img85xEZo+uN public String rsa_sign(String encryData) {
//        String encryData_example="app_id=12996762&app_secret=902ac71d8fdbdfc097f53070afbcaefb&platform=1&sign_type=RSA×tamp=1536032317";
String sign = "";
try {
byte[] buffer = Base64Utils.decode(privateKey);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = Instance(KEYPAIR_GENERATOR_ALGORITHM);
PrivateKey prK = atePrivate(keySpec);
Signature signature = Instance(SIGNATURE_ALGORITHM);
signature.initSign(prK);
signature.Bytes());
sign =  de(signature.sign());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sign;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RsaSign rs = new RsaSign();
String encryData = "app_id=12996762&app_secret=902ac71d8fdbdfc097f53070afbcaefb&platform=1&sign_type=RSA×tamp=1536032317";
System.out.println(rs.rsa_sign(encryData));
}
}
其中RsaSign是我主要想要调⽤的⼯具类。
6.打包即可,这⾥注意,打包时需要将maven依赖包“commons-code-1.11.jar”也打进来,顾需要通过插件“maven-assembly-plugin”,前⾯已经加⼊在pom中了。
7.打包成功后,在target⽬录下可以得到需要的jar包,如
8.将jar包放到python项⽬下,下⾯我们开始编写python代码,进⾏jar包的引⽤
jar_utils.py
import jpype
def get_rsa_sign(sign_raw):
"""
调⽤java jar包,对⼊参进⾏rsa签名
:param sign_raw:待签名字符串
:return:signature:签名后的加密字符串
"""
# 启动JVM
jvmPath = DefaultJVMPath()
# 加载jar包
jpype.startJVM(jvmPath, "-ea", "-Djava.class.path=utils-0.0.1-SNAPSHOT-jar-with-dependencies.jar")
# 指定main class
JDClass = jpype.JClass("com.rfchina.utils.RsaSign")
# 创建类实例对象
jd = JDClass()
# 引⽤jar包类中的⽅法 rsa_sign
signature = jd.rsa_sign(sign_raw)
# 关闭JVM
jpype.shutdownJVM()
return signature
if__name__ == '__main__':
sign_raw = "app_id=68613717&app_secret=039b8b6f1544f62dace6fe53e4c380be&platform=1&sign_type=RSA×tamp=1536039464"
print("rsa 签名结果:",get_rsa_sign(sign_raw))
运⾏结果如下:
JVM has been shutdown
rsa 签名结果: Z8OqE9ldgp7+gG6+fFm/N17pqQAIvGzQY7VztYzWgsSEYiyi7g9piHPIZqB22ekRZfzPFpOXM0lXc+GDjZWYwN+0XJGvJYQMWDGuTgHcf8c6VhiiOgUHXtptHG4Op+06v3SJ1iNaEy8F5tH9Y74X6GhTqR28SHuW3sdep1AhExY=
***扫⼀扫,关注“python测试开发圈”,了解更多测试教程!***

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。