WACONCTF2023 Crypto Cry

WACONCTF2023 Crypto Cry

I participated in the WaconCTF competition last week, did the password part, and recorded the crypto question

Cry


  • Descrpition:😭
  • Solves:3

Task.py

from Crypto.Util.number import bytes_to_long, getStrongPrime, isPrime

SIZE = 512
e = 65537

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


def encrypt(m):
    while True:
        p = getStrongPrime(SIZE)
        if p % 4 != 3:
            continue
        q = p**2 + 1
        assert q % 2 == 0
        if isPrime(q // 2):
            break

    r = getStrongPrime(SIZE * 3)
    n = p * q * r
    c = pow(m, e, n)
    return n, c

if __name__ == "__main__":
    n0, c0 = encrypt(m)
    n1, c1 = encrypt(c0)
    n2, c  = encrypt(c1)

    assert m < n0 and c0 < n1 and c1 < n2

    print(f"{n0 = }")
    print(f"{n1 = }")
    print(f"{n2 = }")
    print(f"{c = }")

Solution


In general,the question is factoring n = p ∗ ( p 2 + 1 ) ∗ r n=p*(p^2+1)*r n=p(p2+1)r,which p is 512bits and r is 512*3 bits.At first glance, it looks very similar to the Sus by maple3142 in the ictf a few weeks ago.

Refer to last thought about Sus,I want to construct a random_element k in F p 4 \mathbb{F}_{p^4} Fp4,so k^n has the order p 2 − 1 p^2-1 p21,just like ax+b in F p 2 \mathbb{F}_{p^2} Fp2.How to constrain the element to two degrees of freedom not still four degrees.

Originally,I random choice the f ( x ) f(x) f(x) to construct the quotient ring Z n [ x ] / f ( x ) \mathbb{Z}_n[x]/f(x) Zn[x]/f(x),and random choice a element k,expect k^n has type a ∗ x + b a*x+b ax+b since it’s order is p^2-1.

After I consult maple3142 and find the discuss on the discard.

Just Choice f ( x ) f(x) f(x) as the type:
f ( x ) = ( x + a ) k m o d m t h i s   m a p l e 314 2 ′ s   i d e a   I   h a v e   n o t   g e t   i t f(x)=(x+a)^kmodm\\ this\ maple3142's\ idea\ I\ have\ not\ get\ it\\ f(x)=(x+a)kmodmthis maple3142s idea I have not get it
f ( x ) = x 4 + a x 2 + b t h i s   c a n   l e t   t h e   e l e m e n t   o r d e r   p 2 − 1   a s   0 x 3 + a x 2 + 0 x + b f(x)=x^4+ax^2+b\\ this\ can\ let\ the\ element\ order\ p^2-1\ as\ 0x^3+ax^2+0x+b\\ f(x)=x4+ax2+bthis can let the element order p21 as 0x3+ax2+0x+b
f ( x ) = g 2 + a , g ′ s   d e g r e e = 2 t h i s   c a n   l e t   t h e   e l e m e n t   o r d e r   p 2 − 1   a s   k ∗ g + b   e q u a l   t o   0 x 3 + a x 2 + b x + c f(x)=g^2+a,g's\ degree=2\\ this\ can\ let\ the\ element\ order\ p^2-1\ as\ k*g+b\ equal\ to\ 0x^3+ax^2+bx+c f(x)=g2+a,gs degree=2this can let the element order p21 as kg+b equal to 0x3+ax2+bx+c

solve.sage

def fac(n):
    R = Zmod(n)["x"]
    while True:
        Q = R.quo(x ^ 4 + randint(0,n) * x ^ 2 + randint(0,n))
        t = Q.random_element() ^ n
        g = gcd(ZZ(t[3]), n) #or t[1]
        if 1 < g < n and g != 2:
            if g % 2 == 0:
                g = g // 2
            return g
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Node.js 中的 crypto 是一个内置模块,用于提供加密和解密功能。它支持各种加密算法和操作,包括哈希函数、对称加密和非对称加密。你可以使用 crypto 模块来实现数据的加密、解密、签名和验证等操作。 要使用 crypto 模块,你需要在你的代码中引入它,例如: ```javascript const crypto = require('crypto'); ``` 一些常见的 crypto 操作包括: 1. 哈希函数:crypto 模块提供了多个哈希函数,如 MD5、SHA-1、SHA-256 等。你可以使用这些函数对数据进行哈希处理,生成唯一的摘要。例如: ```javascript const hash = crypto.createHash('sha256'); hash.update('Hello, world!'); const digest = hash.digest('hex'); console.log(digest); // 输出生成的摘要 ``` 2. 对称加密:crypto 模块支持对称加密算法,如 AES、DES、3DES 等。你可以使用这些算法对数据进行加密和解密。例如: ```javascript const cipher = crypto.createCipher('aes192', 'password'); let encrypted = cipher.update('Hello, world!', 'utf8', 'hex'); encrypted += cipher.final('hex'); console.log(encrypted); // 输出加密后的数据 const decipher = crypto.createDecipher('aes192', 'password'); let decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); console.log(decrypted); // 输出解密后的数据 ``` 3. 非对称加密:crypto 模块还支持非对称加密算法,如 RSA。你可以使用这些算法生成公钥和私钥,进行加密和解密。例如: ```javascript const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 4096, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem' } }); console.log(publicKey); // 输出生成的公钥 console.log(privateKey); // 输出生成的私钥 const encrypted = crypto.publicEncrypt(publicKey, Buffer.from('Hello, world!')); console.log(encrypted.toString('base64')); // 输出加密后的数据 const decrypted = crypto.privateDecrypt(privateKey, encrypted); console.log(decrypted.toString('utf8')); // 输出解密后的数据 ``` 这只是 crypto 模块的一小部分功能,你可以查阅 Node.js 文档以获取更详细的信息和使用方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值