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"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ChangeListManager">
|
<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="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
@ -19,6 +23,7 @@
|
|||||||
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
|
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
|
||||||
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
|
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
|
||||||
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
|
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
|
||||||
|
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
|
||||||
</component>
|
</component>
|
||||||
<component name="RunManager">
|
<component name="RunManager">
|
||||||
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
||||||
@ -28,7 +33,7 @@
|
|||||||
<envs>
|
<envs>
|
||||||
<env name="PYTHONUNBUFFERED" value="1" />
|
<env name="PYTHONUNBUFFERED" value="1" />
|
||||||
</envs>
|
</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="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||||
<option name="IS_MODULE_SDK" value="false" />
|
<option name="IS_MODULE_SDK" value="false" />
|
||||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||||
|
37
crypt.py
37
crypt.py
@ -1,7 +1,7 @@
|
|||||||
import os, random, struct
|
import os, random, struct
|
||||||
from Crypto.Cipher import AES
|
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
|
""" Encrypts a file using AES (CBC mode) with the
|
||||||
given key.
|
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
|
either 16, 24 or 32 bytes long. Longer keys
|
||||||
are more secure.
|
are more secure.
|
||||||
|
|
||||||
in_filename:
|
in_file:
|
||||||
Name of the input file
|
Name of the input file
|
||||||
|
|
||||||
out_filename:
|
out_file:
|
||||||
If None, '<in_filename>.enc' will be used.
|
If None, '<in_filename>.enc' will be used.
|
||||||
|
|
||||||
chunksize:
|
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.
|
sizes can be faster for some files and machines.
|
||||||
chunksize must be divisible by 16.
|
chunksize must be divisible by 16.
|
||||||
"""
|
"""
|
||||||
if not out_filename:
|
if not out_file:
|
||||||
out_filename = in_filename + '.enc'
|
out_file = in_file + '.enc'
|
||||||
|
|
||||||
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
|
cipher = AES.new(key, AES.MODE_CBC)
|
||||||
encryptor = AES.new(key, AES.MODE_CBC, iv)
|
filesize = os.path.getsize(in_file)
|
||||||
filesize = os.path.getsize(in_filename)
|
|
||||||
|
|
||||||
with open(in_filename, 'rb') as infile:
|
with open(in_file, 'rb') as infile:
|
||||||
with open(out_filename, 'wb') as outfile:
|
with open(out_file, 'wb') as outfile:
|
||||||
outfile.write(struct.pack('<Q', filesize))
|
outfile.write(struct.pack('<Q', filesize))
|
||||||
outfile.write(iv)
|
outfile.write(cipher.iv)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
chunk = infile.read(chunksize)
|
chunk = infile.read(chunksize)
|
||||||
if len(chunk) == 0:
|
if len(chunk) == 0:
|
||||||
break
|
break
|
||||||
elif len(chunk) % 16 != 0:
|
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
|
""" Decrypts a file using AES (CBC mode) with the
|
||||||
given key. Parameters are similar to encrypt_file,
|
given key. Parameters are similar to encrypt_file,
|
||||||
with one difference: out_filename, if not supplied
|
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
|
(i.e. if in_filename is 'aaa.zip.enc' then
|
||||||
out_filename will be 'aaa.zip')
|
out_filename will be 'aaa.zip')
|
||||||
"""
|
"""
|
||||||
if not out_filename:
|
#if not out_file:
|
||||||
out_filename = os.path.splitext(in_filename)[0]
|
# 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]
|
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
|
||||||
iv = infile.read(16)
|
iv = infile.read(16)
|
||||||
decryptor = AES.new(key, AES.MODE_CBC, iv)
|
decryptor = AES.new(key, AES.MODE_CBC, iv)
|
||||||
|
|
||||||
with open(out_filename, 'wb') as outfile:
|
with open(out_file, 'wb') as outfile:
|
||||||
while True:
|
while True:
|
||||||
chunk = infile.read(chunksize)
|
chunk = infile.read(chunksize)
|
||||||
if len(chunk) == 0:
|
if len(chunk) == 0:
|
||||||
|
8
main.py
8
main.py
@ -1,3 +1,9 @@
|
|||||||
from crypt import *
|
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