import gmpy2 from sympy.ntheory.modular import crt from Crypto.Util.number import long_to_bytes from pwn import * r=remote("node1.anna.nssctf.cn",'28993') r.recv() n=[] c=[] for i inrange(127): r.send(b'\n')
from Crypto.Util.number import * from secret import flag
p = getPrime(512) q = getPrime(512) assert p > q n = p*q e = 65536 m = bytes_to_long(flag) num1 = (pow(p,e,n)-pow(q,e,n)) % n num2 = pow(p-q,e,n) c = pow(m,e,n)
import gmpy2 from flag import flag from Crypto.Util.number import *
definit(): p = getPrime(2048) whileTrue: x = getRandomNBitInteger(1024) y = getPrime(768) z = gmpy2.invert(x, p) * y % p return (p, x, y, z)
defencrypt(cipher, p, z): message = bytes_to_long(cipher) r = getRandomNBitInteger(1024) c = (r * z + message) % p return c
p, x, y, z = init() c = encrypt(flag, p, z) withopen("cipher.txt", "w") as f: f.write("binz = " + str(bin(z)) + "\n") f.write("binp = " + str(bin(p)) + "\n") f.write("binc = " + str(bin(c)) + "\n")
题解
基础 NTRU 格密码,具体推导可看前几篇博客 DUTCTF 的 ez_RSA
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import gmpy2 from Crypto.Util.number import * z=0b10101100101100011011110010110100101000101001111100110011110110010001000011100001001001111000111110000010010010111110001011111100111101010111001100100111100100111001011011110010100010100010110111101101011101111111110010000000010110011010011110000011000001111110001110111111001100001100101001010101100011100001101001101011011101110001000111001010111111110000010111100111011010011001111100111111101000010111111011010100110111000001011000001111010100000000101010110000100000111000000010001101110001101000111001010111111101100111110000011001110100011100011101000101111010111000111100010001101001101100000111010110100000001101100010001000011111100111110001010000010110110110010001110010100000000101111101100010000111011011101111110100001000011001101001110110011110000110011100010010011010011001110100010110001011110100100101110101001000011100100100000001001011010011001110001000111101101110110010011110000010110000110111000111100010101100010100100101101100000111100001010000101010111001000111111000111100111011111110011001000000110000000110111101000010111001100111100010010100010001111011100110110111101111001010101110001000001110101110101100101001101100100101011010000011111111110111101001100001000111010000111110000001010100100101111010110001000111010100011110100000000100100001101001000101100111001110010010011110010000101101010011101000110000001101010010011000010110111101001011010011010000001010110001100100000001111001100000000100100100010010101011110100001010010110001101001111000100100110001011111000101110011100110010111001110100010000111001110101000101110101110110100100110110111000100110011111110011100111100111100011001101010011110000101000100110010000000000111101111000001100000100110010100100111001011011011100100011110001010001111011111011000010010011101110000011101101110001111100011100101011001000101110010110011100110000100101010001010111011001000011001101000011111100010001111001100001111100011001010111010010110001110110111111010011010111011010000110110100100011010111000110011010010001100011011010100111011101011000010110000110001010 p=0b10111111010111000101011100010100001001011110111101011011010110110000010011111101110011100111001101110111010101000001111111111000101100111000110010000110101001110100000000011100001111101000101110010101110011110101100011111111110000110001101101110100100111100101101100110001001101110010010100010011100011111000001000001100001110000011010000111001000000111010000001001101101100110111001111110000111000110001011111000010011101110010101011100110011000111100111100000001100101011001000110001111000010011111110010101010111001111000111011011110110100011011010110000011100101100100010001011011111100100011001101011110100100000011011110110110011111000110001000010010001111001111100101000101101011011110111010111101110000011100000100010001010100000011000110001011001000101011101011101011111010010010110101000111111110011010100000010001000011111100100110001000010011111000111010100111000101100011100111110100111000101011010101010101111111010011101001110011101101001100100010100000110011101111100000011101010001010001011000101011001111010000000000010100001101010010011011000000010000111010000001000111101001001000011000011100101100011010100111010010111110110100011101100000000110001011101110100100011011110101010010010001101100001011000001101010110110101110100110110011110011110011100001000000011101000000010011101001001001111111001010100111111110101000110011010110001101011101011111100100001011001111100010001100011011010100111100111000100010000011100100001010111100001100011001010001111001001001110011100100010111000001110001000110101110011100100000101000100010011011011001011010010101111001110101000011001111000101111011011101001101001111100111001111011100110100100011000010010100000110001010110000100111110101001111100100101001101101000100001001011011011000101100111101100011110011011101001010110101001100100100010010100011111000010110101010101011110100011000011000001111001100000110010110101110100101100010100010001001101000110110111101110100000001001001000010010001101101001011000101111101011110001101010101110111111101101100101110011011010000100110111011 c=0b1100000111101010011111101100110110001100001111110010001011011110011111000011111010010111110100010001110000101100101010100010001001011110001100000101100111010010001001000011010110100100100100101010001100011010111111010101011010111000010010111101110101001010110000101101010010011100000100010110011011010001111101000111000100101011011011101100001011100101001101100000010100001100010111010110101001100110101000000101110111111001011000011100100101110010000000110010111100001000111110111011111011011111100101011000110000000011001101010111001101010100011000011100001000010000001100011100110110000010111100101011110010110011100000111001010000101010001000001101101100010000101100111111100110010100001011110101111001000101111111010100100110011011001101101000010000111110010100100100111100110100110011011000011101110010111011111110100110011010110100010101000011111001111000100001000011011000010000001010011111001111010000000110111011000110001100110010101000101110110101000001101001000001011000110011011011010111011011101100101011101010110101111100010101000010100001010011101010101001100000010000000001111111100100100011011110001001010010110001000101111011101010101100001011111101100101000100010100010100000001111101000111110101111111100001011111001010101001011000011011100001011100010100111100100110101111011001011001011100001011111110100001111011101001000001110110011101111101011000010110011111011001100011000111010101111000101001100010010001101110111011111101110111110001000111111001111010101110100111001001001000100111111111111100010100111110110001111010011001111010001000010110000000101100001000111010101010100000111110111110011010001100001101111010111110010010011001111001100111010010011010010101100111011100001011110111100001000101101000111001000001011000010111010101011110011100001010001100110011101011000000110001010011110010100011100000101111000110110110000001011010110010000001001111100001010010010001101111011010111000100001011111010111001000000011101001001111001011101010100001100010101001011010110111011010010100100100101011111100110111110111 h=z
M = Matrix([[1, h], [0, p]]) fg = M.LLL()[0] f, g = abs(fg[0]), abs(fg[1]) a = f * c % p E = a * gmpy2.invert(f, g)%g print(f.bit_length()) print(long_to_bytes(E))
ez_fac(未出)
题目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from Crypto.Util.number import * import random from secret import flag,a0,a1,b0,b1
p = getPrime(512) q = getPrime(512) e = getPrime(128) n = p*q assertpow(a0,2) + e * pow(b0,2) == n assertpow(a1,2) + e * pow(b1,2) == n m = bytes_to_long(flag) c = pow(m,e,n)