Convert from Java to Python

How can I convert this Java encryption code to python

The bug in the Python code is in the concatenation, it must be:

base64ed = base64.b64encode(cipher.nonce + ciphertext + tag).decode('utf-8')

After this fix, if you compare the ciphertexts of both codes with identical input data (especially with identical IV), they are the same.

how can I translate efficiently a Java code to python?

If you want to translate java code to python you have to translate it manually. Automatic conversion generally does not have the appropriate quality. It looks like there are some tools out e.g. java2python but the author states

The generated Python code is not guaranteed to run, nor is guaranteed to be syntactically valid Python.

Converting a library to another programming language is never an easy task.

If you simply want to use a java library in a application that you want to write in python you could give jython a try.

Parsing string long to int with java and python

In the end, I used the answer from gengkev here.

By playing around, I found something similar to BreizhGatch's function, but that doesn't require a conditional statement. n & 0x80000000 extracts the 32-bit sign bit; then, the - keeps the same 32-bit representation but sign-extends it; finally, the extended sign bits are set on n.

def toSigned32(n):

n = n & 0xffffffff
return n | (-(n & 0x80000000))

How to convert Java datetime code to Python

You have to install pytz (a World Timezone Definitions package) on your system.

Try this :

from datetime import datetime
from pytz import timezone

now =;
gmt_time = now.replace(tzinfo=timezone('GMT'))
print(gmt_time.strftime("%a, %d %b %Y %H:%M:%S %z"))

I have referred from here (this includes the installation process) :

Related Topics

Leave a reply