Пример #1
0
  def decrypt(self, filename, keyB_private):
    ciphertext = self.read_file_list(filename)
    
    process = PointOperatorECC(self._get_A(), self._get_B(), self._get_P())

    key_pri = self.read_file_list(keyB_private)
    
    kG = Point()
    kG._set_x(ciphertext.pop(0))
    kG._set_y(ciphertext.pop(0))
    kGPub = process.multiply(key_pri, kG)
    
    print("(x_pub, y_pub) : ", kGPub._get_x(), kGPub._get_y())
    i = 0
    list_plain_point = []
    while i <  len(ciphertext):
      point_cipher = Point()
      point_cipher._set_x(ciphertext[i])
      point_cipher._set_y(ciphertext[i+1])
      res = process.minus(point_cipher, kGPub)
      print("(x , y, x_min, y_min) : ", ciphertext[i], ciphertext[i+1], res._get_x() , res._get_y())
      list_plain_point.append(res)
      i+=2
    
    # for i in range(len(list_plain_point)):
    #   print(list_plain_point[i]._get_x(), list_plain_point[i]._get_y())

    list_plain_int = self.decoding(list_plain_point)
    print(list_plain_int)
    regex = re.compile('.\w+').findall(filename)
    self.write_file_list("out_ecceg" + regex[-1], list_plain_int)
Пример #2
0
 def generate_galois_field(self):
   field = []
   for i in range(self._get_P()):
     list_y = self.compute_ecc_equation(i)
     if list_y:
       for j in range(len(list_y)):
         point = Point()
         point._set_x(i)
         point._set_y(list_y[j])
         field.append(point)
   return field
Пример #3
0
 def encoding(self, list_message):
   result = []
   for i in range(len(list_message)):
     for j in range(self._get_k()-1):
       x = (list_message[i] * self._get_k() + (j + 1))
       y = self.compute_ecc_equation(x)
       # print(x ,y)
       if y:
         point = Point()
         point._set_x(x)
         point._set_y(y[0]) 
         result.append(point)
         break
   return result
Пример #4
0
  def encrypt(self, filename, keyB_public, keyB_private, k):
    plaintext = self.read_file(filename)
    list_plaintext = list(plaintext)
    list_plain_int = list(map(int, plaintext))
    print(list_plain_int)

    key_pub = self.read_file_list(keyB_public)
    point_key_pub = Point()
    point_key_pub._set_x(key_pub[0])
    point_key_pub._set_y(key_pub[1])
    print("(x_pubB, y_pubB) : ", point_key_pub._get_x(), point_key_pub._get_y())    
    list_message_point = self.encoding(list_plain_int)
    # print(self.decoding(list_message_point))

    key_pri = self.read_file_list(keyB_private)

    process = PointOperatorECC(self._get_A(), self._get_B(), self._get_P())
    field = self.generate_galois_field()
    # Base point in galois field[0]
    base = field[0]

    # for i in range(len(list_message_point)):
    #   print(list_message_point[i]._get_x(), list_message_point[i]._get_y())


    # K value, different from k for encoding
    kG = process.multiply(k, base)
    priG = process.multiply(key_pri, base) 
    print("(x_priG, y_priG) : ", priG._get_x(), priG._get_y())        
    keyPub = process.multiply(key_pri, kG) #8x9
    print(key_pri)
    print("(x_pripub, y_pripub) : ", keyPub._get_x(), keyPub._get_y())        
    kGPub = process.multiply(k, priG) #9x8
    print(k)
    print("(x_pub, y_pub) : ", kGPub._get_x(), kGPub._get_y())    
    cipher = []
    cipher.append(kG._get_x())
    cipher.append(kG._get_y())
    for i in range(len(list_message_point)):
      msg_cipher = process.add(list_message_point[i], kGPub)
      print("(x, y, x_add, y_add) : ", list_message_point[i]._get_x(), list_message_point[i]._get_y(), msg_cipher._get_x(), msg_cipher._get_y())
      cipher.append(msg_cipher._get_x())
      cipher.append(msg_cipher._get_y())

    print("==============================================================")
    
    regex = re.compile('.\w+').findall(filename)
    self.write_file_list("cipher_ecceg" + regex[-1], cipher)
Пример #5
0
    def double_point(self, p):
        result = Point()

        if p._get_y() == 0:
            result._set_x(sys.maxsize)
            result._set_y(sys.maxsize)
        else:
            inv = inverse(2 * p._get_y(), self._get_P())
            lamda = ((3 *
                      (p._get_x())**2 + self._get_A()) * inv) % self._get_P()
            lamda = int(lamda % self._get_P())
            # print(lamda)

            xr = (lamda**2 - (2 * p._get_x())) % self._get_P()
            # print(xr)
            yr = (lamda * (p._get_x() - xr) - p._get_y()) % self._get_P()
            # print(yr)

            result._set_x(int(xr))
            result._set_y(int(yr))

        return result
Пример #6
0
    def add(self, p1, p2):
        result = Point()

        if p1._get_x() == 0 and p1._get_y() == 0:
            result._set_x(p2._get_x())
            result._set_x(p2._get_y())
        elif p2._get_x() == 0 and p2._get_y() == 0:
            result._set_x(p1._get_x())
            result._set_x(p1._get_y())
        elif p1._get_y() - p2._get_y() == 0:
            result._set_x((0 - p1._get_x() - p2._get_x()) % self._get_P())
            result._set_x((0 - p1._get_x()) % self._get_P())
        elif p1._get_x() - p2._get_x() == 0:
            result._set_x(sys.maxsize)
            result._set_y(sys.maxsize)
        else:
            inv = inverse((p1._get_x() - p2._get_x()), self._get_P())
            lamda = ((p1._get_y() - p2._get_y()) * inv) % self._get_P()

            xr = (lamda**2 - p1._get_x() - p2._get_x()) % self._get_P()
            yr = (lamda * (p1._get_x() - xr) - p1._get_y()) % self._get_P()

            result._set_x(xr)
            result._set_y(yr)

        return result
Пример #7
0
            result._set_x((0 - p1._get_x()) % self._get_P())
        elif p1._get_x() - p2._get_x() == 0:
            result._set_x(sys.maxsize)
            result._set_y(sys.maxsize)
        else:
            inv = inverse((p1._get_x() - p2._get_x()), self._get_P())
            lamda = ((p1._get_y() - p2._get_y()) * inv) % self._get_P()

            xr = (lamda**2 - p1._get_x() - p2._get_x()) % self._get_P()
            yr = (lamda * (p1._get_x() - xr) - p1._get_y()) % self._get_P()

            result._set_x(xr)
            result._set_y(yr)

        return result


if __name__ == '__main__':
    point = Point()
    point._set_x(2)
    point._set_y(4)
    # p1 = Point()
    # p1._set_x(2)
    # p1._set_y(4)
    # p2 = Point()
    # p2._set_x(5)
    # p2._set_y(9)
    op = PointOperatorECC(1, 6, 11)
    result = op.double_point(point)
    print(result._get_x(), result._get_y())