def rsa_signing(p, q, m): n = p * q pi = (p - 1) * (q - 1) e = get_coprime(pi) d = exeu(e, pi) s = mod(m, d, n) v = mod(s, e, n) print(f"{m} ?= {v}(s = {s})")
def generate(self, m, k): # self.k = random.randint(1, self.p - 1) self.k = k self.r = mod(self.g, self.k, self.p) self.ki = exeu(self.k, self.p - 1) self.s = ((m - self.x * self.r) * self.ki) % (self.p - 1) print(f"generate: r = {self.r}, s = {self.s}, k = {self.k}") return self.r, self.s
def __init__(self, p, q): self.p = p self.q = q self.n = p * q self.pi = (p - 1) * (q - 1) self.d = get_coprime(self.pi) self.e = exeu(self.d, self.pi) print(f"Public key: (e, n) = ({self.e}, {self.n})") print(f"Private key: (d) = ({self.d})")
def __init__(self, p, q, m): self.p = p self.q = q self.m = m self.n = p * q self.pi = (p - 1) * (q - 1) self.e = get_coprime(self.pi) self.d = exeu(self.e, self.pi) print(f"(n, pi) = ({self.n}, {self.pi})") print(f"(e, d) = ({self.e}, {self.d})")
def decrypt(self, c1, c2): self.m = (c2 * mod(exeu(c1, self.p), self.x, self.p)) % self.p print(f"Decrypt: M = {self.m}") return self.m
def diff(self, xp, yp, xq, yq): s = ((yp - yq) * exeu(xp - xq, self.p)) % self.p x = ((s**2) - (xp + xq)) % self.p y = ((s * (xp - x)) - yp) % self.p return x, y
def same(self, x, y): s = (((3 * (x**2)) + self.a) * exeu(2 * y, self.p)) % self.p x2g = ((s**2) - (2 * x)) % self.p y2g = ((s * (x - x2g)) - y) % self.p return x2g, y2g
def unblinding(self, blind_s): self.s = (exeu(self.k, self.n) * blind_s) % self.n print(f"S = {self.s}") return self.s
def decrypt(self, c): res = [] for i in c: res.append(((i - self.k2 + 26) * exeu(self.k1, 26)) % 26) return res
def encrypt(self, m): res = [] for i in m: res.append(((i - self.k2 + 26) * exeu(self.k1, 26)) % 26) return res
self.p = p def encrypt(self, m): res = [] for i in range(len(m)): j = i % len(self.k) res.append((m[i] + self.k[j]) % self.p) return res def decrypt(self, c): res = [] for i in range(len(c)): j = i % len(self.k) res.append((c[i] - self.k[j]) % self.p) return res if __name__ == "__main__": enc = str_to_list("wegvrjepqnwezbdzqetgzbigvpta") affine = Affine(exeu(5, 26), 13) res = affine.decrypt(enc) res = list_to_str(res) print(res) key = intstr_to_list("20170317") vigenere = Vigenere(key, 10) m = intstr_to_list("314159265358979323846264") res = vigenere.encrypt(m) res = list_to_intstr(res) print(res)