week06
This commit is contained in:
36
week05/hard/test.py
Normal file
36
week05/hard/test.py
Normal file
@ -0,0 +1,36 @@
|
||||
import base64
|
||||
import random
|
||||
from Crypto.Cipher import AES
|
||||
|
||||
KEY = random.randbytes(16)
|
||||
# KEY =b'\xaab\xa65z"\xac\xb1,a\xff1\xdb6}\xb0' # The key should match the server's key
|
||||
IV = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" # The IV can be random
|
||||
|
||||
def pkcs7(message: bytes, block_size: int = 16) -> bytes:
|
||||
gap_size = block_size - (len(message) % block_size)
|
||||
return message + bytes([gap_size] * gap_size)
|
||||
|
||||
|
||||
def cbc_mac(message: bytes, iv: bytes, key: bytes) -> bytes:
|
||||
cipher = AES.new(key, AES.MODE_CBC, iv)
|
||||
message = pkcs7(message)
|
||||
last_block = cipher.encrypt(message)[-16:]
|
||||
return last_block
|
||||
|
||||
# Craft the message
|
||||
message = b"type=recrets&number=1337"
|
||||
|
||||
# Calculate the MAC
|
||||
mac = cbc_mac(message, IV, KEY)
|
||||
|
||||
# Base64 encode the message, IV, and MAC
|
||||
encoded_message = base64.b64encode(message).decode('utf-8')
|
||||
encoded_iv = base64.b64encode(IV).decode('utf-8')
|
||||
encoded_mac = base64.b64encode(cbc_mac(message, IV,KEY)).decode('utf-8')
|
||||
|
||||
# Final request to send
|
||||
final_message = f"{encoded_message};{encoded_iv};{encoded_mac}"
|
||||
|
||||
print(final_message)
|
||||
|
||||
# MAC verification failed: expected afb7f5f307ea507b631d964e089a820a, got a0b0b48844a7deaf9d917a5f11ae0359
|
Reference in New Issue
Block a user