Skip to content

Instantly share code, notes, and snippets.

@Chrstm
Chrstm / algorithm_a.py
Created March 24, 2019 11:07
Fast Correlation Attacks - Algorithm A
BLOCK = 48
class LFSR:
def __init__(self, init, mask, length=BLOCK):
self.init = init
self.length = length
self.lengthmask = 2 ** length - 1
self.mask = mask & self.lengthmask
@Chrstm
Chrstm / rsa_lsb_oracle_attack.py
Last active February 28, 2023 16:52
RSA LSB Oracle Attack
def lsb_oracle_attack(n, e, c, lsb_oracle):
'''
RSA LSB Oracle Attack
:param lsb_oracle: lsb_oracle(x) == pow(x, d, n) % 2
:return: m or None if not found
'''
l, r, k2 = 0, 1, 1
while n * l // k2 + 1 < n * r // k2:
m = l + r
l <<= 1
@Chrstm
Chrstm / acyclic_group.py
Last active December 15, 2021 09:40
ByteCTF 2021 Final Crypto - Acyclic Group
#!/usr/bin/env python3
from hashlib import sha256
from random import choices, getrandbits, randint
import signal
import string
from flag import FLAG
def proof_of_work() -> bool:
@Chrstm
Chrstm / noise.py
Last active October 25, 2020 11:13
D^3CTF 2019 Crypto - Noise
#!/usr/bin/env python3
from os import urandom
import signal
def getrandbits(bit):
return int.from_bytes(urandom(bit >> 3), "big")
def main():
@Chrstm
Chrstm / elasticsearch_array.py
Last active April 12, 2019 13:10
How to use array of general datatype in elasticsearch-dsl-py?
from elasticsearch_dsl import DocType, Index, Integer, Text, Q
from elasticsearch_dsl.connections import connections
import time
post_index = Index('test_post')
class PostDoc(DocType):
author_id = Integer() # List[Integer]
@Chrstm
Chrstm / mt_recover.py
Created December 2, 2018 17:03
recover python random state (Mersenne Twister)
import random
class MT19937Recover:
"""Reverses the Mersenne Twister based on 624 observed outputs.
The internal state of a Mersenne Twister can be recovered by observing
624 generated outputs of it. However, if those are not directly
observed following a twist, another output is required to restore the
internal index.
See also https://en.wikipedia.org/wiki/Mersenne_Twister#Pseudocode .