Пример #1
0
 def test_PyCtxt_creation_deletion(self):
     try:
         ctxt = PyCtxt()
         ctxt2 = PyCtxt(copy_ctxt=ctxt)
         pyfhel = Pyfhel()
         ctxt3 = PyCtxt(pyfhel=pyfhel)
         ctxt4 = PyCtxt(copy_ctxt=ctxt3)
     except Exception as err:
         self.fail("PyCtxt() creation failed unexpectedly: ", err)
     self.assertEqual(ctxt.size(), 2)
     self.assertEqual(ctxt._encoding, ENCODING_t.UNDEFINED)
     ctxt._encoding = ENCODING_t.FRACTIONAL
     self.assertEqual(ctxt._encoding, ENCODING_t.FRACTIONAL)
     del (ctxt._encoding)
     self.assertEqual(ctxt._encoding, ENCODING_t.UNDEFINED)
     self.assertEqual(ctxt.size(), 2)
     ctxt._pyfhel = pyfhel
     ctxt2._pyfhel = ctxt._pyfhel
     try:
         del (ctxt)
     except Exception as err:
         self.fail("PyCtxt() deletion failed unexpectedly: ", err)
Пример #2
0
#Assuming that ciphertext is received, verify reliability

sum_d = base64.b64decode(bytes(sum_e, "utf-8"))
sub_d = base64.b64decode(bytes(sub_e, "utf-8"))
mul_d = base64.b64decode(bytes(mul_e, "utf-8"))

with open("txt.c1", "wb") as t1_f:
    t1_f.write(sum_d)

with open("txt.c2", "wb") as t2_f:
    t2_f.write(sub_d)

with open("txt.c3", "wb") as t3_f:
    t3_f.write(mul_d)

sum = PyCtxt()
sum.load("txt.c1")
sum._encoding = ENCODING_t.INTEGER

sub = PyCtxt()
sub.load("txt.c2")
sub._encoding = ENCODING_t.INTEGER

mul = PyCtxt()
mul.load("txt.c3")
mul._encoding = ENCODING_t.INTEGER

HE.restoresecretKey("secret_k.pysk")
print(" addition:       decrypt(ctxt1 + ctxt2) =  ", HE.decryptInt(sum))
print(" substraction:   decrypt(ctxt1 - ctxt2) =  ", HE.decryptInt(sub))
print(" multiplication: decrypt(ctxt1 * ctxt2) =  ", HE.decryptInt(mul))
Пример #3
0
    def put(self, func):
        todos[func] = "1"
        a = request.form['a']
        b = request.form['b']
        c = request.form['c']
        d = request.form['d']
        HE = Pyfhel()

        #Import context
        con = base64.b64decode(bytes(a, "utf-8"))
        with open('context.pycon', "wb") as pk_fw:
            pk_fw.write(con)

        HE.restoreContext("context.pycon")

        # Import public key
        pk = base64.b64decode(bytes(b, "utf-8"))
        with open('public_k.pypk', "wb") as pk_fw:
            pk_fw.write(pk)
        HE.restorepublicKey("public_k.pypk")

        # Import Ciphertext 1
        c1 = base64.b64decode(bytes(c, "utf-8"))
        with open('ctxt.c1', "wb") as c1_fw:
            c1_fw.write(c1)
        ctxt1 = PyCtxt()
        ctxt1.load("ctxt.c1")
        ctxt1._encoding = ENCODING_t.INTEGER

        # Import Ciphertext 2
        c2 = base64.b64decode(bytes(d, "utf-8"))
        with open('ctxt.c2', "wb") as c2_fw:
            c2_fw.write(c2)
        ctxt2 = PyCtxt()
        ctxt2.load("ctxt.c2")
        ctxt2._encoding = ENCODING_t.INTEGER

        #密文同态运算
        ctxtSum = HE.add(ctxt1, ctxt2, True)
        ctxtSub = HE.sub(ctxt1, ctxt2, True)
        ctxtMul = HE.multiply(ctxt1, ctxt2, True)

        #Ciphertext homomorphism operation
        ctxtSum.save("sum")
        ctxtSub.save("sub")
        ctxtMul.save("mul")

        with open("sum", "rb") as f_sum:
            sum_e = str(base64.b64encode(f_sum.read()), "utf-8")

        with open("sub", "rb") as f_sub:
            sub_e = str(base64.b64encode(f_sub.read()), "utf-8")

        with open("mul", "rb") as f_mul:
            mul_e = str(base64.b64encode(f_mul.read()), "utf-8")

        if func == "add":
            todos[func] = sum_e
        elif func == "sub":
            todos[func] = sub_e
        elif func == "mul":
            todos[func] = mul_e
        return todos[func]