def test_salt_marker(): """Setting the salt marker produces valid header.""" marker = b'test' infile = BytesIO(plaintext.encode()) cipherfile = BytesIO() encrypt(infile, cipherfile, password, salt_marker=marker) ciphertext = cipherfile.getvalue() assert ciphertext[:4] == marker and ciphertext[6:10] == marker
def test_roundtrip(): """AES file encryption/decryption roundtrip produces identical files.""" with open(infn, 'rb') as infile, open(encfn, 'wb') as outfile: encrypt(infile, outfile, password) with open(encfn, 'rb') as infile, open(outfn, 'wb') as outfile: decrypt(infile, outfile, password) with open(infn, 'rb') as original, open(outfn, 'rb') as copy: assert original.read() == copy.read()
def makearchive(backup_id, uploader, enc_password, max_size): print("Progress: %0.02f MB up, %0.02f MB to go" % (getfilesize(backup_id, True)/(1024*1024), getfilesize(backup_id, False)/(1024*1024))) atime = int(time.time()) aformatteddate = time.strftime('%Y-%m-%d-%H%M%S') aname = '%s.tar.gz.aes' % (aformatteddate) archive_id = manifest.db.execute("INSERT INTO archives (filename,ctime,backup_id) VALUES(?,?,?)", (aname, atime, backup_id,)).lastrowid if arg.v >= 2: print("Compiling file list "+aname, end=" "); sys.stdout.flush() strip_len = len(backup_dir) if strip_len != "/": strip_len += 1 with tempfile.TemporaryDirectory() as dir, \ open('/tmp/backup.log', 'a') as log: files_count = 0 apathprefix = os.path.join(dir, aformatteddate) with open(apathprefix+'.lst', 'wb') as tmp, open(apathprefix+'.csv', 'w') as csvfile: csvout = csv.writer(csvfile, quoting=csv.QUOTE_ALL) for file in getfiles(backup_id, 2500, max_size): if arg.v >= 2: print (".", end=""); sys.stdout.flush() tmp.write(bytes(file['filespec'][strip_len:], 'UTF-8')) tmp.write(bytes([0])) hash = "" if file['regular']: hash = aescrypt.md5_file(file['filespec']) manifest.db.execute('UPDATE files SET archive_id=?,hash=? WHERE rowid=?', (archive_id, hash, file['file_rowid'],)) csvout.writerow([hash, file['filespec'], archive_id, aname]) files_count += 1 if arg.v >= 2: print ("\n",files_count," files") if files_count == 0: return False if arg.v >= 2: print("Encrypting file list") with open(apathprefix+'.csv', 'rb') as filelist, open(apathprefix+'.csv.aes', 'wb') as encfilelist: aescrypt.encrypt(filelist, encfilelist, enc_password) if arg.v >= 2: print("Uploading "+apathprefix+'.csv.aes'+"...") uploader.uploadfile(apathprefix+'.csv.aes') if arg.v >= 2: print("OK") if arg.v >= 2: print("Building archive from file list ",apathprefix+'.lst') apath = os.path.join(dir, aname) with open(apath, 'wb') as archive_file: tar = Popen(['tar', '--null', '-czf', '-', '-C', backup_dir, '-T', tmp.name, '--no-recursion'], stdout=PIPE, stderr=log) aescrypt.encrypt(tar.stdout, archive_file, enc_password) if arg.v >= 2: print("Uploading "+apath+"...") uploader.uploadfile(apath) hash = aescrypt.md5_file(apath) manifest.db.execute("UPDATE archives set hash=? where rowid=?", (hash,archive_id,)) manifest.db.commit() if arg.v >= 2: print("OK") return True
def test_key_size(): """Key sizes of 128, 192 and 256 bit produce valid ciphertexts.""" infile = BytesIO(plaintext.encode()) for key_size in AES.key_size: cipherfile = BytesIO() encrypt(infile, cipherfile, password, key_size=key_size) infile.seek(0) ciphertext = cipherfile.getvalue() assert len(ciphertext) % 16 == 0 cipherfile.seek(0) outfile = BytesIO() decrypt(cipherfile, outfile, password, key_size=key_size) decrypted = outfile.getvalue().decode('utf-8') assert decrypted == plaintext
def test_salt_marker_notbytes(): """Passing not bytes-type salt marker raises TypeError.""" marker = '$' infile = BytesIO(plaintext.encode()) cipherfile = BytesIO() encrypt(infile, cipherfile, password, salt_marker=marker)
def test_salt_marker_toolong(): """Passing too long salt marker raises ValueError.""" marker = b'iamlong' infile = BytesIO(plaintext.encode()) cipherfile = BytesIO() encrypt(infile, cipherfile, password, salt_marker=marker)
def test_salt_marker_empty(): """Passing empty salt marker raises ValueError.""" marker = b'' infile = BytesIO(plaintext.encode()) cipherfile = BytesIO() encrypt(infile, cipherfile, password, salt_marker=marker)
def test_kdf_iterations_tohigh(): """Setting kdf_iterations too high raises ValueError.""" infile = BytesIO(plaintext.encode()) cipherfile = BytesIO() encrypt(infile, cipherfile, password, kdf_iterations=65536)
def test_kdf_iterations(): """Passed kdf_iterations are set correctly in header.""" infile = BytesIO(plaintext.encode()) cipherfile = BytesIO() encrypt(infile, cipherfile, password, kdf_iterations=1000) assert cipherfile.getvalue()[1:3] == b'\x03\xe8'
fichier = open(args.in_info, "r") message = fichier.read() fichier.close() if abort is False and message == "": abort = True print "Le message est vide" if not abort: sess_key=random_key = base64.encodestring(urandom(16)) cle_enc = elgamal.encrypt(info_pk[2], sess_key) message_enc = aescrypt.encrypt(sess_key, message, "genereiv".encode('hex')) fichier_out=open(args.out_info, "w") fichier_out.write(cle_enc.decode()+"\n"+message_enc) fichier_out.close() print("Message encrypté écrit dans "+args.out_info) # ecctool -dec -id alice -in secret.crypt -out secret.txt elif args.dec: action = 0 if action != -1 and args.user_id: action = 2 else: print("[ERREUR] -id manquant")