Utilisation de pycryptodome
Cryptage/Décryptage fonctionnel
This commit is contained in:
parent
55dc49ebf0
commit
124dd6c831
9
.idea/workspace.xml
generated
9
.idea/workspace.xml
generated
@ -1,7 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="3be09a79-ba49-407e-8f34-349a7b2cd6dc" name="Changes" comment="" />
|
||||
<list default="true" id="3be09a79-ba49-407e-8f34-349a7b2cd6dc" name="Changes" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/crypt.py" beforeDir="false" afterPath="$PROJECT_DIR$/crypt.py" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
@ -19,6 +23,7 @@
|
||||
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
|
||||
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
|
||||
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
|
||||
</component>
|
||||
<component name="RunManager">
|
||||
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
||||
@ -28,7 +33,7 @@
|
||||
<envs>
|
||||
<env name="PYTHONUNBUFFERED" value="1" />
|
||||
</envs>
|
||||
<option name="SDK_HOME" value="/usr/bin/python3.9" />
|
||||
<option name="SDK_HOME" value="$PROJECT_DIR$/venv/bin/python" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||
<option name="IS_MODULE_SDK" value="false" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
|
37
crypt.py
37
crypt.py
@ -1,7 +1,7 @@
|
||||
import os, random, struct
|
||||
from Crypto.Cipher import AES
|
||||
|
||||
def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
|
||||
def encrypt_file(key, in_file, out_file=None, chunksize=64 * 1024):
|
||||
""" Encrypts a file using AES (CBC mode) with the
|
||||
given key.
|
||||
|
||||
@ -10,10 +10,10 @@ def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
|
||||
either 16, 24 or 32 bytes long. Longer keys
|
||||
are more secure.
|
||||
|
||||
in_filename:
|
||||
in_file:
|
||||
Name of the input file
|
||||
|
||||
out_filename:
|
||||
out_file:
|
||||
If None, '<in_filename>.enc' will be used.
|
||||
|
||||
chunksize:
|
||||
@ -22,28 +22,27 @@ def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
|
||||
sizes can be faster for some files and machines.
|
||||
chunksize must be divisible by 16.
|
||||
"""
|
||||
if not out_filename:
|
||||
out_filename = in_filename + '.enc'
|
||||
if not out_file:
|
||||
out_file = in_file + '.enc'
|
||||
|
||||
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
|
||||
encryptor = AES.new(key, AES.MODE_CBC, iv)
|
||||
filesize = os.path.getsize(in_filename)
|
||||
cipher = AES.new(key, AES.MODE_CBC)
|
||||
filesize = os.path.getsize(in_file)
|
||||
|
||||
with open(in_filename, 'rb') as infile:
|
||||
with open(out_filename, 'wb') as outfile:
|
||||
with open(in_file, 'rb') as infile:
|
||||
with open(out_file, 'wb') as outfile:
|
||||
outfile.write(struct.pack('<Q', filesize))
|
||||
outfile.write(iv)
|
||||
outfile.write(cipher.iv)
|
||||
|
||||
while True:
|
||||
chunk = infile.read(chunksize)
|
||||
if len(chunk) == 0:
|
||||
break
|
||||
elif len(chunk) % 16 != 0:
|
||||
chunk += ' ' * (16 - len(chunk) % 16)
|
||||
chunk += b' ' * (16 - len(chunk) % 16)
|
||||
|
||||
outfile.write(encryptor.encrypt(chunk))
|
||||
outfile.write(cipher.encrypt(chunk))
|
||||
|
||||
def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
|
||||
def decrypt_file(key, in_file, out_file=None, chunksize=24 * 1024):
|
||||
""" Decrypts a file using AES (CBC mode) with the
|
||||
given key. Parameters are similar to encrypt_file,
|
||||
with one difference: out_filename, if not supplied
|
||||
@ -51,15 +50,17 @@ def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
|
||||
(i.e. if in_filename is 'aaa.zip.enc' then
|
||||
out_filename will be 'aaa.zip')
|
||||
"""
|
||||
if not out_filename:
|
||||
out_filename = os.path.splitext(in_filename)[0]
|
||||
#if not out_file:
|
||||
# out_file = os.path.splitext(in_file)[0]
|
||||
if not out_file:
|
||||
out_file = in_file + '.dec'
|
||||
|
||||
with open(in_filename, 'rb') as infile:
|
||||
with open(in_file, 'rb') as infile:
|
||||
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
|
||||
iv = infile.read(16)
|
||||
decryptor = AES.new(key, AES.MODE_CBC, iv)
|
||||
|
||||
with open(out_filename, 'wb') as outfile:
|
||||
with open(out_file, 'wb') as outfile:
|
||||
while True:
|
||||
chunk = infile.read(chunksize)
|
||||
if len(chunk) == 0:
|
||||
|
8
main.py
8
main.py
@ -1,3 +1,9 @@
|
||||
from crypt import *
|
||||
from Crypto.Random import get_random_bytes
|
||||
|
||||
encrypt_file('0123456789abcdef', 'original_file.txt', 'crypted_file')
|
||||
key = get_random_bytes(32) # 32 bytes * 8 = 256 bits (1 byte = 8 bits)
|
||||
print(key)
|
||||
|
||||
encrypt_file(key, 'original_file.txt')
|
||||
|
||||
decrypt_file(key, 'original_file.txt.enc')
|
||||
|
Loading…
x
Reference in New Issue
Block a user