implementacao em piton
This commit is contained in:
parent
2aa5dbb1d9
commit
1227017b76
5 changed files with 86 additions and 0 deletions
82
TPs/TP02/py/cfich_chacha20.py
Executable file
82
TPs/TP02/py/cfich_chacha20.py
Executable file
|
@ -0,0 +1,82 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
def setup(key_file):
|
||||||
|
f = open(key_file,"wb")
|
||||||
|
key_bytes = os.urandom(32)
|
||||||
|
f.write(key_bytes)
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
def encrypt(input_file, key_file):
|
||||||
|
fkey = open(key_file,"rb")
|
||||||
|
inp = open(input_file,"rb")
|
||||||
|
out = open(f"{input_file}.enc","wb")
|
||||||
|
|
||||||
|
key = fkey.read()
|
||||||
|
plaintext = inp.read()
|
||||||
|
nonce = os.urandom(16)
|
||||||
|
cipher = Cipher(algorithms.ChaCha20(key=key,nonce=nonce),mode=None)
|
||||||
|
encryptor = cipher.encryptor()
|
||||||
|
ciphertext = encryptor.update(plaintext)
|
||||||
|
ciphertext = nonce + ciphertext
|
||||||
|
|
||||||
|
out.write(ciphertext)
|
||||||
|
|
||||||
|
fkey.close()
|
||||||
|
inp.close()
|
||||||
|
out.close()
|
||||||
|
|
||||||
|
def decrypt(input_file,key_file):
|
||||||
|
fkey = open(key_file,"rb")
|
||||||
|
inp = open(f"{input_file}.enc","rb")
|
||||||
|
out = open(f"{input_file}.dec","wb")
|
||||||
|
|
||||||
|
key = fkey.read()
|
||||||
|
input_bytes = inp.read()
|
||||||
|
nonce = input_bytes[:16]
|
||||||
|
ciphertext = input_bytes[16:]
|
||||||
|
|
||||||
|
cipher = Cipher(algorithms.ChaCha20(key=key,nonce=nonce),mode=None)
|
||||||
|
decryptor = cipher.decryptor()
|
||||||
|
plaintext = decryptor.update(ciphertext)
|
||||||
|
|
||||||
|
out.write(plaintext)
|
||||||
|
|
||||||
|
fkey.close()
|
||||||
|
inp.close()
|
||||||
|
out.close()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = sys.argv[1:]
|
||||||
|
if len(args) == 0:
|
||||||
|
sys.exit("Needs at least 2 arguments, use help.")
|
||||||
|
|
||||||
|
match args[0]:
|
||||||
|
case "setup":
|
||||||
|
if len(args) != 2:
|
||||||
|
print("Number of arguments is incorrect for setup option")
|
||||||
|
key_file = args[1]
|
||||||
|
setup(key_file)
|
||||||
|
case "enc":
|
||||||
|
if len(args) != 3:
|
||||||
|
print("Number of arguments is incorrect for enc option")
|
||||||
|
input_file = args[1]
|
||||||
|
key_file = args[2]
|
||||||
|
encrypt(input_file,key_file)
|
||||||
|
case "dec":
|
||||||
|
if len(args) != 3:
|
||||||
|
print("Number of arguments is incorrect for dec option")
|
||||||
|
input_file = args[1]
|
||||||
|
key_file = args[2]
|
||||||
|
decrypt(input_file,key_file)
|
||||||
|
|
||||||
|
case "help":
|
||||||
|
print("skill issue")
|
||||||
|
case _:
|
||||||
|
print(f"{args[1]} is not an option, use help")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
1
TPs/TP02/py/input.txt
Normal file
1
TPs/TP02/py/input.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
panda é fixe!!!
|
1
TPs/TP02/py/input.txt.dec
Normal file
1
TPs/TP02/py/input.txt.dec
Normal file
|
@ -0,0 +1 @@
|
||||||
|
panda é fixe!!!
|
1
TPs/TP02/py/input.txt.enc
Normal file
1
TPs/TP02/py/input.txt.enc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
…[nÍ"[v©Õ·µ4ZÍ![n{Vƒ#<23>ÿì<C3BF>»
|
1
TPs/TP02/py/key
Normal file
1
TPs/TP02/py/key
Normal file
|
@ -0,0 +1 @@
|
||||||
|
ן<EFBFBD>Y!ֽ¬m›½'•HjS‘ע<E28098><ה<>:לְAֶ‡Ff
|
Loading…
Reference in a new issue