Skip to content

Instantly share code, notes, and snippets.

def repeating_key_xor(intxt, key):
outtxt = ""
for i, c in enumerate(intxt):
outtxt += chr(ord(c) ^ ord(key[i % len(key)]))
return outtxt
def repeating_key_xor(intxt, key):
outtxt = ""
for i, c in enumerate(intxt):
outtxt += chr(ord(c) ^ ord(key[i % len(key)]))
return outtx
def detect_single_byte():
f = open('Data/4.txt')
best_freq, ptxt = 0, ''
for ctxt in f.read().split('\n'):
raw = ctxt.decode('hex')
txt = single_byte_cipher(raw)
cur_freq = char_freq(txt)
if cur_freq > best_freq:
from Crypto.Cipher import AES
def AES_ECB_decrypt(ctxt, key):
ptxt = ''
cipher = AES.new(key, AES.MODE_ECB)
for block in range(len(ctxt) / AES.block_size):
start, end = block * AES.block_size, (block+1) * AES.block_size
ptxt += cipher.decrypt(ctxt[start:end])
def hamming(s1, s2):
dist = 0
for c1, c2 in zip(s1, s2):
diff = ord(c1) ^ ord(c2)
dist += sum([1 for b in bin(diff) if b == '1'])
return dist
def best_key_lengths(data):
from Crypto.Cipher import AES
def detect_ECB_mode(ctxt):
blocks = []
for block in range(len(ctxt) / AES.block_size):
start, end = block * AES.block_size, (block+1) * AES.block_size
blocks.append(ctxt[start:end])

Keybase proof

I hereby claim:

  • I am AntonKueltz on github.
  • I am kueltz (https://keybase.io/kueltz) on keybase.
  • I have a public key whose fingerprint is 71BD E22C B50C 5A8E BE07 4891 E9E0 E063 E731 86AA

To claim this, I am signing this object:

def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
@AntonKueltz
AntonKueltz / rsa_factor.py
Created April 1, 2017 03:02
Factor an RSA modulus given the public and private key
def factor(n, e, d):
"""http://crypto.stackexchange.com/a/25910/17884
n - modulus
e - public exponent
d - private exponent
returns - (p, q) such that n = p*q
"""
from fractions import gcd
from random import randint
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC25sHAjYY4W/lloAU+RdWqkpz5
d23lTwSL3nRZ/y5Ku2HPUJS16RiHuLbpEeR9HRlOj6PcnWUXKyaEM/IzAzL2+H1h
luVlXQPe1sKoUCFbteoEzE2+ZGJjr1uS7kWx96xWctJ+ANz12g0dhtODGEPS/q/w
q+zMYgwxyEz10HGs2wIDAQAB
-----END PUBLIC KEY-----