current position:Home>How does Python correctly call jar package encryption to get the encrypted value?

How does Python correctly call jar package encryption to get the encrypted value?

2021-08-23 06:21:55 Shanghai - youyou


When it comes to interface automation , We often encounter some parameters that need to be encrypted , For example, password parameters . The general developers of encryption rules don't want to tell you , I'll give you a jar package , Let you call jar The packet gets the encrypted value , stay jmeter It can be directly referenced jar Bag , but python call jar The bag will be a little troublesome .

Environmental preparation

My computer environment :

  • windows10
  • python3.6.6 (64 position )
  • jdk 1.8.0_20 (64 position )

install jdk When , It will also be installed jre, The installation directory structure is as follows (jdk Installation of their own online to find a detailed tutorial )

After installation, check the version number and jar command

C:\Users\dell>java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

 usage : jar {ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
 Options :
    -c   Create a new profile 
    -t   Make a list of files 
    -x   Extract the specified... From the file  ( Or all )  file 
    -u   Update existing files 
    -v   Generate detailed output in standard output 
    -f   Specify the file name 
    -m   Contains the manifest information in the specified manifest file 
    -n   Create a new file and execute  Pack200  Normalization 
    -e   Bind to executable for  jar  File independent application 
         Specify the application entry point 
    -0   Store only ;  Do not use any  ZIP  Compress 
    -M   Don't create a manifest file for items 
    -i   For the specified  jar  File generation index information 
    -C   Change to the specified directory and include the following files 
 If any file is a directory ,  Then recursively . Listing filename ,  The specified order of file name and entry point name 
 And  'm', 'f'  and  'e'  Tags are specified in the same order . Example  1:  File two class files into a file called  classes.jar  In the archives of :
       jar cvf classes.jar Foo.class Bar.class
 Example  2:  Use the existing manifest file  'mymanifest'  and 
            take  foo/  All files in the directory are archived to  'classes.jar'  in :
       jar cvfm classes.jar mymanifest -C foo/ .

encryption jar package

With the simplest md5 encryption jar For example, bag

java Reference method

import com.iyunwen.crypto.Encrypto;

String password = "admin";
Mademd5 md = new Mademd5();

python call jar

Install dependency packages jpype1 ( I'm going to add one after that 1)

> pip install jpype1

start-up JVM, Print ”hello world”

import jpype
#  author - Shanghai is long  QQ Communication group :717225969
# blog Address

#  obtain JVM The installation path 
jvmPath = jpype.getDefaultJVMPath()
#  start-up JVM
#  perform java Code"hello world!")
#  close JVM

It can print out correctly after running ”hello world”, It indicates that there is no problem with the installation of the environment

Call encryption jar package

jpype.JClass Method to import the class

import com.ndktools.javamd5pto; learn 

String password = "admin";
Mademd5 md = new Mademd5();

hold java Code translation into python It's the following code

import jpype
#  author - Shanghai is long  QQ Communication group :717225969
# blog Address

#  obtain JVM The installation path 
jvmPath = jpype.getDefaultJVMPath()
jarPath = 'JavaMD5.jar'  # jar Package path , You can put the code in the same directory 
#  start-up JVM
jpype.startJVM(jvmPath, "-Djava.class.path=%s" % jarPath)
#  perform java Code 
#"hello world!")
#  call java class object , Import the package first 
md5class = jpype.JClass("com.ndktools.javamd5.Mademd5")
password = "yoyo"
md = md5class()   #  Instantiation 
md5_password = md.toMd5(password)
#  close JVM

Running results :48DC8D29308EB256EDC76F25DEF07251

Imported jar When there are multiple bags

When importing jar When there is only one bag , With the above method , If there are more than one jar Package needs to be imported , Merge first jar Merge JAR It is divided into 2 Step :

  • decompression JAR
  • Merge all the source code

install JDK after , adopt jar -xvf Command to unzip a single JAR, This will be generated in the same level directory according to the package name JAVA The compiled class file

#  First unzip one by one jar, Generate class file 
jar -xvf a.jar
jar -xvf b.jar
jar -xvf c.jar

Use jar -cvfM command , Put all the local class The file generates a new JAR

jar -cvfM demo.jar .

There is a point at the end of the command (.),demo.jar It is the newly generated after the merger jar It's packed

Reference material : Reference material :

This article is from WeChat official account. - Learn automated testing from scratch (yoyoketang) , author : Shanghai is long

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .

Original publication time : 2021-08-11

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

copyright notice
author[Shanghai - youyou],Please bring the original link to reprint, thank you.

guess what you like

Random recommended