In [46]:
JAVABIN = "java -jar ../javallier/target/scala-2.10/javallier.jar"
PYBIN = "~/py35/bin/pheutil"
print("Check we can access both command line tools:")
!{PYBIN} --version
!{JAVABIN}
In [47]:
test_numbers = [0, 1, 1000, 5000, 1e12]
In [48]:
# Generate the key using Python
!{PYBIN} genpkey --keysize 1024 py_1024_priv.json
!{PYBIN} extract py_1024_priv.json py_1024_public.json
!{JAVABIN} genpkey --keysize 1024 java_1024_priv.json
!{JAVABIN} extract java_1024_priv.json java_1024_public.json
In [50]:
# Python code encrypt with Java public key, and vice-versa
for i, num in enumerate(test_numbers):
!{PYBIN} encrypt --output py_{i}.enc java_1024_public.json {num}
!{JAVABIN} encrypt --output java_{i}.enc py_1024_public.json {num}
In [51]:
import json
import base64
from binascii import hexlify
def base64url_decode(payload):
l = len(payload) % 4
if l == 2:
payload += '=='
elif l == 3:
payload += '='
elif l != 0:
raise ValueError('Invalid base64 string')
return int(hexlify(base64.urlsafe_b64decode(payload.encode('utf-8'))), 16)
with open('py_1024_public.json') as f:
print("Python public key encoding:")
file = json.load(f)
print(file)
s = file['n']
print(s)
print(base64url_decode(s))
print("\nJava public key encoding:")
with open('java_1024_public.json') as f:
file = json.load(f)
print(file)
s = file['n']
print(s)
print(base64url_decode(s))
In [52]:
# Decrypt from java all the values encrypted bt the python code with the java public key
for i, num in enumerate(test_numbers):
!{JAVABIN} decrypt --output java_{i}.plaintext java_1024_priv.json py_{i}.enc
out = open("java_{}.plaintext".format(i)).read().strip()
print(out)
print(float(num))
In [53]:
# Decrypt from python all the values encrypted bt the java code with the python public key
for i, num in enumerate(test_numbers):
!{PYBIN} decrypt --output py_{i}.plaintext py_1024_priv.json java_{i}.enc
out = open("py_{}.plaintext".format(i)).read().strip()
print(out)
print(float(num))
In [ ]: