0

I have a task to encrypt a string using java class and replace the encrypted string into same file. I am trying to use subprocess but I have no luck, Kindly let me know what mistake I am making.

from subprocess import check_output,STDOUT, CalledProcessError
    
def search(filename,decryptstring):
    # some python code ...
    args= [ "C:/x/x/x/x/jre1.8.0_77/bin/java -cp" +" " '"lib\*"' +" " "x.x.x.x.x.util.CryptUtil" + " " "-decrypt" " "+  dstring]
    h = subprocess.run(args, shell=True, stdout=subprocess.PIPE, universal_newlines= True, bufsize=-1 )

Output:

CompletedProcess(args=['C:/x/x/x/x/jre1.8.0_77/bin/java -cp'], returncode=1, stdout='')
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47

2 Answers2

0

There is an SO answer to this question.

I would prefer finding a Python encryption class rather than cross languages. Seems like an unnecessary complication. Encryption algorithms are general and should have implementations in many languages. I would find a Python equivalent.

What encryption/decryption algorithm do you plan to use? How will you handle public/private keys?

Another solution would be to expose the Java encryption/decryption as a web service and send HTTP requests from Python. You can reuse the Java class this way, but it'll come at the cost of extra latency.

There is a Jython binding for the JVM, but I believe it's stuck on Python 2.x.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Remove -cp from the args.

from subprocess import check_output,STDOUT, CalledProcessError
    
def search(filename,decryptstring):
    # some python code ...
    args= [ "C:/x/x/x/x/jre1.8.0_77/bin/java" +" " '"lib\*"' +" " "x.x.x.x.x.util.CryptUtil" + " " "-decrypt" " "+  dstring]
    h = subprocess.run(args, shell=True, stdout=subprocess.PIPE, universal_newlines= True, bufsize=-1 )
Anto Livish A
  • 322
  • 4
  • 15