Exemplo n.º 1
0
 def _resetData(self):
     if self._pkFile is not None and self._skFile is not None:
         self._data['e'], self._data['n'] = self._loadFile(self._pkFile)
         self._data['d'], _ = self._loadFile(self._skFile)
         self._canEncrypt, self._canDecrypt = True, True
     elif self._pkFile is not None:
         self._data['e'], self._data['n'] = self._loadFile(self._pkFile)
         self._canEncrypt = True
     elif self._skFile is not None:
         self._data['d'], self._data['n'] = self._loadFile(self._skFile)
         self._canDecrypt = True
     else:
         self._data = self._genKey()
         self._canEncrypt, self._canDecrypt = True, True
         self._saveFile('pk.txt', (self._data['e'], self._data['n']))
         self._saveFile('sk.txt', (self._data['d'], self._data['n']))
         self._saveFile('ak.txt', (self._data['p'], self._data['q']))
     if self._akFile is not None:
         self._data['p'], self._data['q'] = self._loadFile(self._akFile)
     if self._fastDecrypt and 'p' in self._data:
         self._data['xp'] = self._data['q'] * inverse(self._data['q'], self._data['p'])
         self._data['xq'] = self._data['p'] * inverse(self._data['p'], self._data['q'])
         self._data['dp'] = self._data['d'] % (self._data['p']-1)
         self._data['dq'] = self._data['d'] % (self._data['q']-1)
         self._canFast = True
     self._data['pk'] = (self._data['e'], self._data['n']) if self._canEncrypt else None
     self._data['sk'] = (self._data['d'], self._data['n']) if self._canDecrypt else None
     self._data['ak'] = (self._data['dp'], self._data['dq'], self._data['xp'], self._data['xq'], self._data['p'], self._data['q'], self._data['n']) if self._canFast else None
Exemplo n.º 2
0
def affine(x, key, mod=26, method='encrypt'):
    a, b = key
    if gcd(a, mod) != 1:
        return -1
    if method == 'encrypt':
        if not x.islower():
            return -1
        forward = dict()
        for i in range(26):
            forward[chr(ord('a') + i)] = chr(ord('A') + (a * i + b) % mod)
        code = str()
        for c in x:
            code += forward[c]
        return code
    elif method == 'decrypt':
        if not x.isupper():
            return -1
        backward = dict()
        for i in range(26):
            backward[chr(ord('A') + i)] = chr(
                ord('a') + (i - b + mod) * inverse(a, mod) % mod)
        code = str()
        for c in x:
            code += backward[c]
        return code
    else:
        return -1
Exemplo n.º 3
0
 def verify(self, m: bytes, sign: tuple, pk: int) -> bool:
     r, s = sign
     w = inverse(s, self._data['q'])
     u1 = (self._hash(m) * w) % self._data['q']
     u2 = (r * w) % self._data['q']
     v = ((quick_power(self._data['g'], u1, self._data['p']) * quick_power(
         pk, u2, self._data['p'])) % self._data['p']) % self._data['q']
     return v == r
Exemplo n.º 4
0
def crt(b, m):
    ans = 0
    lcm = 1
    for mi in m:
        lcm *= mi
    for i in range(len(m)):
        tpn = lcm // m[i]
        tpr = inverse(tpn, m[i])
        ans = (ans + tpn * tpr * b[i]) % lcm
    return (ans + lcm) % lcm
Exemplo n.º 5
0
 def sign(self, m: bytes, sk: int) -> tuple:
     k = None
     while k is None:
         temp_k = random.randint(3, self._q - 2)
         if gcd(temp_k, self._q - 1) == 1:
             k = temp_k
     s1 = quick_power(self._a, k, self._q)
     s2 = (inverse(k, self._q - 1) *
           ((self._hash(m) % self._q) - sk * s1)) % (self._q - 1)
     return (s1, s2)
 def _genKey(n=64):
     s = 0
     klist = list()
     for i in range(n):
         k = random.randint(s+2, 2*s+2)
         s += k
         klist.append(k)
     q = random.randint(s+2, 2*s+2)*2+1
     r = q
     while gcd(r, q) != 1:
         r = random.randint(2, q-1)
     invr = inverse(r, q)
     b = [(k*r)%q for k in klist]
     return {'w': klist, 'b': b, 'q': q, 'r': r, 'invr': invr}
Exemplo n.º 7
0
 def _genKey():
     primeList = list()
     while len(primeList) != 2:
         p = RSA._genPrime()
         if primeTest(p, prob = 0.001, method='miller_rabin'):
             primeList.append(p)
     p, q = primeList[0], primeList[1]
     n = p * q
     phi = (p-1) * (q-1)
     d = -1
     while d == -1:
         e = random.randint(phi//2, phi) # avoid small e
         d = inverse(e, phi)
     return {'e': e, 'd': d, 'n': n, 'p': p, 'q': q}
Exemplo n.º 8
0
 def sign(self, M:bytes, uid:bytes, SK:int) -> tuple:
     M, uid = self._bytes2bits(M), self._bytes2bits(uid)
     PK = SK * self._G
     Z = self._identity(uid, PK)
     M = bitarray.concat((Z, M))
     e = self._bytes2int(self._bits2bytes(self._hash(M)))
     while True:
         k = random.randint(1, self._n-1)
         P = k * self._G
         x1 = self._elem2int(P.x)
         r = (e + x1) % self._n
         if r == 0 or r+k == self._n:
             continue
         s = (inverse(1+SK, self._n) * (k-r*SK)) % self._n
         if s != 0:
             break
     r, s = self._int2bytes(r, self._byteLen), self._int2bytes(s, self._byteLen)
     return (r, s)
Exemplo n.º 9
0
 def sign(self, m: bytes, sk: int) -> tuple:
     k = random.randint(1, self._data['q'] - 1)
     r = quick_power(self._data['g'], k, self._data['p']) % self._data['q']
     s = (inverse(k, self._data['q']) *
          (self._hash(m) + sk * r)) % self._data['q']
     return (r, s)
Exemplo n.º 10
0
 def genKey(self) -> tuple:
     sk = random.randint(1, self._data['q']-1)
     pk = quick_power(inverse(self._data['g'], self._data['p']), sk, self._data['p'])
     return (sk, pk)