2021-07-27

[b01lers2020]safety_in_numbers

题目
enc.py

import sys
import Crypto.PublicKey.RSA as RSA


def enc(msg, pubkey):
   (n,e) = pubkey
   m = int.from_bytes(msg, byteorder = 'little')
   c = pow(m, e, n)
   ctxt = (c).to_bytes(c.bit_length() // 8 + 1, byteorder = 'little')
   return ctxt


with open("pubkey.pem", "r") as f:
   ciph = RSA.importKey(f.read())     # chill out, Crypto.RSA takes its sweet time... (minutes)

pubkey = (ciph.n, ciph.e)


with open("flag.txt", "rb") as f:
   flag = f.read()

sys.stdout.buffer.write(enc(flag, pubkey))

pubkey.pem
在这里插入图片描述
flag.enc
在这里插入图片描述
解题
首先要进行公钥解析,但是文件有点大,所以这次不能用在线工具了,那就用代码

import Crypto.PublicKey.RSA as RSA
with open("pubkey.pem", "r") as f:
   key = RSA.importKey(f.read())
n = key.n
e = key.e
print (n)
print (e)

注意,由于文件比较大,所以要等一会才能得到n和e。

运行之后发现,n很大很大,但是e相对与n来说很小很小
在这里插入图片描述
所以应该是小明文攻击

import gmpy2
from Crypto.Util.number import long_to_bytes

e = 65537
f=open('flag.enc','r')
f.readline()
c = int.from_bytes(f.readline(), byteorder='little')
m = gmpy2.iroot(c,e)[0]
print(long_to_bytes(m))

但是,并没有得到我想要的,而是在运行了一段时间之后,得到了一条错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xce in position 9: illegal multibyte sequence

有师傅建议,在打开文件时加上

,encoding='utf-8'

但是,运行了一下,依旧不行

后来是将’r’改成了’rb‘用二进制形式打开的

结果运行出了:b'\x01'

???

发现是文件打开的代码错误
正确代码:

import gmpy2
from Crypto.Util.number import long_to_bytes

e = 65537
f=open('flag.enc','rb').read()

c = int.from_bytes(f, byteorder='little')
m = gmpy2.iroot(c,e)[0]
print(long_to_bytes(m))

运行得到:b'}f00Rp_3RutUf!{ftcp'

将字符逆过来就好了

print(long_to_bytes(m)[::-1])

运行得到:b'pctf{!fUtuR3_pR00f}'

答案

flag{!fUtuR3_pR00f}

[AFCTF2018]MyOwnCBC

题目

CBC什么东西呀?不就是把上一轮加密的影响扩散到下一轮嘛
它写的CBC一点都不正宗
我这样写肯定也行的!

大概吧?
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

from Crypto.Cipher import AES
from Crypto.Random import random
from Crypto.Util.number import long_to_bytes

def MyOwnCBC(key, plain):
	if len(key)!=32:
		return "error!"
	cipher_txt = b""
	cipher_arr = []
	cipher = AES.new(key, AES.MODE_ECB, "")
	plain = [plain[i:i+32] for i in range(0, len(plain), 32)]
	print plain
	cipher_arr.append(cipher.encrypt(plain[0]))
	cipher_txt += cipher_arr[0]
	for i in range(1, len(plain)):
		cipher = AES.new(cipher_arr[i-1], AES.MODE_ECB, "")
		cipher_arr.append(cipher.encrypt(plain[i]))
		cipher_txt += cipher_arr[i]
	return cipher_txt
	
key = random.getrandbits(256)
key = long_to_bytes(key)

s = ""
with open("flag.txt","r") as f:
	s = f.read()
	f.close()

with open("flag_cipher","wb") as f:
	f.write(MyOwnCBC(key, s))
	f.close()

在这里插入图片描述
解题

CBC指的是密码分组链接(Cipher-block chaining)的简称。CBC是最为常用的工作模式。它的主要缺点在于加密过程是串行的,无法被并行化,而且消息必须被填充到块大小的整数倍。解决后一个问题的一种方法是利用密文窃取。注意在加密时,明文中的微小改变会导致其后的全部密文块发生改变,而在解密时,从两个邻接的密文块中即可得到一个明文块。因此,解密过程可以被并行化,而解密时,密文中一位的改变只会导致其对应的明文块完全改变和下一个明文块中对应位发生改变,不会影响到其它明文的内容。

题目给出了加密代码,尝试直接写解密代码
参考(https://blog.csdn.net/weixin_44110537/article/details/107633811)

import os,sys
os.chdir(sys.path[0])
from Crypto.Cipher import AES
from Crypto.Random import random
from Crypto.Util.number import*

cipher = open('flag_cipher.txt','rb').read()
key = cipher[0:32]
# print(key)
def MyOwnCBC(key,cipher):
    cipher = [cipher[i:i+32] for i in range(0,len(cipher),32)]
    flag = b''
    tempkey = key
    for i in range(1, len(cipher)):
        dic_cipher = AES.new(tempkey, AES.MODE_ECB)
        flag += dic_cipher.decrypt(cipher[i])
        tempkey = cipher[i]
    return flag
	
print(MyOwnCBC(key,cipher))

运行得到

b"mode of operation is an algorithm that uses a block cipher to provide an information service such as confidentiality or authenticity. A block cipher by itself is only suitable for the secure cryptographic transformation (encryption or decryption) of one fixed-length group of bits called a block. A mode of operation describes how to repeatedly apply a cipher's single-block operation to securely transform amounts of data larger than a block.\n\nMost modes require a unique binary sequence, often called an initialization vector (IV), for each encryption operation. The IV has to be non-repeating and, for some modes, random as well. The initialization vector is used to ensure distinct ciphertexts are produced even when the same plaintext is encrypted multiple times independently with the same key. Block ciphers have one or more block size(s), but during transformation the block size is always fixed. Block cipher modes operate on whole blocks and require that the last part of the data be padded to a full block if it is smaller than the current block size. There are, however, modes that do not require padding because they effectively use a block cipher as a stream cipher.\n\nHistorically, encryption modes have been studied extensively in regard to their error propagation properties under various scenarios of data modification. Later development regarded integrity protection as an entirely separate cryptographic goal. Some modern modes of operation combine confidentiality and authenticity in an efficient way, and are known as authenticated encryption modes.\n\nAh you found it~ afctf{Don't_be_fooled_by_yourself}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"

最后一句有flag

答案

flag{Don’t_be_fooled_by_yourself}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值