Skip to content
Snippets Groups Projects
Commit 30d25b30 authored by Viken's avatar Viken
Browse files

Work of the 26th : including implementation of some of the encryption algo

parent 3175743c
No related branches found
No related tags found
No related merge requests found
Showing
with 253 additions and 0 deletions
File added
File added
File deleted
File added
File added
File added
def decrypt_file(input_file, output_file, key, cipher_algorithm):
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
cipher = cipher_algorithm.new(key, cipher_algorithm.MODE_ECB) # Create the cipher object with the provided algorithm, key, and mode
with open(input_file, 'rb') as file_in, open(output_file, 'wb') as file_out:
# Decrypt and write the file data in chunks
while True:
chunk = file_in.read(chunk_size)
if not chunk:
break
decrypted_chunk = cipher.decrypt(chunk)
file_out.write(decrypted_chunk)
=o<1RFap|[B<YN
\ No newline at end of file
from Crypto.Random import get_random_bytes
def encrypt_file(input_file, output_file, key, cipher_algorithm):
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
cipher = cipher_algorithm.new(key, cipher_algorithm.MODE_ECB) # Create the cipher object with the provided algorithm and key
'''
Note that we have set the mode to MODE_ECB for simplicity in this example.
In practice, it is recommended to use a more secure mode of operation,
such as MODE_CBC (Cipher Block Chaining) or MODE_CTR (Counter Mode),
depending on your specific requirements.
'''
with open(input_file, 'rb') as file_in, open(output_file, 'wb') as file_out:
# Encrypt and write the file data in chunks
while True:
chunk = file_in.read(chunk_size)
if not chunk:
break
# Pad the last chunk if needed (optional)
if len(chunk) % cipher.block_size != 0:
chunk += b'\x00' * (cipher.block_size - (len(chunk) % cipher.block_size))
ciphertext = cipher.encrypt(chunk)
file_out.write(ciphertext)
Je suis un message
\ No newline at end of file
x~?ykmTQ
\ No newline at end of file
from Crypto.Random import get_random_bytes
def generate_key(key_size):
key = get_random_bytes(key_size) # Generate a random key of specified size
return key
def save_key_to_file(key, output_file):
with open(output_file, 'wb') as file:
file.write(key)
# Example usage
key_size = 16 # Key size in bytes (128 bits)
output_file = 'src/key_file.bin'
# Generate the key
key = generate_key(key_size)
# Save the key to a file
save_key_to_file(key, output_file)
from Crypto.Cipher import AES, ARC4, ChaCha20
from encryption import encrypt_file
from decryption import decrypt_file
# Choose the encryption algorithm (AES, Simon, Speck, RC4, or ChaCha20)
cipher_algorithm = AES
key_file = "./src v1.0/key_file.bin"
with open(key_file, 'rb') as file:
key = file.read()
input_file = './src v1.0/file.txt'
output_file = './src v1.0/encrypted_file.bin'
print("Enncryption in process...")
encrypt_file(input_file, output_file, key, cipher_algorithm)
print("file encrypted")
input_file = 'src v1.0/encrypted_file.bin'
output_file = 'src v1.0/decrypted_file.txt'
print("Decryption in pocress...")
decrypt_file(input_file, output_file, key, cipher_algorithm)
print("file decrypted")
Bonjour monsieur, Je suis un message
\ No newline at end of file
import Crypto.Cipher.AES
def aes_encryption(key, key_size, mode, input_file, output_file):
'''
Parameters :
- a key : format must be binary.
If not supplied at initialization, both ciphers will default to 128-bit
encryption keys and block sizes. If the defaults are not used, it is
reccomended to specify both the key size and block explictly.
- a key_size : size of the used key
- mode : For convenience, both ciphers support the most common modes of block cipher
operation.
Electronic Code Book ECB (Default mode for Speck/Simon)
Counter CTR
Cipher Block Chaining CBC
Propagating Cipher Block Chaining PCBC
Cipher Feedback CFB
Output Feedback OFB
- an input file : format must be binary. It is the file which will be encrypted
- an output file : format must be binary too. It is the file in whch the encryption will be stored
- a CIPHER indication : used to chose between SimonCipher and SpeckCipher
'''
def aes_decryption(key, key_size, mode, input_file, output_file):
'''
Parameters :
- a key : format must be binary.
If not supplied at initialization, both ciphers will default to 128-bit
encryption keys and block sizes. If the defaults are not used, it is
reccomended to specify both the key size and block explictly.
- a key_size : size of the used key
- mode : For convenience, both ciphers support the most common modes of block cipher
operation.
Electronic Code Book ECB (Default mode for Speck/Simon)
Counter CTR
Cipher Block Chaining CBC
Propagating Cipher Block Chaining PCBC
Cipher Feedback CFB
Output Feedback OFB
- an input file : format must be binary. It is the file which will be encrypted
- an output file : format must be binary too. It is the file in whch the encryption will be stored
- a CIPHER indication : used to chose between SimonCipher and SpeckCipher
'''
with open('src v1.1/key_file.bin', 'rb') as file:
key = file.read()
key_size = 128
input_file = './src v1.1/file.txt'
output_file = './src v1.1/function/AES/encrypted_file.bin'
print("Enncryption in process...")
aes_encryption(key, key_size, 'ECB', input_file, output_file)
print("file encrypted")
input_file = output_file
output_file = './src v1.1/function/AES/decrypted_file.txt'
print("Decryption in process...")
aes_decryption(key, key_size, 'ECB', input_file, output_file)
print("file decrypted")
import Crypto.Cipher.ChaCha20
def chacha20_encryption(key, key_size, mode, input_file, output_file):
'''
Parameters :
- a key : format must be binary.
If not supplied at initialization, both ciphers will default to 128-bit
encryption keys and block sizes. If the defaults are not used, it is
reccomended to specify both the key size and block explictly.
- a key_size : size of the used key
- mode : For convenience, both ciphers support the most common modes of block cipher
operation.
Electronic Code Book ECB (Default mode for Speck/Simon)
Counter CTR
Cipher Block Chaining CBC
Propagating Cipher Block Chaining PCBC
Cipher Feedback CFB
Output Feedback OFB
- an input file : format must be binary. It is the file which will be encrypted
- an output file : format must be binary too. It is the file in whch the encryption will be stored
- a CIPHER indication : used to chose between SimonCipher and SpeckCipher
'''
def chacha20_decryption(key, key_size, mode, input_file, output_file):
'''
Parameters :
- a key : format must be binary.
If not supplied at initialization, both ciphers will default to 128-bit
encryption keys and block sizes. If the defaults are not used, it is
reccomended to specify both the key size and block explictly.
- a key_size : size of the used key
- mode : For convenience, both ciphers support the most common modes of block cipher
operation.
Electronic Code Book ECB (Default mode for Speck/Simon)
Counter CTR
Cipher Block Chaining CBC
Propagating Cipher Block Chaining PCBC
Cipher Feedback CFB
Output Feedback OFB
- an input file : format must be binary. It is the file which will be encrypted
- an output file : format must be binary too. It is the file in whch the encryption will be stored
- a CIPHER indication : used to chose between SimonCipher and SpeckCipher
'''
with open('src v1.1/key_file.bin', 'rb') as file:
key = file.read()
key_size = 128
input_file = './src v1.1/file.txt'
output_file = './src v1.1/function/ChaCha20/encrypted_file.bin'
print("Enncryption in process...")
chacha20_encryption(key, key_size, 'ECB', input_file, output_file)
print("file encrypted")
input_file = output_file
output_file = './src v1.1/function/ChaCha20/decrypted_file.txt'
print("Decryption in process...")
chacha20_decryption(key, key_size, 'ECB', input_file, output_file)
print("file decrypted")
\ No newline at end of file
suis un message
\ No newline at end of file
۩LNvU
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment