コード例 #1
0
def main():
    with open('1.2.4_ciphertext.enc.asc') as c:
        ciphertext = c.read()
    with open('moduli.hex') as n:
        moduli = []
        for line in n:
            moduli.append(int(line, 16))

    P = batchgcd_faster(moduli)
    #print(P)
    for i in range(len(P)):
        if P[i] == 1:
            pass
        else:
            q = moduli[i] / P[i]
            totient = (P[i] - 1) * (q - 1)
            e = 65537L
            d = mulinv(e, totient)
            N = moduli[i]
            try:
                private_key = RSA.construct((N, e, d))
                plaintext = pbp.decrypt(private_key, ciphertext)
                outputfile = open('sol_1.2.4.txt', 'w')
                outputfile.write(plaintext)
                outputfile.close()
            except:
                #print 'Wrong private key!'
                pass
コード例 #2
0
ファイル: sol_3.2.4.py プロジェクト: KeyeZhang/ece422
def print_results(private_keys, cipher_content, out):
    for key in private_keys:
        try:
            plaintext = pbp.decrypt(key, cipher_content)
            print(plaintext)
            out.write(plaintext)
        except ValueError:
            a = 1
コード例 #3
0
    moduli = read_from_file(mod_filename)
    moduli = moduli.splitlines()
    moduli = [int(moduli[i], 16) for i in range(0, len(moduli))]

    working_window = moduli
    product = np.product(working_window)
    # mining p and q
    # following the Algorithm in Section 3.3
    for i in range(0, len(working_window)):
        ni2 = np.power(moduli[i], 2)
        zi = product % ni2
        p = gcd(moduli[i], zi / moduli[i])
        if p == 1:
            continue
        q = moduli[i] / p
        try:
            private_d = modinv(public_e, (p - 1) * (q - 1))
        except ValueError:
            continue
        tup = (moduli[i], public_e, private_d, p, q)
        rsakey = RSA.construct(tup)
        try:
            plaintext = decrypt(rsakey, ciphertext)
            print i, 'Correct RSA Key!'
            with open(output_file, 'w') as f:
                f.write(plaintext)
            break
        except ValueError:
            print i, 'Wrong RSA Key'
コード例 #4
0
    return u1 % m


for i in range(len(N)):
    if (gcds[i] == 1): continue
    n = (N[i])
    p = (gcds[i])
    q = (n // p)
    e = 65537
    taut = (p - 1) * (q - 1)
    d = (invmod(int(e), int(taut)))
    '''
    print(f'{n=}')
    print(f'{p=}')
    print(f'{q=}')
    print(f'{taut=}')
    assert(np.gcd(n,p) == p)
    assert(np.gcd(n,q) == q)
    assert(n==p*q)
    assert(is_prime(p))
    assert(is_prime(q))
    print(f'{d=}')
    '''
    assert (np.mod(d * e, taut) == 1)
    key = RSA.construct((n, e, d))
    try:
        dec = pbp.decrypt(key, enc)
        print(dec)
    except:
        pass
コード例 #5
0
remainder_list = batchgcd_simple(mod_list)


for i in range(len(remainder_list)):
    p = remainder_list[i]
    if p == 1:
            continue
    q = mod_list[i] / p
    try:
            d = modinv(e, (p-1)*(q-1))
    except ValueError:
            continue
    tup = (mod_list[i],e,d)
    key = RSA.construct(tup)
    try:
        plaintext = decrypt(key,ciphertext)
        print "Correct Key"
        print(plaintext)
        break
    except ValueError:
        print("exception")
        continue
#

# d_list = []
# for i in range(len(gcd_list)):
#     p = gcd_list[i]
#     q = modulus_list[i]/gcd_list[i]
#     m1 = (p-1)*(q-1)
#     temp = number.inverse(e,m1)
#     d_list.append(temp)
コード例 #6
0
ファイル: test_pbp.py プロジェクト: stef/pbp
 def test_encrypt_sym(self):
     encrypted = pbp.encrypt(MESSAGE.encode('utf-8'), pwd=PASSWORD)
     decrypted = pbp.decrypt(encrypted, pwd=PASSWORD)
     self.assertEquals(decrypted, MESSAGE.encode('utf-8'))
コード例 #7
0
ファイル: test_pbp.py プロジェクト: stef/pbp
 def test_encrypt_sym_pwprompt_fail(self):
     encrypted = pbp.encrypt(MESSAGE.encode('utf-8'), pwd=OTHER_PW)
     self.assertTrue(pbp.decrypt(encrypted, pwd='asdf') is None)
コード例 #8
0
    phi = []
    new_n = []
    for i in range(len(gcds)):
        if gcds[i] != 1:
            new_n.append(n[i])
            phi.append((gcds[i] - 1) * ((n[i] / gcds[i]) - 1))
    d = []

    for i in range(len(phi)):
        g, x, y = xgcd(long(65537), phi[i])
        x = x % phi[i]
        if (g != 1):
            print "error"
        else:
            d.append(x)

    encrypted_file = open("1.2.4_ciphertext.enc.asc")
    msg = encrypted_file.read()
    out_file = open("sol_1.2.4.txt", 'w')

    for i in range(len(d)):
        RSAkey = RSA.construct((new_n[i], long(65537), d[i]))
        try:
            result = pbp.decrypt(RSAkey, msg)
            out_file.write(result)
        except:
            print "key " + str(i) + " is not correct"

    out_file.close()
    encrypted_file.close()
コード例 #9
0
ファイル: test_pbp.py プロジェクト: adelq/pbp
 def test_encrypt_sym_pwprompt_fail(self):
     encrypted = pbp.encrypt(MESSAGE, pwd=OTHER_PW)
     self.assertTrue(pbp.decrypt(encrypted, pwd='asdf') is None)
コード例 #10
0
ファイル: test_pbp.py プロジェクト: dnet/pbp
 def test_encrypt_sym_pwprompt(self):
     encrypted = pbp.encrypt(MESSAGE, pwd=PASSWORD)
     decrypted = pbp.decrypt(encrypted, basedir=self.pbp_path)
     self.assertEquals(decrypted, MESSAGE)
コード例 #11
0
ファイル: test_pbp.py プロジェクト: dnet/pbp
 def test_encrypt_sym_fail(self):
     encrypted = pbp.encrypt(MESSAGE, pwd=OTHER_PW)
     with self.assertRaises(ValueError):
         pbp.decrypt(encrypted, pwd=PASSWORD, basedir=self.pbp_path)
コード例 #12
0
ファイル: test_pbp.py プロジェクト: dnet/pbp
 def test_encrypt_sym_pwprompt_fail(self):
     encrypted = pbp.encrypt(MESSAGE, pwd=OTHER_PW)
     with self.assertRaises(ValueError):
         pbp.decrypt(encrypted, pwd='asdf')
コード例 #13
0
ファイル: sol_3.2.4.py プロジェクト: sujaypat/ece422
    print("Done computing remainder tree...")
    return remainders, div


def fastGCD(keys):
    r, d = rTree(pTree(keys))
    return [gcd(r / n, n) for r, n in zip(r, d)]


keys = []

with open('moduli.hex', 'r') as f:
    for line in f:
        keys.append(int(line.rstrip(), 16))

with open('3.2.4_ciphertext.enc.asc', 'r') as f:
    ciphertext = "".join(f.readlines())

gcds = fastGCD(keys)

print(len(gcds))

for i in range(len(gcds)):
    if gcds[i] != 1:
        d = findPrivateExponent(gcds[i], keys[i])
        key = RSA.construct((long(keys[i]), long(65537), long(d)))
        try:
            print pbp.decrypt(key, ciphertext)
        except ValueError:
            pass
コード例 #14
0
 def test_encrypt_sym_pwprompt(self):
     encrypted = pbp.encrypt(MESSAGE.encode('utf-8'), pwd=PASSWORD)
     decrypted = pbp.decrypt(encrypted)
     self.assertEquals(decrypted, MESSAGE.encode('utf-8'))
コード例 #15
0

def batchgcd(X):
    R = remainders(product(X), [n**2 for n in X])
    return [gcd(r / n, n) for r, n in zip(R, X)]


with open('moduli.hex') as f:
    lines = f.readlines()
    lines = [line.strip() for line in lines]
    moduli = [int(line, 16) for line in lines]

gcds = batchgcd(moduli)

with open('3.2.4_ciphertext.enc.asc') as f:
    encrypted = f.read()

e = 65537L
for gcd, modulu in zip(gcds, moduli):
    if gcd == 1:
        continue
    p = gcd
    q = modulu / p
    d = gmpy2.invert(e, (p - 1) * (q - 1))

    key = RSA.construct((modulu, e, long(d)))
    try:
        decrypted = decrypt(key, encrypted)
        print(decrypted)
    except ValueError:
        continue
コード例 #16
0
with open("moduli.hex") as moduli_file, open(
        "3.2.4_ciphertext.enc.asc") as cipher_file:
    cipher = cipher_file.read().replace("\r\n", "\n")
    moduli = moduli_file.readlines()
    for i in range(len(moduli)):
        moduli[i] = int(moduli[i].strip(), 16)

T = producttree(moduli)
prod = T[-1][0]
tmp = []
for i in range(len(T)):
    tmp.append([T[i][j]**2 for j in range(len(T[i]))])
T = tmp
remainder_list = remaindersusingproducttree(prod, T)
gcd_list = get_gcd(remainder_list, moduli)

with open("sol_3.2.4.txt", 'w') as output:
    e = long(65537)
    for i in range(len(gcd_list)):
        p = gcd_list[i][0]
        N = gcd_list[i][1]
        q = N // p
        d = modinv(e, (p - 1) * (q - 1))
        key = RSA.construct((long(N), long(e), long(d)))
        try:
            plain = pbp.decrypt(key, cipher)
            print(plain)
            output.write(plain)
        except ValueError:
            pass
コード例 #17
0

with open('moduli.hex', "r") as f:
    rsa_array = []
    for line in f:
        rsa_array.append(int(line, 16))
    f.close()

with open('3.2.4_ciphertext.enc.asc', "r") as f:
    ciphertext = f.read()
    f.close()

ptree = producttree(rsa_array)
print 'pdone'
rtree = remaindertree(ptree, rsa_array)
print 'rdone'

pub = long('65537')
for i in range(len(rtree)):
    if rtree[i] != 1:
        p = rtree[i]
        q = rsa_array[i] / p
        priv = modinv(pub, (p - 1) * (q - 1))

        try:
            key = RSA.construct((p * q, pub, priv))
            plaintext = pbp.decrypt(key, ciphertext)
            print plaintext
        except ValueError:
            continue
コード例 #18
0
ファイル: test_pbp.py プロジェクト: adelq/pbp
 def test_encrypt_sym_fail(self):
     encrypted = pbp.encrypt(MESSAGE, pwd=OTHER_PW)
     self.assertTrue(pbp.decrypt(encrypted, pwd=PASSWORD) is None)
コード例 #19
0
ファイル: test_pbp.py プロジェクト: adelq/pbp
 def test_encrypt_sym(self):
     encrypted = pbp.encrypt(MESSAGE, pwd=PASSWORD)
     decrypted = pbp.decrypt(encrypted, pwd=PASSWORD)
     self.assertEquals(decrypted, MESSAGE)
コード例 #20
0
ファイル: sol_3.2.4.py プロジェクト: steven112163/UIUC-CS461
    candidates = []
    for idx, prime in enumerate(a):
        if prime > 1:
            candidates.append([modulus[idx], (hex(prime).rstrip('L'))[2:]])

    plainText = ""
    for i in range(len(candidates)):
        n = int(candidates[i][0], 16)
        p = int(candidates[i][1].rstrip('L'), 16)
        q = n / p
        r = (p - 1) * (q - 1)
        e = 65537L
        d = 0L
        try:
            d = modular_inverse(e, r)
        except:
            continue

        key = RSA.construct((n, e, d, p, q))

        try:
            plainText = pbp.decrypt(key, cipherText)
            print(plainText)
        except:
            continue

    output = open('sol_3.2.4.txt', 'w')
    output.write(plainText)
    output.close()
コード例 #21
0
ファイル: test_pbp.py プロジェクト: stef/pbp
 def test_encrypt_sym_fail(self):
     encrypted = pbp.encrypt(MESSAGE.encode('utf-8'), pwd=OTHER_PW)
     self.assertTrue(pbp.decrypt(encrypted, pwd=PASSWORD) is None)
コード例 #22
0
 def test_encrypt_sym_pwprompt(self):
     encrypted = pbp.encrypt(MESSAGE, pwd=PASSWORD)
     decrypted = pbp.decrypt(encrypted)
     self.assertEquals(decrypted, MESSAGE)
コード例 #23
0
	long_to_parse.append(temp)

n=[]
for i in range(0,10000):
	n.append(long(long_to_parse[i],16))
	#print (long(long_to_parse[i],16))
#print productTree(long_parsed);
new = gcd_all(n)
#print "gcd ok!"
for i in range(0,len(new)):
	if long(new[i]) == 1L:
		continue
	else:
		q = n[i]/long(new[i])
		z = (q-1)*(long(new[i])-1)
		d = modinv(e,z)
		if i>=0:
			#print d
			
			key = RSA.construct((long(n[i]),long(e),long(d)))
			try:
				result = decrypt(key,ciphertext)
				localtime = time.asctime(time.localtime(time.time()))
				print localtime
				print result
				with open("sol_3.2.4.txt",'w') as o:
					o.write(result)			
				break
			except:
				continue
コード例 #24
0
ファイル: sol_1.2.4.py プロジェクト: ashkmr20/Security-1-UIUC
constructKeys=[]
for i in range(len(psandqs)):
	totient= gmpy.lcm(psandqs[i]-1,gcdarraywithoutones[i]-1)
	privkeys.append(gmpy.invert(exp,totient))

# thefile = open('privkeys.txt', 'w')
# for item in privkeys:
#  thefile.write("%s\n" % item)

for indexofindex in range(len(indexofgcd)):
	constructKeys.append([long(testdata[indexofgcd[indexofindex]]),long(exp),long(privkeys[indexofindex])])

for key in constructKeys:
	RSAkeys.append(RSA.construct(key))

for key in RSAkeys:
	try:
		result=	pbp.decrypt(key, open('1.2.4_ciphertext.enc.asc').read())
	except ValueError:
    		pass 
with open('sol_1.2.4.txt', "w") as outp:
    outp.write(result)
# for i in range(len(testdata)):
# 	gcdlist[i]=gcdlist[i]/testdata[i]
#print gcdlist

#with open('remlistfactored.txt','r') as g:
# 	gcdwoones=g.readlines()
# print(len(gcdwoones))
# gcdwoones = [x.strip() for x in gcdwoones] 
# gcdwoones = [int(x) for x in gcdwoones]