Skip to content
Snippets Groups Projects
Commit 65155b9d authored by Viken's avatar Viken
Browse files

I finished AES and Chacha20, SImon and Speck still need some work, and i have...

I finished AES and Chacha20, SImon and Speck still need some work, and i have run some basics comparison between them, 27th oj June
parent 30d25b30
No related branches found
No related tags found
No related merge requests found
Showing
with 436 additions and 116 deletions
**/__pycache__/
**/.DS_Store
src v1.1/Women in Red.jpg

144 KiB

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
import Crypto.Cipher.AES as AES
def encryption(key, input_file, output_file, mode = AES.MODE_ECB):
'''
Parameters :
- a key : It must be 16 (AES-128), 24 (AES-192) or 32 (AES-256) bytes
- mode :
- an input file :
- an output file :
'''
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
cipher = AES.new(key=key, mode=mode)
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 we reach the end or if there is no file
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))
# Encrypting the data and writting in the output file
ciphertext = cipher.encrypt(chunk)
file_out.write(ciphertext)
def decryption(key, input_file, output_file, mode = AES.MODE_ECB):
'''
Parameters :
- a key : It must be 16 (AES-128), 24 (AES-192) or 32 (AES-256) bytes
- mode :
- an input file :
- an output file :
'''
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
cipher = AES.new(key=key, mode=mode)
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 we reach the end or if there is no file
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))
# Encrypting the data and writting in the output file
ciphertext = cipher.decrypt(chunk)
file_out.write(ciphertext)
import Crypto.Cipher.ChaCha20 as ChaCha20
from Crypto.Random import get_random_bytes
def encryption(key, input_file, output_file):
'''
Parameters :
- a key : The secret key is 256 bits long, 32 bytes
to which we have added the nonce which is a mandatory value that
must never be reused for any other encryption
done with this key.
For ChaCha20, it must be 8 or 12 bytes long.
For XChaCha20, it must be 24 bytes long.
If not provided, 8 bytes will be randomly generated
(you can find them back in the nonce attribute).
- an input file :
- an output file :
'''
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
key, nonce = key[:-12], key[-12:]
cipher = ChaCha20.new(key=key, nonce=nonce)
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 we reach the end or if there is no file
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))
# Encrypting the data and writting in the output file
ciphertext = cipher.encrypt(chunk)
file_out.write(ciphertext)
def decryption(key, input_file, output_file):
'''
Parameters :
- a key : The secret key is always 256 bits long, 32 bytes
to which we have added the nonce which is a mandatory value that
must never be reused for any other encryption
done with this key.
For ChaCha20, it must be 8 or 12 bytes long.
For XChaCha20, it must be 24 bytes long.
If not provided, 8 bytes will be randomly generated
(you can find them back in the nonce attribute).
- an input file :
- an output file :
'''
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
key, nonce = key[:32], key[32:]
cipher = ChaCha20.new(key=key, nonce=nonce)
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 we reach the end or if there is no file
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))
# Encrypting the data and writting in the output file
plaintext = cipher.decrypt(chunk)
file_out.write(plaintext)
......@@ -2,7 +2,7 @@ from speck import SpeckCipher
from simon import SimonCipher
def simon_speck_encryption(key, key_size, mode, input_file, output_file, CIPHER):
def encryption(key, input_file, output_file, CIPHER="Simon", mode = 'ECB'):
'''
Parameters :
- a key : format must be binary.
......@@ -25,13 +25,21 @@ def simon_speck_encryption(key, key_size, mode, input_file, output_file, CIPHER)
- 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
- a CIPHER indication : used to chose between "Simon" and "Speck"
'''
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
key_size = len(key)*8
key = int.from_bytes(key, byteorder='big', signed=False) # converting key to int
if CIPHER == "Simon" :
CIPHER = SimonCipher
if CIPHER == "Speck" :
CIPHER = SpeckCipher
cipher = CIPHER(key, key_size=key_size, mode=mode) # creation of simon object with the key
print(cipher.block_size)
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:
......@@ -52,7 +60,7 @@ def simon_speck_encryption(key, key_size, mode, input_file, output_file, CIPHER)
file_out.write(ciphertext)
def simon_speck_decryption(key, key_size, mode, input_file, output_file, CIPHER):
def decryption(key, input_file, output_file, CIPHER="Simon", mode = 'ECB'):
'''
Parameters :
- a key : format must be binary.
......@@ -78,8 +86,15 @@ def simon_speck_decryption(key, key_size, mode, input_file, output_file, CIPHER)
- a CIPHER indication : used to chose between SimonCipher and SpeckCipher
'''
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
key = int.from_bytes(key, byteorder='big', signed=False) # converting key to int
key_size = len(key)*8
key = int.from_bytes(key, byteorder='big', signed=False) # converting key to int
if CIPHER == "Simon" :
CIPHER = SimonCipher
if CIPHER == "Speck" :
CIPHER = SpeckCipher
cipher = CIPHER(key, key_size=key_size, mode=mode) # creation of simon object with the key
with open(input_file, 'rb') as file_in, open(output_file, 'wb') as file_out:
......@@ -101,24 +116,4 @@ def simon_speck_decryption(key, key_size, mode, input_file, output_file, CIPHER)
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/SIMON & SPECK/encrypted_file.bin'
print("Enncryption in process...")
simon_speck_encryption(key, key_size, 'ECB', input_file, output_file, SpeckCipher)
print("file encrypted")
input_file = output_file
output_file = './src v1.1/function/SIMON & SPECK/decrypted_file.txt'
print("Decryption in process...")
simon_speck_decryption(key, key_size, 'ECB', input_file, output_file, SpeckCipher)
print("file decrypted")
import Crypto.Cipher.AES
from speck import SpeckCipher
from simon import SimonCipher
def aes_encryption(key, key_size, mode, input_file, output_file):
def encryption(key, input_file, output_file, CIPHER="Speck", mode = 'ECB'):
'''
Parameters :
- a key : format must be binary.
......@@ -24,12 +25,42 @@ def aes_encryption(key, key_size, mode, input_file, output_file):
- 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
- a CIPHER indication : used to chose between "Simon" and "Speck"
'''
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
key_size = len(key)*8
key = int.from_bytes(key, byteorder='big', signed=False) # converting key to int
if CIPHER == "Simon" :
CIPHER = SimonCipher
if CIPHER == "Speck" :
CIPHER = SpeckCipher
cipher = CIPHER(key, key_size=key_size, mode=mode) # creation of simon object with the key
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)
#converting the chunk to the appropriate format for the encrypt method
chunk = int.from_bytes(chunk, byteorder='big', signed=False)
# if we reach the end or if there is no file
if not chunk:
break
# Encrypting the data and writting in the output file
ciphertext = cipher.encrypt(chunk)
ciphertext = int.to_bytes(ciphertext, (int.bit_length(ciphertext) + 7) // 8, 'big')
file_out.write(ciphertext)
def aes_decryption(key, key_size, mode, input_file, output_file):
def decryption(key, input_file, output_file, CIPHER="Speck", mode = 'ECB'):
'''
Parameters :
- a key : format must be binary.
......@@ -54,26 +85,34 @@ def aes_decryption(key, key_size, mode, input_file, output_file):
- a CIPHER indication : used to chose between SimonCipher and SpeckCipher
'''
chunk_size = 16 * 1024 # 16KB chunk size for processing large files
key_size = len(key)*8
key = int.from_bytes(key, byteorder='big', signed=False) # converting key to int
if CIPHER == "Simon" :
CIPHER = SimonCipher
if CIPHER == "Speck" :
CIPHER = SpeckCipher
cipher = CIPHER(key, key_size=key_size, mode=mode) # creation of simon object with the key
with open('src v1.1/key_file.bin', 'rb') as file:
key = file.read()
key_size = 128
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)
#converting the chunk to the appropriate format for the encrypt method
chunk = int.from_bytes(chunk, byteorder='big', signed=False)
input_file = './src v1.1/file.txt'
output_file = './src v1.1/function/AES/encrypted_file.bin'
# if we reach the end or if there is no file
if not chunk:
break
print("Enncryption in process...")
aes_encryption(key, key_size, 'ECB', input_file, output_file)
print("file encrypted")
# Decrypting the data and writting in the output file
plaintext = cipher.decrypt(chunk)
plaintext = int.to_bytes(plaintext, (plaintext.bit_length() + 7) // 8, 'big')
file_out.write(plaintext)
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")

sMK.
\ No newline at end of file
from Crypto.Random import get_random_bytes
def generate_key(key_size):
'''
Param :
- key_size in byte
'''
key = get_random_bytes(key_size) # Generate a random key of specified size
return key
......@@ -8,12 +12,11 @@ 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 v1.1/key_file.bin'
# Generate the key
key = generate_key(key_size)
# Save the key to a file
save_key_to_file(key, output_file)
for key_size in [16,24,32] : # Must be 16, 24 or 32 bytes long key
output_file = f'src v1.1/key_storage/key_file{key_size}.bin'
# Generate the key
key = generate_key(key_size)
# Save the key to a file
save_key_to_file(key, output_file)
_J 9
\ No newline at end of file
S]:#^&-җ_2s;
\ No newline at end of file
;Q~"]*d D2_"
\ No newline at end of file
import function.aes as aes
import function.simon as simon
import function.speck as speck
import function.chacha20 as chacha
import key_generation as key_gen
import time
import os
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
CIPHER = {
"AES" : aes,
"ChaCha20" : chacha,
"Simon" : simon,
"Speck" : speck
}
plaintext = './src v1.1/women in Red.jpg'
def encrypt_and_decrypt(cipher, key, key_size, plaintext_file):
encrypted_file = f'./src v1.1/test_result/{cipher}/{cipher}_{key_size}encrypted_file.bin'
print(f"\n {cipher} enncryption in process...")
encryption_time = time.time()
CIPHER[cipher].encryption(key, plaintext_file, encrypted_file)
encryption_time = time.time() - encryption_time
print(f"file encrypted in {encryption_time} s")
decrypted_file = f'./src v1.1/test_result/{cipher}/{cipher}_{key_size}decrypted_file.jpg'
print("Decryption in process...")
decryption_time = time.time()
CIPHER[cipher].decryption(key, encrypted_file, decrypted_file)
decryption_time = time.time() - decryption_time
print(f"file decrypted in {decryption_time} s")
return encryption_time, decryption_time, os.path.getsize(encrypted_file)
def load_key(cipher, key_size):
if cipher == "ChaCha20" :
# Key and nonce
with open(f'src v1.1/key_storage/key_file32.bin', 'rb') as file:
key = file.read()
nonce_rfc7539 = key_gen.generate_key(12) # generation of a 12 byte long nonce
key += nonce_rfc7539
else :
with open(f'src v1.1/key_storage/key_file{key_size}.bin', 'rb') as file:
key = file.read()
return key
def which_key(cipher):
if cipher == "ChaCha20":
key_size = "32"
key = load_key(cipher=cipher, key_size=int(key_size))
else :
while True :
key_size = input("Which key size do you want to use ? (8, 16, 32) ")
if key_size in ("8", "16", "32") :
key = load_key(cipher=cipher, key_size=key_size)
break
else :
print("Please enter a valid key size")
return key, int(key_size)
'''
while True :
cipher = input("Which mode do you cipher do you want to use ? (AES, ChaCha20, Simon or Speck) ")
if cipher in CIPHER.keys() :
key, key_size = which_key(cipher=cipher)
encrypt_and_decrypt(cipher=cipher, key=key, key_size=key_size, plaintext_file=plaintext)
break
else :
print("Please, enter one of the available cipher")
'''
plt.figure()
KEYS_COLOR = {
16 : 'red',
24 : 'blue',
32 : 'green'
}
legend_labels = [] # List to store legend labels
for key_size in (16,24,32):
for cipher in CIPHER.keys():
key = load_key(cipher=cipher, key_size=key_size)
encryption_time, decryption_time, encrypted_file_size = encrypt_and_decrypt(cipher=cipher, key=key, key_size=key_size, plaintext_file=plaintext)
plt.subplot(311)
plt.scatter([cipher], [encryption_time], color = KEYS_COLOR[key_size])
plt.title("Encryption time")
plt.subplot(312)
plt.scatter([cipher], [decryption_time], color = KEYS_COLOR[key_size])
plt.title("Decryption time")
plt.subplot(313)
plt.scatter([cipher], [encrypted_file_size], color = KEYS_COLOR[key_size])
plt.title("Encrypted file size")
legend_labels.append(mpatches.Patch(color=KEYS_COLOR[key_size], label=f'{key_size} key size'))
plt.subplots_adjust(hspace=1)
plt.legend(handles=legend_labels)
plt.show()
src v1.1/test_result/AES/AES_16decrypted_file.jpg

144 KiB

File added
File added
src v1.1/test_result/AES/AES_24decrypted_file.jpg

144 KiB

File added
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