LABI: tema01 added

This commit is contained in:
TiagoRG 2023-02-18 14:35:57 +00:00
parent 8c970f94a4
commit 76aa0c9336
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
6 changed files with 66 additions and 0 deletions

View File

@ -5,5 +5,7 @@
* [Slides](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/1semestre/iei/tema01/tema-1-criptografia.pdf)
* [Guião](https://github.com/TiagoRG/uaveiro-leci/blob/master/1ano/1semestre/iei/tema01/guide-1-cripto.pdf)
### Nota: código bastante incompleto porque a biblioteca do PyCrypto não gosta de trabalhar :skull:
---
*Pode conter erros, caso encontre algum, crie um* [*ticket*](https://github.com/TiagoRG/uaveiro-leci/issues/new)

View File

View File

@ -0,0 +1,12 @@
import os
import sys
from Crypto.Cipher import AES
def main(args=None):
cipher = AES.new(os.urandom(16), AES.MODE_ECB)
print(cipher.encrypt(b'Hello World!'))
if __name__ == '__main__':
main(sys.argv[1:])

View File

@ -0,0 +1,16 @@
import hashlib
import sys
def main(args=None):
with open(args[0], 'rb') as f:
buffer = f.read(512)
h = hashlib.sha1()
while len(buffer) > 0:
h.update(buffer)
buffer = f.read(512)
print(h.hexdigest())
if __name__ == '__main__':
main(sys.argv[1:])

View File

@ -0,0 +1,14 @@
import sys
from Crypto.Hash import SHA256
def main(args=None):
with open(args[0], 'rb') as f:
buffer = f.read()
h = SHA256.new()
h.update(buffer)
print(h.hexdigest())
if __name__ == '__main__':
main(sys.argv[1:])

View File

@ -0,0 +1,22 @@
# Screw 1.4 :skull:
import codecs
import os
import sys
from Crypto.Cipher import ARC4
def main(args=None):
with open(args[0], 'r') as f:
text = f.read(2048)
cipher = ARC4.new(bit_array_from_str(args[1]))
cryptogram = cipher.encrypt(text)
os.write(1, cryptogram)
print()
def bit_array_from_str(s):
return [codecs.encode("hex") for elem in s]
if __name__ == '__main__':
main(sys.argv[1:])