def run_instance(pub_id, p, q, bits): p = int(p, 16) q = int(q, 16) m = p * q a = id_val(pub_id, m) r = PKG(a, m, p, q) d_bits = decrypt(bits, r, m) print("Public id value = {}".format(fc.int2hex(a))) print("Private key: {}".format(fc.int2hex(r))) print("Decrypted bits: {}".format(d_bits)) return fc.int2hex(r), d_bits
def encode_length(V): nbr = int(V.zfill(1), 16) nbr_bytes = 1 if nbr==0 else len(V) // 2 if nbr_bytes <= 127: return fc.int2hex(nbr_bytes).zfill(2) # Make sure one byte else: if nbr_bytes <= 255: return "81" + fc.int2hex(nbr_bytes).zfill(2) # Use one byte to encode length elif nbr_bytes <= 65535: hex_nbr_bytes = fc.int2hex(nbr_bytes).zfill(4) #Encode as 4 bytes return "82" + hex_nbr_bytes else: print("*** NOT IMPLEMENTED ***") sys.exit()
def encode_integer_value(nbr): # Will be even hex => bytes hex_nbr = fc.int2hex(nbr) # Check if msb 1 if(bin(int(hex_nbr[:1], 16))[2:].zfill(4)[0] == "1"): hex_nbr = "00" + hex_nbr # Padd with zero byte return hex_nbr
def test2(self): p = int("9240633d434a8b71a013b5b00513323f", 16) q = int("f870cfcd47e6d5a0598fc1eb7e999d1b", 16) m = p * q a = main.id_val("*****@*****.**", m) r = fc.int2hex(main.PKG(a, m, p, q)) self.assertEqual( "814a8c2282ca8f4d0f2b2b72dfeeee6e5e3d8f438c039bdb5d059550739fdcec", r)
def der_encode(nbr): T = "02" V = fc.int2hex(nbr) if bin(int(V[:1], 16))[2:].zfill(4)[0] == "1": V = "00" + V nbr_bytes = 1 if nbr == 0 else int(len(V) / 2) if nbr_bytes > 257: #up to 2048 bit numbers, > 257 to allow padding print("too large number") sys.exit() if nbr_bytes > 127: if nbr_bytes >= 256: L = "8201" + fc.int2hex(nbr_bytes - 256).zfill(2) else: L = "81" + fc.int2hex(nbr_bytes).zfill(2) else: L = fc.int2hex(nbr_bytes).zfill(2) ##should return hex vector return str(T + L + V)
def OAEP_encode(M, seed, L=""): k = 128 #Enligt uppgiften 1024 bit rsa. K är längd i octets/bytes, dvs 1024/8 = 128 lHash = hashlib.sha1(L.encode('utf-8')).hexdigest() hLen = int(len(lHash) / 2) mLen = int(len(M) / 2) #length in octets is / 2 if len(L) / 2 > 2**61 - 1: print("label too long") sys.exit() if mLen > k - (2 * hLen) - 2: print("message too long") sys.exit() PS = "00" * (k - mLen - (2 * hLen) - 2) DB = lHash + PS + fc.int2hex(1) + M dbMask = MGF1(seed, k - hLen - 1) maskedDB = '{:x}'.format(int(DB, 16) ^ int(dbMask, 16)).zfill(hLen * 2) seedMask = MGF1(maskedDB, hLen) maskedSeed = '{:x}'.format(int(seed, 16) ^ int(seedMask, 16)).zfill(hLen * 2) return fc.int2hex(0) + maskedSeed + maskedDB
def encode_key(p, q, e): version = 0 n = p * q d = modinv(e, (p - 1) * (q - 1)) exp1 = d % (p - 1) #FEL exp2 = d % (q - 1) #FEL coef = modinv(q, p) #CORRECT pem = str( der_encode(version) + der_encode(n) + der_encode(e) + der_encode(d) + der_encode(p) + der_encode(q) + der_encode(exp1) + der_encode(exp2) + der_encode(coef)) L = int(len(pem) / 2) seq = "30" if L > 127: seq = seq + "81" + fc.int2hex(L).zfill( 2) if L < 256 else seq + "82" + fc.int2hex(L).zfill(4) else: seq = seq + fc.int2hex(L).zfill(2) print(seq) pem = base64.b64encode(fc.hex_to_bytearray(seq + pem)) return str(pem)[2:][:len(pem)]
def test1(self): pub_id = "*****@*****.**" p = int("9240633d434a8b71a013b5b00513323f", 16) q = int("f870cfcd47e6d5a0598fc1eb7e999d1b", 16) a = fc.int2hex(main.id_val(pub_id, p * q)) self.assertEqual("25a4d152bf555e0f61fb94ac4ee60962decbbe99", a)