HTB crypto BabyEncryption

解压得到密文和加密逻辑
chall.py如下

import string
from secret import MSG

def encryption(msg):
    ct = []
    for char in msg:
        ct.append((123 * char + 18) % 256)
    return bytes(ct)

ct = encryption(MSG)
f = open('./msg.enc','w')
f.write(ct.hex())
f.close()

ct是由msg中的每个字符乘123+18模256然后转16进制得到的
只需要转回bytes然后减18*d就能得到msg了

其中d是123对256的逆元

解密脚本如下

import gmpy2
d = gmpy2.invert(123,256)
def de(msg):
    pt = []
    for char in msg:
        char = char - 18
        char = d * char % 256
        pt.append(char)
    return bytes(pt)

with open('msg.enc') as f:
    ct = bytes.fromhex(f.read())

pt = de(ct)
print(pt.decode())

你可能感兴趣的:(CTF-CRYPTO,python,网络安全)