Q1 e Q2 acabadas

Signed-off-by: tsousa111 <tiagao2001@hotmail.com>
This commit is contained in:
Tiago Sousa 2024-02-20 19:31:56 +00:00
parent 1227017b76
commit 8d679cab60
4 changed files with 61 additions and 20 deletions

View file

@ -0,0 +1,30 @@
#!/usr/bin/env python3
import sys
def attack(fctxt, pos, plainAtPos, newPlainAtPos):
f = open(fctxt,"rb")
ciphertext = f.read()
f.close()
plainAtPos = plainAtPos.encode()
newPlainAtPos = newPlainAtPos.encode()
txt_len = len(plainAtPos)
diff = bytes([a ^ b for (a,b) in zip(plainAtPos,newPlainAtPos)])
cipher_diff = bytes([a ^ b for (a,b) in zip(diff,ciphertext[pos:pos+txt_len])])
new_ciphertext = ciphertext[:pos] + cipher_diff + ciphertext[pos+txt_len:]
with open(fctxt+".attck","wb") as f:
f.write(new_ciphertext)
def main():
argv = sys.argv[1:]
argc = len(argv)
if argc < 3 or argc > 5:
sys.exit("Needs 4 arguments <fctxt> <pos> <ptxtAtPos> <newPtxtAtPos>")
attack(argv[0],int(argv[1]),argv[2],argv[3])
if __name__ == "__main__":
main()