[This article is from the Sky Cloud-owned blog Park]
Demand
Recently, a custom AES plus decryption program in the background project code has been applied frequently in the usual testing work. Because writing scripts often need to be used, and after a variety of attempts, such as jpype, etc., are not satisfactory. Finally, the idea of conversion has found a good way:
1. Make the Java project into a jar package;
2. Set the Main-class property of the jar package;
3. Execute the JAR package through subprocess in Python.
Modify main function to hit jar package
To hit the jar package in IntelliJ:
1. file--> Project Structure----artifacts-------modules with dependencies--- > tick the Include in project build--records above the output directory path--OK;
2. Build--and build Project--then you can see the jar package in the output directory path;
3. Open the jar package with WinRAR, modify the Manifest.mf file under the Meta-inf folder, add a line of Main-class entry, for example: "Main-class:com.package.name.classname", Note After the colon is empty, the last empty line of the file.
Note: Executing a JAR package is the main function in the main-class that executes the jar package. so if you want to call the Java method in Python and pass the argument, modify the Main method in Main-class before hitting the jar package, Main-class is the target Java class.
You can set the parameters in the main function of Main-class:
String data= args[0= args[1]; // Call the decryption program and print the decryption results
Here data is the ciphertext, key is the secret key.
Writing a Python script
Then write the Python script, where the Python file is named aes.py, assuming that the jar package we used to decrypt is named Aes.jar:
ImportsubprocessImportChardetImportSYSclassAES (object):def __init__(self, data, key): Self.data=Data Self.key=KeydefDecrypt (self): command="Java-jar Aes.jar"arg0=self.data arg1=self.key cmd=[COMMAND,ARG0,ARG1] New_cmd=" ". Join (CMD) Stdout,stderr= subprocess. Popen (new_cmd,stdout=subprocess. Pipe,stderr=subprocess. PIPE). Communicate () encoding= Chardet.detect (stdout) ["encoding"] Result=stdout.decode (encoding)returnresultif __name__=='__main__': Data= Sys.argv[1] Key= Sys.argv[2] AES=AES (Data,key)Print(Aes.decrypt ())
Here, in order to correspond to the two parameters of the jar package, the Python script needs to pass in two arguments, the first is data to be decrypted, the second is the decryption key key.
Here we create a folder AES, where aes.py and Aes.jar are placed under this folder, both of which are in the AES root directory.
Then start cmd in the current directory, execute a python script, pass in data and key two parameters to the Python script:
" string to decrypt " " secret key "
The python script will then pass the two parameters to the jar package and call the decryption process in the main function of the jar package Main-class and print the decryption results.
This allows the Java decryption program to be called in Python to print out the decrypted string.
Resources
1. IntelliJ Idea Java Project import jar package, hit jar package
2. Java run jar command prompt without master manifest attribute
3. Use directives in CMD to execute JAR packages
Python calls the Java method