def generate_random_key(self): if State.encrypt_method == 'AES': State.generated_key = get_random_bytes(16) elif State.encrypt_method == 'Triple DES': while True: try: State.generated_key = DES3.adjust_key_parity( get_random_bytes(24)) break except ValueError: pass elif State.encrypt_method == 'Salsa20': State.generated_key = get_random_bytes(32) key_filename = filedialog.asksaveasfilename( title="Save key", defaultextension='key', filetypes=CONFIG["DEFAULT_FILETYPES_SECURE"]) if key_filename and State.generated_key: State.generated_key_filename = key_filename self.frame_encrypt.key_label.config( text= f'Key: {get_filename_from_path(State.generated_key_filename)}') with open(key_filename, 'wb') as f: f.write(State.generated_key) messagebox.showinfo( title='Success', message= 'Key was saved successfully. You can now encrypt your file.' ) else: messagebox.showwarning( title='Empty key', message='You did not choose right path for key')
def test_parity_option3(self): before_3k = unhexlify( "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC") after_3k = DES3.adjust_key_parity(before_3k) self.assertEqual( after_3k, unhexlify("ABABABABABABABABBABABABABABABABACDCDCDCDCDCDCDCD"))
def run_3DES(): while True: try: key = DES3.adjust_key_parity(Random.get_random_bytes(24)) break except ValueError: pass nonce = Random.get_random_bytes(16) print("=================") print("Encrypting with 3DES") print("Key: ", to_hex(key)) print("Nonce: ", to_hex(nonce)) cipher = DES3.new(key, DES3.MODE_EAX, nonce) ciphertext = cipher.encrypt(data) #print("Cleartext: ", to_hex(data)) #print("Ciphertext: ", to_hex(ciphertext)) file_out = open("des3.bin", "wb") [file_out.write(x) for x in (cipher.nonce, ciphertext)] file_out.close() file_in = open("des3.bin", "rb") decrypt_nonce, decrypt_ciphertext = [file_in.read(x) for x in (16, -1)] file_in.close() print("=================") print("Decrypting with 3DES") print("Key: ", to_hex(key)) print("Nonce: ", to_hex(decrypt_nonce)) #print("Ciphertext: ", to_hex(decrypt_ciphertext)) decipher = DES3.new(key, DES3.MODE_EAX, decrypt_nonce) cleartext = decipher.decrypt(decrypt_ciphertext) #print("Cleartext: ", to_hex(cleartext)) print("=================") if cleartext == data: print("Decryption successful\n") else: print("Decryption failed\n") return
def btn_gen_key(self): # Проверить выбран ли алгоритм alg = self.cur_alg.get() if not alg: messagebox.showerror("Ошибка", "Выберите алгоритм из списка") return #alg = self.algs[alg] # Сгенерировать ключ для алгоритма if alg == "3DES": while True: try: key = DES3.adjust_key_parity(get_random_bytes(24)) break except ValueError: pass else: key = get_random_bytes(self.algs[alg]["key"]) # Сохранить ключ with open("key", "wb") as f: f.write(key) self.btn_key.action("key")
def pycryptodomexExamples(): from Cryptodome.Cipher import DES, DES3, ARC2, ARC4, Blowfish, AES from Cryptodome.Random import get_random_bytes key = b'-8B key-' DES.new(key, DES.MODE_OFB) # Noncompliant {{Use a strong cipher algorithm.}} # ^^^^^^^ key = DES3.adjust_key_parity(get_random_bytes(24)) cipher = DES3.new( key, DES3.MODE_CFB) # Noncompliant {{Use a strong cipher algorithm.}} # ^^^^^^^^ key = b'Sixteen byte key' cipher = ARC2.new( key, ARC2.MODE_CFB) # Noncompliant {{Use a strong cipher algorithm.}} # ^^^^^^^^ key = b'Very long and confidential key' cipher = ARC4.new(key) # Noncompliant {{Use a strong cipher algorithm.}} # ^^^^^^^^ key = b'An arbitrarily long key' cipher = Blowfish.new( key, Blowfish.MODE_CBC) # Noncompliant {{Use a strong cipher algorithm.}} # ^^^^^^^^^^^^ key = b'Sixteen byte key' cipher = AES.new(key, AES.MODE_CCM) # Compliant cipher = UnknownFlyingValue.new( key, UnknownMode.CBC) # Compliant, doesn't matter # Force the engine to generate an ambiguous symbol, for code coverage only. ambiguous = "" if 42 * 42 < 1700 else (lambda x: x * x) cipher = ambiguous.new(key, Unknown.Mode)
def pycroptodomeExamples(): from Crypto.Cipher import DES, DES3, ARC2, ARC4, Blowfish, AES from Crypto.Random import get_random_bytes key = b'-8B key-' DES.new(key, DES.MODE_OFB) # Noncompliant {{Use a strong cipher algorithm.}} # ^^^^^^^ key = DES3.adjust_key_parity(get_random_bytes(24)) cipher = DES3.new( key, DES3.MODE_CFB) # Noncompliant {{Use a strong cipher algorithm.}} # ^^^^^^^^ key = b'Sixteen byte key' cipher = ARC2.new( key, ARC2.MODE_CFB) # Noncompliant {{Use a strong cipher algorithm.}} # ^^^^^^^^ key = b'Very long and confidential key' cipher = ARC4.new(key) # Noncompliant {{Use a strong cipher algorithm.}} # ^^^^^^^^ key = b'An arbitrarily long key' cipher = Blowfish.new( key, Blowfish.MODE_CBC) # Noncompliant {{Use a strong cipher algorithm.}}
import time import generateFiles import out_file from encryption import des3, des, AEScbc, AESctr, AESecb, rc4 from classes import Test from Cryptodome.Random import get_random_bytes from Cryptodome.Cipher import DES3 from generateFiles import generateRandomString desKey = generateRandomString(8) key128bit = generateRandomString(16) while True: try: des3parityKey = DES3.adjust_key_parity( generateRandomString(16).encode("utf-8")).decode("utf-8") break except ValueError: pass print('Setting up Test') out_file.clear() algorithms = [ Test("DES", des, desKey, "none", []), Test("DES3", des3, des3parityKey, "none", []), Test("AES_CBC", AEScbc, key128bit, "none", []), Test("AES_CTR", AESctr, key128bit, "none", []), Test("AES_ECB", AESecb, key128bit, "none", []), Test("RC4", rc4, key128bit, "none", []) ]
def test_parity_option3(self): before_3k = unhexlify("AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC") after_3k = DES3.adjust_key_parity(before_3k) self.assertEqual(after_3k, unhexlify("ABABABABABABABABBABABABABABABABACDCDCDCDCDCDCDCD"))
def test_parity_option2(self): before_2k = unhexlify("CABF326FA56734324FFCCABCDEFACABF") after_2k = DES3.adjust_key_parity(before_2k) self.assertEqual(after_2k, unhexlify("CBBF326EA46734324FFDCBBCDFFBCBBF"))
#!/usr/bin/python3 import argparse import Cryptodome.Cipher.DES3 as DES3 import Cryptodome.Random as Random #data = b'This is the first message that I have encrypted using PyCryptodome!!' data = Random.get_random_bytes(16777216) while True: try: key = DES3.adjust_key_parity(Random.get_random_bytes(24)) break except ValueError: pass nonce = Random.get_random_bytes(16) def run_3DES(num): for i in list(range(num)): cipher = DES3.new(key, DES3.MODE_EAX, nonce) ciphertext = cipher.encrypt(data) return def main(): num = 1 parser = argparse.ArgumentParser() parser.add_argument("--num",
def DES3_generation(): key = DES3.adjust_key_parity(get_random_bytes(24)) return key