def decrypt(filename, private, p, q): decrypted = [] ## Open file and get the contents encrypted = openFile(filename, False) ## Using the chinese remainder theorem ## pCRT = C^d mod p ## qCRT = C^d mod q pCRT, qCRT = [], [] for block in encrypted: pCRT.append(pow(block, private[0], p)) qCRT.append(pow(block, private[0], q)) ## Geting bitvector versions of the p * q values for their multiplicative inverses pBV = BitVector(intVal=p) qBV = BitVector(intVal=q) ## Xp = q * (q^-1 mod p) ## Xq = p * (p^-1 mod q) pX = q * int(qBV.multiplicative_inverse(pBV)) qX = p * int(pBV.multiplicative_inverse(qBV)) ## C^d mod n = (VpXp + VqXq) mod n for i in range(len(encrypted)): decrypted.append(((pCRT[i] * pX) + (qCRT[i] * qX)) % private[1]) return decrypted
def GenerateKeys(eVAL): ## Setup the prime generator pg = PrimeGenerator(bits=128, debug=0) while (True): p = pg.findPrime() q = pg.findPrime() ## Check p and q are different if p == q: continue ## Check left two MSB's are 1 (bin command returns 0b appended at front) if not (bin(p)[2] and bin(p)[3] and bin(q)[2] and bin(q)[3]): continue ## Check that the totients of p and q are co-prime to e if (GCD(p - 1, eVAL) != 1) or (GCD(q - 1, eVAL) != 1): continue break ## Calculate modulus n = p * q ## Calculate totient of n tn = (p - 1) * (q - 1) modBV = BitVector(intVal=tn) eBV = BitVector(intVal=eVAL) ## Calculate multiplicative inverse d = eBV.multiplicative_inverse(modBV) d = int(d) ## Create public and private sets public, private = [eVAL, n], [d, n] ## Return items return public, private, p, q, eVAL
def generate_private_key_d(e, p, q): n = p * q phi = (p - 1) * (q - 1) bv_modulus = BitVector(intVal=phi) bv = BitVector(intVal=e) bv_result = bv.multiplicative_inverse(bv_modulus) d = int(bv_result) return d
def decrypt_chinese_remainder(cipher_blocks, e, p, q): d = generate_private_key_d(e, p, q) d_p = d % (p - 1) d_q = d % (q - 1) bv_modulus = BitVector(intVal=p) q_bv = BitVector(intVal=q) q_inv = int(q_bv.multiplicative_inverse(bv_modulus)) decrypted = [] for c in cipher_blocks: m1 = (c**d_p) % p m2 = (c**d_q) % q h = (q_inv * (m1 - m2)) % p m = m2 + (h * q) decrypted.append(m) return decrypted
# RSA block cipher import sys from BitVector import BitVector # P, Q generated using modified PrimeGenerator.py # PrimeGenerator.py was modified to generate prime-1 coprime e p = 332302551650268460897954734536272420319 q = 258222644969751264151459428110328891689 e = 65537 # d = 51045946630242965074234865575892110109352763218080182137347987188415402400657 bv_p = BitVector(intVal=p) bv_q = BitVector(intVal=q) bv_pi = bv_p.multiplicative_inverse(bv_q) bv_qi = bv_q.multiplicative_inverse(bv_p) pi = int(bv_pi) # p inverse mod q qi = int(bv_qi) # q inverse mod p n = p * q bv_n = BitVector(intVal=n) tn = (p - 1) * (q - 1) bv_tn = BitVector(intVal=tn) bv_e = BitVector(intVal=e) bv_d = bv_e.multiplicative_inverse(bv_tn) d = int(bv_d) def mod_exp(a, b, n): result = 1 while(b > 0):
# ECE 404 HW #6 # RSA block cipher import sys from BitVector import BitVector # P, Q generated using modified PrimeGenerator.py # PrimeGenerator.py was modified to generate prime-1 coprime e p = 332302551650268460897954734536272420319 q = 258222644969751264151459428110328891689 e = 65537 # d = 51045946630242965074234865575892110109352763218080182137347987188415402400657 bv_p = BitVector(intVal=p) bv_q = BitVector(intVal=q) bv_pi = bv_p.multiplicative_inverse(bv_q) bv_qi = bv_q.multiplicative_inverse(bv_p) pi = int(bv_pi) # p inverse mod q qi = int(bv_qi) # q inverse mod p n = p * q bv_n = BitVector(intVal=n) tn = (p - 1) * (q - 1) bv_tn = BitVector(intVal=tn) bv_e = BitVector(intVal=e) bv_d = bv_e.multiplicative_inverse(bv_tn) d = int(bv_d) def mod_exp(a, b, n): result = 1