栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Hack-you-2014

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hack-you-2014

Hack-you-2014_Crypto_Easy-one

k e y w o r d s : keywords: keywords: 简单异或方程,已知明文攻击

D e script i o n Description Description
#include 
#include 
#include 

int main(int argc, char **argv) {
	if (argc != 3) {
		printf("USAGE: %s INPUT OUTPUTn", argv[0]);
		return 0;
	}
	FILE* input  = fopen(argv[1], "rb");
	FILE* output = fopen(argv[2], "wb");
	if (!input || !output) {
		printf("Errorn");
		return 0;
	}
	char k[] = "CENSORED";
	char c, p, t = 0;
	int i = 0;
	while ((p = fgetc(input)) != EOF) {
		c = (p + (k[i % strlen(k)] ^ t) + i*i) & 0xff;
		t = p;
		i++;
		fputc(c, output);
	}
	return 0;
}

破解密文,解密msg002.enc文件

A n a l y s i s Analysis Analysis

老古董密码学了,甚至用的是cpp编写的加密脚本,但是不影响理解,之后会用python脚本进行解密

主要加密过程

	while ((p = fgetc(input)) != EOF) {
		c = (p + (k[i % strlen(k)] ^ t) + i*i) & 0xff;
		t = p;
		i++;
		fputc(c, output);

其中p是input的逐个字节,实际上也就是附带文件里面的msg001里的字节

这个方程很简单,移项爆破就可以进行解密,首先我们需要知道key,也就是k[],那么将其作为未知数,然后用msg001与msg001.enc两者作为明文密文进行已知明文攻击

这里我们爆破的是key的字符,由于作为key,想必密钥空间一定是ASCII可见字符的组成

那么对每位key爆破所有可见字符(使之与密文进行解方程),判断结果是否为明文的对应位

t = 0
key = []
for i in range(0,len(enc1)):
    for temp_k in range(30,128):
        temp_msg = (enc1[i] - i*i - (temp_k ^ t)) & 0xff
        # print(temp_msg)
        if chr(temp_msg) == msg1[i]:
            key.append(temp_k)
            t = temp_msg
            break
        if temp_k == 127: # 确保所有的字符都找到了,不然就报错退出程序
            print("".join(list(map(chr,key))))
            print("Eorro!")
            exit(0)

这样就得到结果

VeryLongKeyYouWillNeverGuessVe

注意一下,显然最后两个字符是key开始重复了,那么记得截取掉重复的部分

最后将key代入解密方程解密即可

S o l v i n g   c o d e Solving~code Solving code
f = open("msg001.enc","rb")
enc1 = f.read()
f.close()
msg1 = "Hi! This is only test messagen"
assert len(msg1) == len(enc1)
t = 0
key = []
for i in range(0,len(enc1)):
    for temp_k in range(30,128):
        temp_msg = (enc1[i] - i*i - (temp_k ^ t)) & 0xff
        # print(temp_msg)
        if chr(temp_msg) == msg1[i]:
            key.append(temp_k)
            t = temp_msg
            break
        if temp_k == 127:
            print(i)
            print("".join(list(map(chr,key))))
            print("Eorro!")
            exit(0)

# print("".join(list(map(chr,key))))
real_key = "".join(list(map(chr,key)))[:-2]
f = open("msg002.enc","rb")
enc2 = f.read()
t = 0
for i in range(0,len(enc2)):
    m = (enc2[i] - (ord(real_key[i % len(real_key)]) ^ t)  - i * i) & 0xff
    t = m
    print(chr(m),end="")
R e f e r e n c e Reference Reference

(11条消息) 攻防世界-Crypto-Easy-one(main函数传参、加密代码审计)-Hack-you-2014_Sea_Sand息禅-CSDN博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/767754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号