Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/Crypton
Path: blob/master/Authenticated-Encryption/AES-GCM/Attack-Forbidden/Challenges/Forbidden/task.py
1074 views
1
from cryptography.hazmat.backends import default_backend
2
from cryptography.hazmat.primitives.ciphers import (
3
Cipher, algorithms, modes
4
)
5
#from secret import key
6
7
8
def encrypt(iv, key, plaintext, associated_data):
9
encryptor = Cipher(
10
algorithms.AES(key),
11
modes.GCM(iv),
12
backend=default_backend()
13
).encryptor()
14
encryptor.authenticate_additional_data(associated_data)
15
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
16
17
return (ciphertext, encryptor.tag)
18
19
20
def decrypt(key, associated_data, iv, ciphertext, tag):
21
decryptor = Cipher(
22
algorithms.AES(key),
23
modes.GCM(iv, tag),
24
backend=default_backend()
25
).decryptor()
26
decryptor.authenticate_additional_data(associated_data)
27
28
return decryptor.update(ciphertext) + decryptor.finalize()
29
30
31
iv = "9313225df88406e555909c5aff5269aa".decode('hex')
32
key = "\x1eh_[Q\xa0\xae\xfb\x9b,\x11\x85\xfb\xa6\xbe\xaa"
33
34
ciphertext1, tag1 = encrypt(iv, key, "From: John Doe\nTo: John Doe\nSend 100 BTC", "John Doe")
35
ciphertext2, tag2 = encrypt(iv, key, "From: VolgaCTF\nTo: VolgaCTF\nSend 0.1 BTC", "VolgaCTF")
36
ciphertext3, tag3 = encrypt(iv, key, "From: John Doe\nTo: VolgaCTF\nSend ALL BTC", "John Doe")
37
38