def decrypt(cipher, key): a, b = key message = [] a_inv = mod_inv(a, N) for block in split(cipher): message.append(int2block((a_inv * (block2int(block) - b)) % N)) return "".join(message)
def decrypt(ciphertext, key): a, b = key message = [] a_inv = mod_inv(a, m) for c in ciphertext: message.append(alphabet[a_inv * (alphabet.index(c) - b) % m]) return "".join(message)
def decrypt(cipher, key): a, b = key a_inv = mod_inv(a, m) message = [] for (i, char) in enumerate(cipher[1:]): x = a_inv * (alphabet.index(char) - b) % m c = (x - alphabet.index(cipher[i])) % m message.append(alphabet[c]) return "".join(message)
def rsa_crt(c, d, p, q, verbose): #d_p = mod_inv(e, p-1, verbose) #d_q = mod_inv(e, q-1, verbose) d_p = d % (p-1) d_q = d % (q-1) q_inv = utils.mod_inv(q, p, verbose) #m_p = pow(c, d_p) % p m_p = utils.fast_exp(c, d_p, p, 0) #m_q = pow(c, d_q) % q m_q = utils.fast_exp(c, d_q, q, 0) h = q_inv * (m_p - m_q) % p m = m_q + h * q if(verbose != 0): print('Message was '+str(m)) return m
def decrypt(cipher, a, b): message = [] for c in cipher: message.append(alphabet[mod_inv(a, m) * (alphabet.index(c) - b) % m]) return "".join(message)
def decrypt(cipher, a, b): message = [] for block in split(cipher): message.append(int2block((mod_inv(a, N) * (block2int(block) - b)) % N)) return "".join(message)
from utils import mod_inv z1 = 78963682628359021178354263774457319969002651313568557216154777320971976772376 s1 = 5416854926380100427833180746305766840425542218870878667299 r1 = 5568285309948811794296918647045908208072077338037998537885 z2 = 62159883521253885305257821420764054581335542629545274203255594975380151338879 s2 = 1063435989394679868923901244364688588218477569545628548100 r2 = 5568285309948811794296918647045908208072077338037998537885 n = 6277101735386680763835789423176059013767194773182842284081 # s_1 - s_2 = k^{-1} (H(m_1) - H(m_2)) mod q k = (z1 - z2) * mod_inv(s1 - s2, n) % n print(hex(k)[2:])
def hex_to_int(h): return int(h.translate({ord(k): "" for k in ": \n"}), 16) H = sha256 S1 = (b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 35623234345770674178461511775741429919697286385779853734750439026778433274062, 45539125425907527650281204451874392252555815280529934540724889833951752886498) S2 = (b"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", 35623234345770674178461511775741429919697286385779853734750439026778433274062, 19012499976858645754384863291076389351600771465090039299838961759746601923026) m1, r1, s1 = S1 m2, r2, s2 = S2 assert r1 == r2 # the two signatures were generated with the same nonce h = lambda x: int.from_bytes(H(x).digest(), byteorder="big") # s_1 - s_2 = k^{-1} (H(m_1) - H(m_2)) mod q k = (h(m1) - h(m2)) * mod_inv(s1 - s2, q) % q r = pow(g, k, p) % q assert r == r1 == r2 # s = k^{-1} (H(m) + xr) mod q x = (k * s1 - h(m1)) * mod_inv(r, q) % q assert pub == pow(g, x, p) priv = hex(x)[2:] print(priv)
H = sha256 S1 = ( b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 35623234345770674178461511775741429919697286385779853734750439026778433274062, 45539125425907527650281204451874392252555815280529934540724889833951752886498 ) S2 = ( b"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", 35623234345770674178461511775741429919697286385779853734750439026778433274062, 19012499976858645754384863291076389351600771465090039299838961759746601923026 ) m1, r1, s1 = S1 m2, r2, s2 = S2 assert r1 == r2 # the two signatures were generated with the same nonce h = lambda x: int.from_bytes(H(x).digest(), byteorder="big") # s_1 - s_2 = k^{-1} (H(m_1) - H(m_2)) mod q k = (h(m1) - h(m2)) * mod_inv(s1 - s2, q) % q r = pow(g, k, p) % q assert r == r1 == r2 # s = k^{-1} (H(m) + xr) mod q x = (k * s1 - h(m1)) * mod_inv(r, q) % q assert pub == pow(g, x, p) priv = hex(x)[2:] print(priv)
message = 0xf5ed9da29d8d260f22657e091f34eb930bc42f26f1e023f863ba13bee39071d1ea988ca62b9ad59d4f234fa7d682e22ce3194bbe5b801df3bd976db06b944da def openssl_modulus(key): """Key is an openssl key, return the RSA modulus as int.""" out = run("echo '%s' | openssl rsa -pubin -text -noout" % key, shell=True, stdout=subprocess.PIPE).stdout out = out.decode("utf-8") a = out.find("Modulus:") + len("Modulus:") # parse it out... b = out.find("Exponent") out = out[a:b] out = out.replace(' ', '').replace('\n', '').replace(':', '') N = int(out, 16) return N # RSA, pk = (N, e) # Enc: c := [m^e mod N] # Dec: m := [c^d mod N] N1 = openssl_modulus(key1) N2 = openssl_modulus(key2) p = gcd(N1, N2) q1 = N1 // p q2 = N2 // p e = 65537 phi = (p - 1) * (q1 - 1) d = mod_inv(e, phi) m = pow(message, d, N1) solution = hex(m)[2:] print(solution)
def _mul_inv(x): """Multiplicative inverse in the multiplicative group (mod 2**16 + 1)""" return mod_inv(x, MOD + 1)