Beispiel #1
0
def test_order_guarantees():
    """Test that encryption is order-preserving"""
    values = [0, 1, 2, 10, 28, 42, 1000, 1001, 2**15 - 1]
    key = b'key'
    cipher = OPE(key)
    encrypted_values = [cipher.encrypt(value) for value in values]
    assert encrypted_values == sorted(set(encrypted_values)), "Order is not preserved"
Beispiel #2
0
def test_ope_deterministic():
    """Test that encrypting the same values yields the same results"""
    values = [0, 314, 1337, 1338, 10000]
    cipher = OPE(b'key-la-la')
    encrypted_values_first = [cipher.encrypt(value) for value in values]
    encrypted_values_second = [cipher.encrypt(value) for value in values]
    assert encrypted_values_first == encrypted_values_second
Beispiel #3
0
def test_ope_deterministic():
    """Test that encrypting the same values yields the same results"""
    values = [0, 314, 1337, 1338, 10000]
    cipher = OPE(b'key-la-la')
    encrypted_values_first = [cipher.encrypt(value) for value in values]
    encrypted_values_second = [cipher.encrypt(value) for value in values]
    assert encrypted_values_first == encrypted_values_second
Beispiel #4
0
def test_encrypt_small_out_range_issue():
    """Regression test for this issue: https://github.com/tonyo/pyope/issues/13"""
    cipher = OPE(b'fresh key',
                 in_range=ValueRange(0, 2),
                 out_range=ValueRange(2, 5))
    assert cipher.encrypt(0)
    assert cipher.encrypt(1)
    assert cipher.encrypt(2)
Beispiel #5
0
def test_big_ranges():
    in_range = ValueRange(2**32, 2**33)
    out_range = ValueRange(2**48, 2**49)
    ope = OPE(b'test-big-ranges', in_range, out_range)
    plaintext = in_range.start
    while plaintext <= in_range.end:
        assert ope.encrypt(plaintext)
        plaintext += 2**24
Beispiel #6
0
def test_order_guarantees():
    """Test that encryption is order-preserving"""
    values = [0, 1, 2, 10, 28, 42, 1000, 1001, 2**15 - 1]
    key = b'key'
    cipher = OPE(key)
    encrypted_values = [cipher.encrypt(value) for value in values]
    assert encrypted_values == sorted(
        set(encrypted_values)), "Order is not preserved"
Beispiel #7
0
def insertion(request):
    print(request.POST)
    cipher = OPE(b'mDErfp0Arn+noGTR+p0P9CmGU8+KKenk8ff+6aHxPi0=')
    for i in range(1, 10):
        value = random.randrange(1, 10000)
        value = cipher.encrypt(value)
        data_instance = DatabaseData.objects.create(key=i, number=value)
    return render(request, "insertion.html")
Beispiel #8
0
def test_long_different_keys():
    """Test that different keys yield different ciphertexts"""
    key1 = b'\x12\x23\x34\x45\x56\x67\x78\x89\x90\x0A\xAB\xBC\xCD\xDE\xEF\xF0\x13\x14\x15\x16'
    key2 = b'\x0A\xAB\xBC\xCD\xDE\xEF\xF0\x13\x14\x15\x16\x12\x23\x34\x45\x56\x67\x78\x89\x90\x12\x13'
    ope1, ope2 = OPE(key1), OPE(key2)
    values = [0, 1, 10, 100, 1000, 2000, 3000, 4000, 5000]
    for v in values:
        assert ope1.encrypt(v) != ope2.encrypt(v)
Beispiel #9
0
def test_long_different_keys():
    """Test that different keys yield different ciphertexts"""
    key1 = b'\x12\x23\x34\x45\x56\x67\x78\x89\x90\x0A\xAB\xBC\xCD\xDE\xEF\xF0\x13\x14\x15\x16'
    key2 = b'\x0A\xAB\xBC\xCD\xDE\xEF\xF0\x13\x14\x15\x16\x12\x23\x34\x45\x56\x67\x78\x89\x90\x12\x13'
    ope1, ope2 = OPE(key1), OPE(key2)
    values = [0, 1, 10, 100, 1000, 2000, 3000, 4000, 5000]
    for v in values:
        assert ope1.encrypt(v) != ope2.encrypt(v)
Beispiel #10
0
def homepage(request):
    data = DatabaseData.objects.order_by('number')[:10]

    if data:
        encrypted_data = DatabaseData.objects.values(
            'key', 'number').order_by('number')
        cipher = OPE(b'mDErfp0Arn+noGTR+p0P9CmGU8+KKenk8ff+6aHxPi0=')

        for x in encrypted_data:
            x["number"] = cipher.decrypt(x["number"])

        context = {'data': data, 'encrypted_data': encrypted_data}
    else:
        context = {}
    return render(request, 'index.html', context)
Beispiel #11
0
def test_dense_range():
    """Equal ranges must yield 1-to-1 mapping"""
    range_start = 0
    range_end = 2**15
    in_range = ValueRange(range_start, range_end)
    out_range = ValueRange(range_start, range_end)
    key = b'123'
    cipher = OPE(key, in_range, out_range)
    values = [0, 10, 20, 50, 100, 1000, 2**10, 2**15]
    for v in values:
        assert cipher.encrypt(v) == v
        assert cipher.decrypt(v) == v

    with pytest.raises(Exception):
        OPE(key, ValueRange(0,10), ValueRange(1,2))
Beispiel #12
0
def test_dense_range():
    """Equal ranges must yield 1-to-1 mapping"""
    range_start = 0
    range_end = 2**15
    in_range = ValueRange(range_start, range_end)
    out_range = ValueRange(range_start, range_end)
    key = b'123'
    cipher = OPE(key, in_range, out_range)
    values = [0, 10, 20, 50, 100, 1000, 2**10, 2**15]
    for v in values:
        assert cipher.encrypt(v) == v
        assert cipher.decrypt(v) == v

    with pytest.raises(Exception):
        OPE(key, ValueRange(0, 10), ValueRange(1, 2))
Beispiel #13
0
def test_ope_encrypt_decrypt():
    """Encrypt and then decrypt"""
    values = [-1000, -100, -20, -1, 0, 1, 10, 100, 314, 1337, 1338, 10000]
    key = b'key'
    in_range = ValueRange(-1000, 2**20)
    out_range = ValueRange(-10000, 2**32)
    # Client encrypts values
    cipher = OPE(key, in_range, out_range)
    encrypted_values = [cipher.encrypt(value) for value in values]

    # Decryption at the peer side
    cipher_dec = OPE(key, in_range, out_range)
    for value, encrypted in zip(values, encrypted_values):
        decrypted = cipher_dec.decrypt(encrypted)
        assert value == decrypted, "Dec(Enc(P)) != P"
Beispiel #14
0
def test_ope_encrypt_decrypt():
    """Encrypt and then decrypt"""
    values = [-1000, -100, -20, -1, 0, 1, 10, 100, 314, 1337, 1338, 10000]
    key = b'key'
    in_range = ValueRange(-1000, 2**20)
    out_range = ValueRange(-10000, 2**32)
    # Client encrypts values
    cipher = OPE(key, in_range, out_range)
    encrypted_values = [cipher.encrypt(value) for value in values]

    # Decryption at the peer side
    cipher_dec = OPE(key, in_range, out_range)
    for value, encrypted in zip(values, encrypted_values):
        decrypted = cipher_dec.decrypt(encrypted)
        assert value == decrypted, "Dec(Enc(P)) != P"
Beispiel #15
0
# Encrypts given integer value with given key.
#
# Usage:
#
# python encrypt.py key integerValue
# example: python encrypt.py Friday 1337
#

import sys
from pyope.ope import OPE
from pyope.ope import ValueRange
inrange = ValueRange(0, 2**34 - 1)
outrange = ValueRange(0, 2**50 - 1)
cipher = OPE(str(sys.argv[1]), inrange, outrange)
print(cipher.encrypt(int(sys.argv[2])))
Beispiel #16
0
from pyope.ope import OPE
from pyope.ope import ValueRange

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', int(sys.argv[1]))
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# store key
key = str(sys.argv[2])
inrange = ValueRange(0, 2**34-1)
outrange = ValueRange(0, 2**50-1)
cipher = OPE(key, inrange, outrange)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(32)
            if data:
                val = int(str(data).strip());
Beispiel #17
0
def instantiate_ope_cipher(key):
    return OPE(key,
               in_range=ValueRange(-100000000000, 100000000000),
               out_range=ValueRange(-214748364800, 214748364700))
Beispiel #18
0
# -*- coding: utf-8 -*-
import sys
from pyope.ope import OPE, ValueRange
cipher = OPE(b'key goes here' * 2,
             in_range=ValueRange(-2**31, 2**31 - 1),
             out_range=ValueRange(-2**53, 2**53 - 1))
input = sys.argv[1]
if " " in input:
    for i in input.split():
        print cipher.decrypt(int(i))
    print 0
else:
    print cipher.decrypt(int(input))
Beispiel #19
0
from pyope.ope import OPE
from pyope.ope import ValueRange

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', int(sys.argv[1]))
print >> sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# store key
key = str(sys.argv[2])
inrange = ValueRange(0, 2**34 - 1)
outrange = ValueRange(0, 2**50 - 1)
cipher = OPE(key, inrange, outrange)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print >> sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print 'got a connection'
        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(32)
            if data:
Beispiel #20
0
# Benchmarks en-/decrypion in pure python
#
# Usage:
#
# python benchmarkPurePython.py key startvalue maxvalue
# example: python benchmarkPurePython.py Friday 0 10000
#

import sys
from pyope.ope import OPE
from pyope.ope import ValueRange
from time import *

inrange = ValueRange(0, 2**34 - 1)
outrange = ValueRange(0, 2**50 - 1)
cipher = OPE(str(sys.argv[1]), inrange, outrange)

t1 = clock()

i = int(sys.argv[2])
maxvalue = int(sys.argv[3])
while (i < maxvalue):
    enc = cipher.encrypt(i)
    dec = cipher.decrypt(enc)
    #print str(i) + ':(' + str(enc) + ', ' + str(dec) + ')'
    i = i + 1

t2 = clock()

print 'duration = ' + str((t2 - t1) * 1000) + 'ms'
Beispiel #21
0
def test_huge_output_range():
    """Regression test for https://github.com/tonyo/pyope/pull/16"""
    cipher = OPE(b'key11',
                 in_range=ValueRange(0, 0),
                 out_range=ValueRange(0, 2**65))
    assert cipher.encrypt(0)
Beispiel #22
0
import sys
from pyope.ope import OPE, ValueRange
cipher = OPE(b'key goes here' * 2, in_range=ValueRange(-2**31, 2**31-1), out_range=ValueRange(-2**53, 2**53-1))
input = sys.argv[1]
print cipher.encrypt(int(input))
Beispiel #23
0
import sys
from pyope.ope import OPE, ValueRange
cipher = OPE(b'key goes here' * 2, in_range=ValueRange(-2**31, 2**31-1), out_range=ValueRange(-2**53, 2**53-1))
input = list(sys.argv[1])
for c in input:
	print cipher.encrypt(ord(c))

Beispiel #24
0
from timeit import default_timer as timer

from pyope.ope import OPE

from crypto_utils import instantiate_ope_cipher

random_key = OPE.generate_key()
cipher = instantiate_ope_cipher(random_key)

ciphertexts = []
num = 1000

start = timer()
for value in range(num):
    ciphertexts.append(cipher.encrypt(value))

end = timer()
print(f"OPE Encryption time for {num} values: {end - start}")


start = timer()
for c in ciphertexts:
    cipher.decrypt(c)

end = timer()
print(f"OPE Decryption time for {num} values: {end - start}")
Beispiel #25
0
def cipher():
    random_key = OPE.generate_key()
    c = instantiate_ope_cipher(random_key)
    return c