def main(): # 获取消息 cipher_message = receiver().split(',') des_cipher = cipher_message[0] des_key_encrypted = cipher_message[1] signed_hash = cipher_message[2] # 获取rsa公钥、私钥 rsa_d = int(read_message('./receiver/rsa_d.txt'), 16) rsa_n = int(read_message('./receiver/rsa_n.txt'), 16) rsa_e = int(read_message('./receiver/rsa_e.txt'), 16) # 解密消息签名 hash_decrypted = rsa.rsa_decrypt(signed_hash, rsa_e, rsa_n) # 验证消息签名 if hash_decrypted == sha1.sha1(des_cipher): print('\n消息签名验证成功,加密数据完整!') else: print('\n消息签名验证失败,加密数据不完整!') # 解密对称密钥 des_key = rsa.rsa_decrypt(des_key_encrypted, rsa_d, rsa_n) # 解密密文 msg = des.ECB_decrypt(des_cipher, des_key) # 将16进制密文转化为ASCII码 msg = binascii.unhexlify(msg).decode() print('\nDecrypted messages: \n' + msg)
def query(self, rsa_blob): '''Takes RSA ciphertext and returns plaintext. If the ciphertext has been queried, returns nothing''' #cannot submit same message to oracle twice if rsa_blob in self._seen: return return rsa.rsa_decrypt(rsa_blob, self._d, self._n)
def test_rsa(self): n, p, q, e, d = rsa.rsa_gen_keys() m = random.randint(1, n - 1) em = rsa.rsa_encrypt(n, e, m) dm = rsa.rsa_decrypt(n, d, em) self.assertEqual(m, dm)
def decrypt_display(self): self.cipher = self.encryptOutputField.get() if self.cipher == "": self.displayText["text"] = "No cipher!" else: timeDecrypt, decrypted = rsa_decrypt(int(self.d), int(self.p), int(self.q), int(self.cipher)) self.decryptOutputField.delete(0, END) self.decryptOutputField.insert(0, decrypted) self.displayText["text"] = "Decrypt success ! Cost Time : " + str( timeDecrypt) + " seconds."
def main(): (n, e, N) = read_key_file(PUBKEY_FILE) (n, d, N) = read_key_file(PRIVKEY_FILE) pt = "hello world".encode("utf-8") # do the thing ct = rsa_encrypt(pt, n, (e, N)) nt = rsa_decrypt(ct, n, (d, N)) # how did we do? print("plaintext in:") hexdump(pt, 32) print("ciphertext (padded):") hexdump(ct, 32) print("plaintext out:") hexdump(nt, 32)
def test_rsa(self): # 1. Generating key pair public_key, private_key = rsa.generate_rsa_key_pair(13, 17) # 2. Open file 'rsa_to_enc.txt' & read text file_to_enc = open('resources/lab_3/rsa_to_enc.txt', 'r') normal_text = file_to_enc.read() # 3. Encrypt read text encrypted_text = rsa.rsa_encrypt(public_key, normal_text) print(''.join(map(lambda x: str(x), encrypted_text))) # 4. Encrypt decrypted text decrypted_text = rsa.rsa_decrypt(private_key, encrypted_text) # 5. Save decrypted text to 'decrypted.txt' decrypted_file = open('resources/lab_3/rsa_decrypted.txt', 'w') decrypted_file.write(decrypted_text) self.assertEqual(normal_text, decrypted_text)
def unlock_file(cipherfile, rsa_private_key, rsa_public_key): """Unlock archive. This function unlock an archive `cipherfile` using RSA private key `rsa_private_key` and RSA public key `rsa_public_key`. :parameter: cipherfile : string Name of the archive to unlock. rsa_private_key : string RSA private key rsa_public_key : string RSA public key """ try: starttime = time.time() # Importe RSA key from PEM rsa_private_key = RSA.importKey(open(rsa_private_key).read()) rsa_public_key = RSA.importKey(open(rsa_public_key).read()) # Extract encrypted tar and signature tar = tarfile.open(cipherfile) for file in tar.getnames(): tar.extract(file) tar.close() # Verification of the payload archive_data = open('encrypted_files_and_key.lkd.sign', mode='rb') raw_signature = archive_data.read() archive_data.close() archive_data = open('encrypted_files_and_key.lkd', mode='rb') raw_files = archive_data.read() archive_data.close() if not rsa.rsa_verify_sign(rsa_public_key, raw_signature, raw_files): print("[warning] This file has been corrupted ! Don't use it !") # clean tar filed files_to_delete = ["encrypted_files_and_key.lkd.sign", "encrypted_files_and_key.lkd"] for eachfile in files_to_delete: if os.path.isfile(eachfile): tools.secure_delete(eachfile, passes=1) else: print("[info] This file is authentic !") # Extract encrypted files and symetric key tar = tarfile.open('encrypted_files_and_key.lkd') for eachfile in tar.getnames(): tar.extract(eachfile) tar.close() # Decryption of the keys archive_data = open('cipherkey.lkd', mode='rb') raw = archive_data.read() archive_data.close() aes_key = rsa.rsa_decrypt(rsa_private_key, raw) # Decrypt files archive_data = open('encrypted_files.lkd', mode='rb') raw_files = archive_data.read() archive_data.close() bytestar = io.BytesIO( str(aes.aes_decrypt(AES_BLOCK_SIZE, aes_key, raw_files))) # Untar files tar = tarfile.open(fileobj=bytestar) for eachfile in tar.getnames(): tar.extract(eachfile) # Delete container related files files_to_delete = ["cipherkey.lkd", "encrypted_files_and_key.lkd", "encrypted_files_and_key.lkd.sign", "encrypted_files.lkd", cipherfile] for eachfile in files_to_delete: tools.secure_delete(eachfile, passes=1) endtime = time.time() elapsedtime = endtime - starttime print("[info] The files have been successfuly " "unlocked in %f seconds." % elapsedtime) except AttributeError: print('[error] A method was called with a false attribute.') sys.exit() except: print('[error] An unexpected error occurred when unlocking files.') files_to_delete = ["cipherkey.lkd", "encrypted_files_and_key.lkd", "encrypted_files_and_key.lkd.sign", "encrypted_files.lkd"] for eachfile in files_to_delete: if os.path.isfile(eachfile): tools.secure_delete(eachfile, passes=1) sys.exit()
def test_create_keypair(): keypair = RSAKeypair.create(e=3) assert rsa_decrypt(rsa_encrypt("bob", keypair), keypair) == "bob"
def fast_oracle(c, d, n, l, u): p = rsa.rsa_decrypt(c, d, n) return l <= p and p < u if p[0] != '\x02': return False
def padding_oracle(c, d, n, mod_len): '''checks if PKCS #1v1.5 padding is of decryption is correct''' p = util.bigint_to_bytes(rsa.rsa_decrypt(c, d, n)) #pad start with 0s so it has same length as modulus p = '\x00' * (mod_len - len(p)) + p return pkcs1.pkcs1_unpad(p, mod_len) != None
def test_check_signature(d, e, n): msg = "hello Bob" padded = pkcs1_pad(msg) sig = rsa.rsa_decrypt(padded, d, n) assert (check_signature(msg, sig, e, n)) assert (not check_signature("hello Alice", sig, e, n))
def oracle(ct, d, n): '''checks if parity of plaintext is even''' return rsa.rsa_decrypt(ct, d, n) % 2 == 0