Ejemplo n.º 1
0
def paillier_cryptosystem():
    from charm.toolbox.integergroup import RSAGroup
    from charm.schemes.pkenc.pkenc_paillier99 import Pai99

    group = RSAGroup()
    pai = Pai99(group)
    (public_key, secret_key) = pai.keygen()

    operand_1 = 3
    operand_2 = 4
    encode_msg_1 = pai.encode(public_key['n'], operand_1)
    encode_msg_2 = pai.encode(public_key['n'], operand_2)
    cipher_1 = pai.encrypt(public_key, encode_msg_1)
    cipher_2 = pai.encrypt(public_key, encode_msg_2)

    # supported
    paillier_plus(pai, public_key, secret_key, operand_1, operand_2, cipher_1,
                  cipher_2)
    paillier_mult(pai, public_key, secret_key, operand_1, operand_2, cipher_1,
                  cipher_2)

    # unsupported
    paillier_minus(pai, public_key, secret_key, operand_1, operand_2, cipher_1,
                   cipher_2)
    paillier_division(pai, public_key, secret_key, operand_1, operand_2,
                      cipher_1, cipher_2)
Ejemplo n.º 2
0
def setupFPE4Cyber():
    global pai
    global pubKeyPaillier
    global secKeyPaillier
    global fpeKey
    global simmKey_ora_Leak
    global simmKey_ibm_dos
    global aes_encSuite_ora
    global aes_encSuite_ibm
    global aes_decSuite_ora
    global aes_decSuite_ibm

    # setup Paillier parameters
    group = RSAGroup()
    pai = Pai99(group)
    (pubKeyPaillier, secKeyPaillier) = pai.keygen(secparam=32)
    fpeKey = "01101001001111000011101001100101"  # por ejemplo!
    # AES
    simmKey_ora_Leak = '0' * 16  # por ejemplo!
    simmKey_ibm_dos = '1' * 16  # por ejemplo!
    aes_encSuite_ora = AES.new(simmKey_ora_Leak, AES.MODE_CBC,
                               'This is an IV456')
    aes_encSuite_ibm = AES.new(simmKey_ibm_dos, AES.MODE_CBC,
                               'This is an IV456')
    aes_decSuite_ora = AES.new(simmKey_ora_Leak, AES.MODE_CBC,
                               'This is an IV456')
    aes_decSuite_ibm = AES.new(simmKey_ibm_dos, AES.MODE_CBC,
                               'This is an IV456')
Ejemplo n.º 3
0
    def testPai99(self):
        group = RSAGroup()
        pai = Pai99(group)

        (pk, sk) = pai.keygen()

        m1 = pai.encode(pk['n'], 12345678987654321)
        m2 = pai.encode(pk['n'], 12345761234123409)
        m3 = pai.encode(pk['n'], 24691440221777730)  # target
        c1 = pai.encrypt(pk, m1)
        c2 = pai.encrypt(pk, m2)

        if debug: print("c1 =>", c1, "\n")
        if debug: print("c2 =>", c2, "\n")
        c3 = c1 + c2
        if debug: print("Homomorphic Add Test...\nc1 + c2 =>", c3, "\n")

        orig_m = pai.decrypt(pk, sk, c3)
        if debug: print("orig_m =>", orig_m)

        # m3 = m1 + m2
        assert m3 == orig_m, "FAILED Decryption!!!"
        if debug: print("Successful Decryption!")

        if debug: print("Homomorphic Mul Test...\n")
        c4 = c1 + 200
        if debug: print("c4 = c1 + 200 =>", c4, "\n")
        orig_m = pai.decrypt(pk, sk, c4)
        if debug: print("m4 =>", orig_m, "\n")

        c5 = c2 * 20201
        if debug: print("c5 = c2 * 2021 =>", c5, "\n")
        orig_m = pai.decrypt(pk, sk, c5)
        if debug: print("m5 =>", orig_m, "\n")
Ejemplo n.º 4
0
    def testPai99(self):
        group = RSAGroup()
        pai = Pai99(group)

        (pk, sk) = pai.keygen()

        m1 = 12345678987654321
        m2 = 12345761234123409
        m3 = 24691440221777730  # target
        c1 = pai.encrypt(pk, m1)
        c2 = pai.encrypt(pk, m2)

        if debug: print("c1 =>", c1, "\n")
        if debug: print("c2 =>", c2, "\n")
        c3 = c1 + c2
        if debug: print("Homomorphic Add Test...\nc1 + c2 =>", c3, "\n")

        orig_m = pai.decrypt(pk, sk, c3)
        if debug: print("orig_m =>", orig_m)

        # m3 = m1 + m2
        assert m3 == orig_m, "FAILED Decryption!!!"
        if debug: print("Successful Decryption!")

        if debug: print("Homomorphic Mul Test...\n")
        c4 = c1 + 200
        if debug: print("c4 = c1 + 200 =>", c4, "\n")
        orig_m = pai.decrypt(pk, sk, c4)
        if debug: print("m4 =>", orig_m, "\n")

        c5 = c2 * 20201
        if debug: print("c5 = c2 * 2021 =>", c5, "\n")
        orig_m = pai.decrypt(pk, sk, c5)
        if debug: print("m5 =>", orig_m, "\n")

        messages = range(0, 10)
        cts = []

        for m in messages:
            c = pai.encrypt(pk, pai.encode(pk['n'], m))
            cts.append(c)
            enc_m = pai.encode(pk['n'], m)
            rec_m = pai.decrypt(pk, sk, c)
            assert rec_m == m, "Failed to decrypt"

        # test homomorphic properties (addition)
        c0 = cts[0]
        for i in range(1, len(cts)):
            c0 = c0 + cts[i]

        rec_sum = pai.decrypt(pk, sk, c0)
        print("Total Sum: ", rec_sum)
        tot_sum = sum(list(messages))
        assert rec_sum == tot_sum, "Failed to decrypt to correct sum"
Ejemplo n.º 5
0
def demoPaillier():
    #DEMO PAILLIER -- suma!!
    group = RSAGroup()
    pai = Pai99(group)
    (pubKeyPaillier, secKeyPaillier) = pai.keygen()
    msg_1=12345678987654321
    msg_2=12345761234123409
    msg_3 = msg_1 + msg_2
    print("msg1:%s\n,msg2:%s\n,msg3:%s\n"%(msg_1,msg_2,msg_3))
    msg_1 = pai.encode(pubKeyPaillier['n'], msg_1)
    msg_2 = pai.encode(pubKeyPaillier['n'], msg_2)
    msg_3 = pai.encode(pubKeyPaillier['n'], msg_3)
    print("msg1:%s\n,msg2:%s\n,msg3:%s\n"%(msg_1,msg_2,msg_3))
    cipher_1 = pai.encrypt(pubKeyPaillier, msg_1)
    cipher_2 = pai.encrypt(pubKeyPaillier, msg_2)
    print("ciph1:%s\n,ciph2:%s\n"%(cipher_1['c'],cipher_2['c']))
    cipher_3 = cipher_1 #+ cipher_2
    print("ciph3:%s\n"%(cipher_3))
    decrypted_msg_3 = pai.decrypt(pubKeyPaillier, secKeyPaillier, cipher_3)
    print(decrypted_msg_3)
    bool=decrypted_msg_3 == msg_3
    print(bool)
Ejemplo n.º 6
0
 def __init__(self, users=2):
     global pai, group
     group = RSAGroup()
     pai = Pai99(group)
     self.users = users
     self.r = 14  #this value act as the common hash output H(r) according to the protocol.
Ejemplo n.º 7
0
# -*- coding: utf-8 -*-
import random
import time
import numpy
import math
from charm.toolbox.integergroup import lcm, integer
from charm.toolbox.PKEnc import PKEnc
from charm.core.engine.util import *
from charm.toolbox.integergroup import RSAGroup
from charm.schemes.pkenc.pkenc_paillier99 import Pai99

if __name__ == "__main__":
    group = RSAGroup()
    pai = Pai99(group)
    (public_key, secret_key) = pai.keygen()

    # setting of plaintext
    m = 19910813
    msg = pai.encode(public_key['n'], m)

    # time
    print "Average time [msec], Standard Deviation [msec]"

    # time 1
    time_box = []
    for i in range(100):
        start = time.time()
        c = pai.encrypt(public_key, msg)
        elapsed_time = time.time() - start
        time_box.append(elapsed_time)
    ave = numpy.average(numpy.array(time_box))
Ejemplo n.º 8
0
 def __init__(self):
     PKEnc.__init__(self)
     self.group = RSAGroup()
Ejemplo n.º 9
0
class GM82(PKEnc):
    """
    >>> gm82 = GM82()
    >>> (pk, sk) = gm82.keygen(512)
    >>> zero = gm82.encrypt(pk, 0)
    >>> one = gm82.encrypt(pk, 1)
    >>> gm82.decrypt(sk, zero)
    0
    >>> gm82.decrypt(sk, one)
    1
    >>> gm82.decrypt(sk, gm82.xor(pk, zero, zero))
    0
    >>> gm82.decrypt(sk, gm82.xor(pk, zero, one))
    1
    >>> gm82.decrypt(sk, gm82.xor(pk, one, zero))
    1
    >>> gm82.decrypt(sk, gm82.xor(pk, one, one))
    0
    """
    def __init__(self):
        PKEnc.__init__(self)
        self.group = RSAGroup()

    def keygen(self, secparam):
        self.group.paramgen(secparam)

        # Find a random quadratic non-residue in the group
        x = self.group.random()
        times = 1
        while times < maxtimes and isResidue(x, self.group.p, self.group.q):
            x = self.group.random()
            times += 1

        # If we are not able to find a quadratic non-residue after 'maxtimes'
        # trials, abort and output error
        if times == maxtimes:
            print(
                "ERROR: non-residue not found after {} trials.".format(times))
            return None

        pk = (self.group.n, x)
        sk = (self.group.p, self.group.q)
        return (pk, sk)

    def encrypt(self, pk, m):
        (n, x) = pk

        y = self.group.random()
        while gcd(n, y) != 1:
            y = self.group.random()

        if m == 0:
            return y**2 % n
        else:
            return y**2 * x % n

    def decrypt(self, sk, c):
        (p, q) = sk
        return 0 if isResidue(c, p, q) else 1

    # Homomorphic XOR over ciphertexts
    def xor(self, pk, c1, c2):
        (n, _) = pk
        return (c1 * c2) % n