-
第一步 把Java代码打成jar包 -
第二步 安装python 模块 jpype ,安装方法 如下: pip install JPype1
-
第三步 写代码,代码我以封装,如下 import jpype
from jpype._core import startJVM, shutdownJVM
from jpype._jvmfinder import getDefaultJVMPath
class AES(object):
def __init__(self,jarpath,classname):
"""
:param jarpath: jar 包的位置
:param classname: 报名.类名
"""
startJVM(getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % jarpath)
JDClass = jpype.JClass(classname)
self.aes = JDClass()
def encrypt(self,string,key):
"""aes 加密"""
return self.aes.encrypt(string,key)
def decrypt(self,string,key):
"""aes解密"""
return self.aes.decrypt(string,key)
def close_jvm(self):
"""关掉java虚拟机"""
shutdownJVM()
-
调用方法如下: if __name__ == '__main__':
aes = AES("encryptdecrypt.jar","TEST.ENCRYPT")
string = "hell0world"
key = '6666'
print(aes.encrypt(string,key))
try:
print(aes.decrypt("sdDyvlgwwUt89ENom6R4pA==",key))
except Exception as e:
print(e)
finally:
aes.close_jvm()
|