p = psutil.Process(os.getpid()) proc_list = p.cpu_affinity() p.cpu_affinity([proc_list[-1]]) # Perform several encryption / decryption operations random_iv = bytearray(os.urandom(16)) random_key = bytearray(os.urandom(16)) data = bytearray(list(range(256))) data1 = data[:151] data2 = data[151:] # Note: __PROFILE_AES__ must be defined when building the native # module in order for the print statements below to work aes_ctr = AES(mode='ctr', key=random_key, iv=random_iv) result = aes_ctr.encrypt(data1) if result: print('Encrypted data1 in: %5d cycles' % result) result = aes_ctr.encrypt(data2) if result: print('Encrypted data2 in: %5d cycles' % result) data_new = data1 + data2 aes_ctr = AES(mode='ctr', key=random_key, iv=random_iv) result = aes_ctr.decrypt(data_new) if result: print('Decrypted data in: %5d cycles' % result) assert data == data_new, "The demo has failed."
def encrypt_data(plaintext, master_key): AESfunct = AES(master_key) encrypted = AES.encrypt(AESfunct, plaintext) return encrypted
def decrypt_data(ciphertext, master_key): AESfunct = AES(master_key) decrypted = AES.decrypt(AESfunct, ciphertext) return decrypted
def __init__(self, verbose=True): self.VERBOSE = verbose self.my_aes = AES()
from Arytmetyka import Arytmetyka from PrimeNumberGenerator import PrimeNumberGenerator from aes import AES x = 69017099569831250560832963150376437645133154616280229282066864016913299192967373975974831584150053337515088624997114590154194762430279980129131488430577361201004666775725438052779996726942967878531367335142952137219372198784721987921 y = 382901830921830718713621790372180396476213820173980217309281731648720389218321832918302100154138092183902189031289038921083921839021803921730921731478213702183902183921083021830912809312809 m = 69017099569831250560832963150376437645133154616280229282066864016913299192967373975974831584150053337515088624997114590154194762430279980129131488430577361201004666775725438052779996726942967878531367335142952143636431261619027793877014515217929147780926047591594823548628941753213191937582400215035077463393 png = PrimeNumberGenerator() aes = AES() aes.prepare_encrypt('CBC') aes.encrypt_image('eiti.jpg', 'eiti_ecb.bmp', 'eiti_data', 'eiti_key_ecb', 'eiti_iv') aes.prepare_decrypt('CBC', 'eiti_key_ecb', 'eiti_iv') aes.decrypt_image('eiti_data', 'eiti_ecb_decyrpt.jpg') aes.prepare_encrypt('CBC') aes.encrypt_txt('message.txt', 'encrypted_message.txt', 'message_key', 'message_iv') aes.prepare_decrypt('CBC', 'message_key', 'message_iv') aes.decrypt_txt('encrypted_message.txt', 'decrypted_message.txt')
def setUp(self): master_key = 0x2b7e151628aed2a6abf7158809cf4f3c self.AES = AES(master_key, "AES-128")
def main(): parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group(required=True) group.add_argument("-e", "--encrypt", action="store_true") group.add_argument("-d", "--decrypt", action="store_true") parser.add_argument("input_file") parser.add_argument("output_file") args = parser.parse_args() passwd = getpass.getpass("Enter {} password: "******"encryption" if args.encrypt else "decryption")) # AES block size is 128 bits (16 bytes) block_size = 16 # Read input file into memory with open(args.input_file, "rb") as f_in: file_in = f_in.read() # Encryption if args.encrypt: # Create a random 128 bit salt (16 bytes) used in the key derivation function # The salt will be stored as the first block of the ciphertext salt = secrets.token_bytes(block_size) # Create a random 10-byte nonce nonce = secrets.token_bytes(10) # Create the IV from the nonce and the initial 6-byte counter value of 0 # The IV will be stored as the second block of the ciphertext counter = 0 IV = nonce + counter.to_bytes(6, "big") # Start AES cipher cipher = AES(password_str=passwd, salt=salt, key_len=256) # Start CTR mode mode = CTR(cipher, nonce) file_out = ( salt + IV + encrypt_decrypt(mode.encrypt, file_in, block_size, counter)) # Create authentication HMAC and store it as the last two blocks of the file hmac_val = hmac.digest(key=cipher.hmac_key, msg=file_out, digest=hashlib.sha256) # Append HMAC to the ciphertext file_out += hmac_val # Decryption else: # Extract the salt from the first 128 bits (16 bytes) of the ciphertext salt = file_in[0:block_size] # Extract nonce from the first 10 bytes of the second block of the ciphertext nonce = file_in[block_size:block_size + 10] # Extract the starting counter value from the next 6 bytes of the ciphertext counter = file_in[block_size + 10:block_size + 10 + 6] counter = int.from_bytes(counter, "big") # Extract the HMAC value from the last 2 blocks of the ciphertext hmac_val = file_in[-2 * block_size:] # Start AES cipher cipher = AES(password_str=passwd, salt=salt, key_len=256) # Compare HMAC values (remove the HMAC value from the ciphertext before comparing) assert hmac.compare_digest( hmac_val, hmac.digest( key=cipher.hmac_key, msg=file_in[:-2 * block_size], digest=hashlib.sha256, ), ), "HMAC check failed." # Start CTR mode mode = CTR(cipher, nonce) # Strip the salt, IV and HMAC from the ciphertext file_in = file_in[2 * block_size:-2 * block_size] file_out = encrypt_decrypt(mode.decrypt, file_in, block_size, counter) # Write output file with open(args.output_file, "wb") as f_out: f_out.write(file_out) print("\n{0} successfully completed! {1} has been stored in: {2}".format( "Encryption" if args.encrypt else "Decryption", "Ciphertext" if args.encrypt else "Plaintext", args.output_file, ))
m -= 1 m = 4 k = int(n / m) # generate strings s = [] for i in range(2**m): st = bin(i)[2:] s.append(str('0' * (m - len(st)) + st)) f = [] # get frecuency for i in s: f.append(findf(bn, i)) sf = 0 for i in f: sf += i**2 pt = (((2**m) / k) * (sf)) - k return pt k = bytes.fromhex('CFBF50935CE44C197D1459E8483A38FA') k = bytearray.fromhex('C6CAB7B7B9D584450D0609112FD5F39A') vi = bytes.fromhex('69F67F1AE5349D250000000000000000') vi = bytes.fromhex('AF92750E273C5EE60000000000000000') s = '' aes = AES(k) for i in range(64): v = bytes.fromhex(hex(int(binascii.hexlify(vi), 16) | i)[2:]) st = str(binascii.hexlify(aes.encrypt_block(v))) s += st[2:len(st) - 1] print(poker(s))