Cryptography
1. rot 13(rotate by 13 places)
2. Easy Peasy
这类题目通常可以会给一个网页连结(可用nc连线)跟伺服器的原始码,主要就是要根据原始码去找答案
one-time pad 一次性密码本,只用一次就丢换另一组密码
netcat后得到讯息如下:
程式码如后:
#!/usr/bin/python3 -uimport os.pathKEY_FILE = "key"KEY_LEN = 50000FLAG_FILE = "flag"def startup(key_location):flag = open(FLAG_FILE).read()kf = open(KEY_FILE, "rb").read()start = key_locationstop = key_location + len(flag)key = kf[start:stop]key_location = stopresult = list(map(lambda p, k: "{:02x}".format(ord(p) ^ k), flag, key)) #将flag跟key,分别取代p,k后,做XOR,再存成listprint("This is the encrypted flag!\n{}\n".format("".join(result))) #把加密后的key吐出来return key_locationdef encrypt(key_location):ui = input("What data would you like to encrypt? ").rstrip()if len(ui) == 0 or len(ui) > KEY_LEN: #当输入字串长度为0(没输入),或者长度大于5000,结束并return -1return -1 start = key_locationstop = key_location + len(ui)kf = open(KEY_FILE, "rb").read()if stop >= KEY_LEN:stop = stop % KEY_LEN#如果输入的字串长度超过KEY_LEN(50000),会重头开始运算key = kf[start:] + kf[:stop]else:key = kf[start:stop]key_location = stopresult = list(map(lambda p, k: "{:02x}".format(ord(p) ^ k), ui, key)) #奖输入ui跟key在做XORprint("Here ya go!\n{}\n".format("".join(result)))return key_locationprint("******************Welcome to our OTP implementation!******************")c = startup(0)while c >= 0:c = encrypt(c)
这题是会把输入字串进行跟key做XOR运算,可直行很多次,会一直累积,当长度超过50000时,会重複执行,写程式执行
from pwn import *r = remote("mercury.picoctf.net", 41934) #创一个connectionr.recvline() #从connection接收一行指令r.recvline()flag_enc = bytes.fromhex(r.recvline().decode()) #接收下来的bytes 变 str后,这边收到十六进位制值引数#'0345376e1e5406691d5c076c4050046e4000036a1a005c6b1904531d3941055d\n'#再用bytes.fromhex把16进位引述数转成位元组字串,变下面这样# b'\x03E7n\x1eT\x06i\x1d\\\x07l@P\x04n@\x00\x03j\x1a\x00\\k\x19\x04S\x1d9A\x05]'fl = len(flag_enc)def enc(m): r.recvline() r.sendline(m.encode()) r.recvline() #从tube一次接收一行 return bytes.fromhex(r.recvline().decode()) #返回位元组字串enc("a" * (50000 - fl)) #将字元a乘以多次keyxor = enc("a" * fl)def xor(x, y): return bytes(a ^ b for a, b in zip(x, y))key = xor(keyxor, b"a" * fl)flag = xor(flag_enc, key)print(flag.decode())