def Decrypt(filename, aesMode): file_in = open(filename, 'r+b') file_out = open('decrypted.txt', 'w+b') pyaes.decrypt_stream(aesMode, file_in, file_out) file_in.close() file_out.close()
def aes_decrypt(fp, key: str, iv: str, out): """ Grabs the payload from an im4p and dumps the decrypted output to `out` :param fp: Input file pointer :param key: STRING-HEX repr of AES 256 bit key :param iv: STRING-HEX repr of AES 64 bit initialization vector :param out: Output file pointer :return: """ iv = bytes.fromhex(iv) key = bytes.fromhex(key) decoder = asn1.Decoder() decoder.start(fp.read()) # location of the raw encrypted cyphertext in the img4, probably? cipher = asn1_serialize(decoder)[0][3] # doing this is lazy temp_filename = '.temp_' + ''.join( random.choice(string.ascii_lowercase) for i in range(10)) with open(temp_filename, 'wb') as outf: outf.write(cipher) # img4 are encrypted with Cipher-Block Chaining mode mode = pyaes.AESModeOfOperationCBC(key, iv=iv) file_in = open(temp_filename, 'rb') pyaes.decrypt_stream(mode, file_in, out) file_in.close() os.remove(temp_filename)
def aes_decrypt(self, key, chunksize=64*1024): self.out_filename = self.out_filename or os.path.splitext(self.in_filename)[0] aes = pyaes.AESModeOfOperationCTR(key) with open(self.in_filename, 'rb') as infile: with open(self.out_filename, 'wb') as outfile: pyaes.decrypt_stream(aes, infile, outfile)
def decrypt(self, file_in_path, file_out_path): file_in = file(file_in_path) file_out = file(file_out_path, 'wb') pyaes.decrypt_stream(self.mode, file_in, file_out) # Close the files file_in.close() file_out.close()
def decrypt(self): output_file_name = str(sys.argv[1].split(".")[0]) try: with open(str(sys.argv[1])) as input_file: with open((str(output_file_name)), 'wb+') as output_file: pyaes.decrypt_stream(self.mode, input_file, output_file) except FileNotFoundError as e: print(e)
def decrypt_db(self): mode = pyaes.AESModeOfOperationCTR(self.key) if (os.path.isfile(USER_DIR + '/critter_enc.db')): file_in = open(USER_DIR + '/critter_enc.db', 'rb') file_out = open(USER_DIR + '/critter.db', 'wb') pyaes.decrypt_stream(mode, file_in, file_out) file_in.close() file_out.close() os.remove(USER_DIR + '/critter_enc.db')
def myaes_decrypt(key, in_file, out_file): key2 = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" key2 = key + key2[len(key):] mode = pyaes.AESModeOfOperationCTR(key2.encode('utf-8')) file_in = open(in_file, 'rb') file_out = open(out_file, 'wb') pyaes.decrypt_stream(mode, file_in, file_out) file_in.close() file_out.close()
def myaes_decrypt(key,in_file,out_file): key2 = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" key2 = key + key2[len(key):] mode = pyaes.AESModeOfOperationCTR(key2) file_in = file(in_file) file_out = file(out_file, 'wb') pyaes.decrypt_stream(mode, file_in, file_out) file_in.close() file_out.close()
def aesMode(rawfile, newfile, operation, aes_keys): key = aes_keys.encode("utf-8") AES = pyaes.AESModeOfOperationCTR(key) file_in = open(rawfile, 'rb') file_out = open(newfile, 'wb') if operation == '加密': pyaes.encrypt_stream(AES, file_in, file_out) elif operation == '解密': pyaes.decrypt_stream(AES, file_in, file_out) file_in.close() file_out.close()
def crypter(infile, outfile, key=KEY_256, iv=IV_128, decrypt=False): # AES cipher in CBC operation aes = pyaes.AESModeOfOperationCBC(key, iv=iv) infile = file(infile) outfile = file(outfile, 'wb') if options.decrypt: pyaes.decrypt_stream(aes, infile, outfile) else: pyaes.encrypt_stream(aes, infile, outfile) # close file infile.close() outfile.close()
def deszyfruj(nazwa_pliku_do_deszyfracji, klucz): file_in = open(nazwa_pliku_do_deszyfracji, "rb") file_out = open('tmp.txt', 'wb+') klucz = hashlib.sha256(klucz).digest()[:32] mode = pyaes.AESModeOfOperationCTR(klucz) pyaes.decrypt_stream(mode, file_in, file_out) file_out.close() file_in.close() path = os.getcwd() if os.path.isfile(path + "\\" + nazwa_pliku_do_deszyfracji): os.remove(path + "\\" + nazwa_pliku_do_deszyfracji) os.rename(path + "\\tmp.txt", path + "\\" + nazwa_pliku_do_deszyfracji)
def decryptfile(): key = bytes("This_key_for_demo_purposes_only!", 'utf-8') # mode= pyaes.AESModeOfOperationCBC(key, iv=iv) mode = pyaes.AESModeOfOperationCBC(key, iv=iv) file_in = open('encryptedeicar.bin', 'r+') file_out = open('decrypteicar.txt', 'w') pyaes.decrypt_stream(mode, file_in, file_out) file_in.close() file_out.close()
def aesMode(rawfilename, mode, operation, aes_bits, aes_keys): aes_mode = mode + '%s位' % aes_bits newfile = base.newFileName(rawfilename, aes_mode, operation) key = aes_keys.encode("utf-8") AES = pyaes.AESModeOfOperationCTR(key) file_in = open(rawfilename, 'rb') file_out = open(newfile, 'wb') if operation == '加密': pyaes.encrypt_stream(AES, file_in, file_out) elif operation == '解密': pyaes.decrypt_stream(AES, file_in, file_out) file_in.close() file_out.close()
def decrypt(self, key, ciphertext): iv = ciphertext[:16] encoded_message = ciphertext[16:] instream = BytesIO(encoded_message) outstream = BytesIO() aes = pyaes.AESModeOfOperationCBC(key, iv) pyaes.decrypt_stream(aes, instream, outstream) plaintext = outstream.getvalue() instream.close() outstream.close() return plaintext
def myaes_decrypt_from_hex(key,in_file,out_file): key2 = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" key2 = key + key2[len(key):] mode = pyaes.AESModeOfOperationCTR(key2) file_in_hex = file(in_file) din = file_in_hex.read().decode('hex') file_in_bin = file(in_file + '.bin', 'wb') file_in_bin.write(din) file_in_hex.close() file_in_bin.close() file_in = file(in_file + '.bin') file_out = file(out_file, 'wb') pyaes.decrypt_stream(mode, file_in, file_out) file_in.close() file_out.close()
import pyaes import hashlib # TODO: try python 2.7 # Any mode of operation can be used; for this example CTR inp = input('Password: '******'UTF-8')).hexdigest()[0:32], encoding='UTF-8') # Create the mode of operation to encrypt with mode = pyaes.AESModeOfOperationCTR(key) # The input and output files file_in = open('out.txt') file_out = open('decs.txt', 'wb') # Encrypt the data as a stream, the file is read in 8kb chunks, be default pyaes.decrypt_stream(mode, file_in, file_out) # Close the files file_in.close() file_out.close()
mode = pyaes.AESModesOfOperation[mode_name] print(mode.name + ' (stream operations)') kw = dict(key = key) if mode_name in ('cbc', 'cfb', 'ofb'): kw['iv'] = os.urandom(16) moo = mode(**kw) output = StringIO() pyaes.encrypt_stream(moo, StringIO(plaintext), output) output.seek(0) ciphertext = output.read() moo = mode(**kw) output = StringIO() pyaes.decrypt_stream(moo, StringIO(ciphertext), output) output.seek(0) decrypted = output.read() passed = decrypted == plaintext cipher_length = len(ciphertext) print(" cipher-length=%(cipher_length)s passed=%(passed)s" % locals()) # Issue #15 # https://github.com/ricmoo/pyaes/issues/15 # @TODO: These tests need a severe overhaul; they should use deterministic input, keys and IVs... def TestIssue15(): print('Issue #15') key = b"abcdefghijklmnop" iv = b"0123456789012345"
import pyaes # https://github.com/ricmoo/pyaes # Alt: key_256 = os.urandom(32) # 32*8 = 256 bits KEY_256 = "TodayMeColdNightSeeSnowFlyAcross" # 今天我寒夜裡看雪飄過 32 bytes key IV_128 = "SeaWide&SkyEmpty" # 海闊天空 16 bytes IV if __name__ == '__main__': # argparse parser = argparse.ArgumentParser(description='Slow encrypter') parser.add_argument("-i", "--infile", help="Path to input", required=True) parser.add_argument("-o", "--outfile", help="Path to output", default='/dev/null') parser.add_argument("-d", "--decrypt", action='store_true', default=False, help="Decrypt instead of encrypt") args = parser.parse_args() # invoke aes = pyaes.AESModeOfOperationCBC(KEY_256, iv=IV_128) infile = file(args.infile) outfile = file(args.outfile, 'wb') if not args.decrypt: pyaes.encrypt_stream(aes, infile, outfile) else: pyaes.decrypt_stream(aes, infile, outfile) infile.close() outfile.close()
passed = decrypted == plaintext cipher_length = len(ciphertext) print(" cipher-length=%(cipher_length)s passed=%(passed)s" % locals()) plaintext = os.urandom(1000) for mode_name in pyaes.AESModesOfOperation: mode = pyaes.AESModesOfOperation[mode_name] print(mode.name + ' (stream operations)') kw = dict(key=key) if mode_name in ('cbc', 'cfb', 'ofb'): kw['iv'] = os.urandom(16) moo = mode(**kw) output = BytesIO() pyaes.encrypt_stream(moo, BytesIO(plaintext), output) output.seek(0) ciphertext = output.read() moo = mode(**kw) output = BytesIO() pyaes.decrypt_stream(moo, BytesIO(ciphertext), output) output.seek(0) decrypted = output.read() passed = decrypted == plaintext cipher_length = len(ciphertext) print(" cipher-length=%(cipher_length)s passed=%(passed)s" % locals())