コード例 #1
0
def main():
    knw_plain = 'a' * 18
    plain = my_rand.my_rand_str(my_rand.my_rand(100, 10)) + knw_plain
    seed = my_rand.my_rand(1 << 16)
    print "plain length:", len(plain)

    cipher = prng_encrypt(mt19937.seed_mt, mt19937.random, seed, plain)
    crk_plain = prng_decrypt(mt19937.seed_mt, mt19937.random, seed, cipher)
    if plain == crk_plain:
        print "mt19937 stream cipher succeed"
    else:
        print "mt19937 stream cipher failed"

    #idx = len(cipher)-1
    #rand_num = ord(cipher[idx]) ^ ord('a')
    #crk_seed = crack_mt19937_seed.crack_mt19937_seed(rand_num, idx, 0, 1 << 16, lambda x : x % 256)
    crk_seed = bf_crack_seed(cipher, knw_plain)
    if seed == crk_seed:
        print "crack seed succeed", crk_seed
    else:
        print "crack seed failed, orig_seed:", seed, "crk_seed:", crk_seed

    token = gen_pswd_ret_token()
    if valid_token(token):
        print "token valid"
    else:
        print "toekn invalid"
コード例 #2
0
ファイル: sha1.py プロジェクト: S213B/crypto
def main():
    import my_rand
    import sha
    import binascii
    for i in range(213):
        msg = my_rand.my_rand_str(my_rand.my_rand(213))
        h = sha1(msg)
        s = sha.new(msg)
        if h != s.digest():
            print "SHA-1 failed"
            print len(msg)
            print h
            print s.digest()

    key = "This is key"
    msg = my_rand.my_rand_str(my_rand.my_rand(213))
    hmac = hmac_sha1(key, msg)
    print binascii.hexlify(hmac)
コード例 #3
0
ファイル: brk_fixed_iv_ctr.py プロジェクト: S213B/crypto
def main():
    key = my_rand.my_rand_str(16)
    iv = 0
    ciphers = []
    for plain in plains:
        plain = base64.b64decode(plain)
        cipher = ctr_mode.ctr_encrypt(key, iv, plain)
        ciphers.append(cipher)
    key_stream = brk_fixed_iv_ctr(ciphers)
    import binascii
    print binascii.hexlify(key_stream)
    print binascii.hexlify(
        ctr_mode.ctr_encrypt(key, iv,
                             chr(0) * max(map(len, ciphers))))
コード例 #4
0
ファイル: crk_cbc_iv_eq_key.py プロジェクト: S213B/crypto
def main():
    plain = my_rand.my_rand_str(16)
    cipher = encrypt(plain)
    valid, _plain = decrypt(cipher)
    if valid and plain == _plain:
        print "works"
        # at least two blocks cipher text passed for padding validation in cbc decryption
        crk_key = crk_cbc_iv_eq_key(decrypt, cipher)
        if crk_key == cbc_key:
            print "crack"
        else:
            print cbc_key
            print crk_key
    else:
        print valid
        print plain
        print _plain
コード例 #5
0
def main():
    ecb_key = 'YELLOW SUBMARINE'
    f = open('aes_128.txt', 'r')
    cipher = base64.b64decode(f.read())
    f.close()
    plain = ecb_mode.ecb_decrypt(aes_128.aes_128_decrypt, cipher, ecb_key)

    key = my_rand.my_rand_str(16)
    iv = 0
    cipher = ctr_mode.ctr_encrypt(key, iv, plain)

    crk_plain = crk_rarw_aes_ctr(cipher, key, iv)

    if plain == crk_plain:
        print "crack succeed"
    else:
        print "crack failed"
コード例 #6
0
def main():
    plains = []
    f = open("brk_fixed_iv_ctr2.txt", 'r')
    for line in f:
        plains.append(line)
    f.close()

    key = my_rand.my_rand_str(16)
    iv = 0
    ciphers = []
    for plain in plains:
        plain = base64.b64decode(plain)
        cipher = ctr_mode.ctr_encrypt(key, iv, plain)
        ciphers.append(cipher)

    key_stream = brk_fixed_iv_ctr.brk_fixed_iv_ctr(ciphers)

    import binascii
    print binascii.hexlify(key_stream)
    print binascii.hexlify(
        ctr_mode.ctr_encrypt(key, iv,
                             chr(0) * max(map(len, ciphers))))
コード例 #7
0
def main():
    dh = diffie_hellman.DiffieHellman()

    a_pri, a_pub, a_p, a_g = dh.gen_key_pair()
    m_pri, m_pub, m_p, m_g = dh.gen_key_pair()
    b_pri, b_pub, b_p, b_g = dh.gen_key_pair()

    a_sk = dh.gen_sec_key(m_pub, a_pri)
    key_a = sha1.sha1(str(a_sk))[:16]
    iv_a = my_rand.my_rand_byte(16)

    b_sk = dh.gen_sec_key(m_pub, b_pri)
    key_b = sha1.sha1(str(b_sk))[:16]
    iv_b = my_rand.my_rand_byte(16)

    plain_ori = my_rand.my_rand_str(my_rand.my_rand(30, 10))

    print "Sending following text from A to B..."
    print "\t" + plain_ori
    cipher = cbc_mode.cbc_encrypt(aes_128.aes_128_encrypt, iv_a, plain_ori, key_a)

    print "Text intercepted by M is:"
    plain = cbc_mode.cbc_decrypt(aes_128.aes_128_decrypt, iv_a, cipher, key_a)
    print "\t" + plain

    print "Forwarding text from M to B..."
    cipher = cbc_mode.cbc_encrypt(aes_128.aes_128_encrypt, iv_b, plain, key_b)

    print "Text received by B is:"
    plain = cbc_mode.cbc_decrypt(aes_128.aes_128_decrypt, iv_b, cipher, key_b)
    print "\t" + plain

    if plain == plain_ori:
        print "OK"
    else:
        print "Fail"
コード例 #8
0
ファイル: ctr_bitflip_atk.py プロジェクト: S213B/crypto
import ctr_mode
import my_rand
import mt19937

aes_key = my_rand.my_rand_str(16)
ctr_iv = mt19937.extract_number()


def encode_str(plain):
    percent = '%%%x' % ord('%')
    semicolon = '%%%x' % ord(';')
    equality = '%%%x' % ord('=')
    space = '%%%x' % ord(' ')
    plain = plain.replace('%', percent)
    plain = plain.replace(';', semicolon)
    plain = plain.replace('=', equality)
    plain = plain.replace(' ', space)
    cipher = plain
    return cipher


def encode(user_data):
    pre_str = "comment1=cooking%20MCs;userdata="
    app_str = ";comment2=%20like%20a%20pound%20of%20bacon"

    plain = pre_str + encode_str(user_data) + app_str
    #print len(plain)
    #print plain

    return ctr_mode.ctr_encrypt(aes_key, ctr_iv, plain)
コード例 #9
0
if __name__ == "__main__":
    plaintext = "YELLOW SUBMARINE"
    text = pkcs7_pad(plaintext, 10)
    print binascii.hexlify(plaintext)
    print binascii.hexlify(text)

    str1 = 'abcdef'
    str2 = pkcs7_pad(str1, 16)
    str3 = str2[:-1] + chr(2)

    try:
        print pkcs7_unpad(str1, 16)
    except BadPadError as err:
        print "Bad Padding Format"

    print pkcs7_unpad(str2, 16)

    try:
        print pkcs7_unpad(str3, 16)
    except BadPadError as err:
        print "Bad Padding Format"

    for i in range(30):
        from my_rand import my_rand, my_rand_str
        raw_txt = my_rand_str(my_rand(213))
        pad_txt = pkcs7_pad(raw_txt, 16)
        unpad_txt = pkcs7_unpad(pad_txt, 16)
        if raw_txt != unpad_txt:
            print "PKCS7 Error"
コード例 #10
0
import md4
import my_rand
import struct

hmac_key = my_rand.my_rand_str(my_rand.my_rand(213, 8))


def auth(msg):
    global hmac_key
    return md4.hmac_md4(hmac_key, msg)


def verify(msg, hmac):
    global hmac_key
    return md4.hmac_md4(hmac_key, msg) == hmac


def cal_md4_padding(msg):
    return md4.padding(msg)


def md4_with_init(msg, h0, h1, h2, h3):
    md4.init_hash_val(h0, h1, h2, h3)
    #msg += md4.padding(msg)
    blks = md4.parsing(msg)
    h = md4.compute(blks)
    return h


def len_ext_atk2(ori_msg, new_msg, ori_hmac, verify_func=verify):
    h0 = struct.unpack("<I", ori_hmac[0:4])[0]
コード例 #11
0
ファイル: crk_cbc_iv_eq_key.py プロジェクト: S213B/crypto
import cbc_mode
import aes_128
import my_rand
import string
import xor_encrypt

cbc_key = my_rand.my_rand_str(16)
cbc_iv = map(ord, cbc_key)


def check_plain(plain):
    for c in plain:
        if c not in string.printable:
            return False
    return True


def encrypt(plain):
    return cbc_mode.cbc_encrypt(aes_128.aes_128_encrypt, cbc_iv, plain,
                                cbc_key)


def decrypt(cipher):
    plain = cbc_mode.cbc_decrypt(aes_128.aes_128_decrypt, cbc_iv, cipher,
                                 cbc_key)
    return check_plain(plain), plain


def crk_cbc_iv_eq_key(decrypt_func, cipher, block_size=16):
    block = 'a' * block_size
    zero = chr(0) * block_size
コード例 #12
0
from byte_at_a_time_ecb_decryption import detect_block_size, is_ecb_mode, make_one_byte_short_dic
from my_rand import my_rand, my_rand_str
from aes_128 import aes_128_encrypt
from ecb_detect import detect_ecb_mode
import ecb_mode

key = my_rand_str(16)
#prefix_str = my_rand_str(my_rand(213))
#suffix_str = my_rand_str(my_rand(213))
prefix_str = my_rand_str(my_rand(32))
suffix_str = my_rand_str(my_rand(32))
#print "prefix string length:", len(prefix_str)
#print "suffix string length:", len(suffix_str)


def encrypt_with_fixed_key(plain):

    plain = prefix_str + plain + suffix_str

    return ecb_mode.ecb_encrypt(aes_128_encrypt, plain, key)


def detect_offset_block(encrypt_func, block_len, pad_len):
    # extra block_len for "xxaa aaaa aaax" pad_len overflow
    test_in = 'a' * (pad_len + block_len * 3)
    test_out = encrypt_func(test_in)
    for i in range(len(test_out) / block_len):
        part_1 = test_out[i * block_len:(i + 1) * block_len]
        part_2 = test_out[(i + 1) * block_len:(i + 2) * block_len]
        #print i, len(test_out)
        #print part_1