Skip to content

Instantly share code, notes, and snippets.

@eeddaann
Created July 26, 2018 23:15
Show Gist options
  • Select an option

  • Save eeddaann/a080c5212e67085fde690c8c36e12a86 to your computer and use it in GitHub Desktop.

Select an option

Save eeddaann/a080c5212e67085fde690c8c36e12a86 to your computer and use it in GitHub Desktop.

Revisions

  1. eeddaann created this gist Jul 26, 2018.
    47 changes: 47 additions & 0 deletions cryptimer.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    from Crypto.Cipher import AES
    import random

    MIN_CIPHER = 1000000000000000
    MAX_CIPHER = 1000000000010000
    PLAIN_TEXT = 'test some plain text here'.rjust(32)


    ### encrypt ###
    def generate_ciphers(ciphers_len):
    return [str(random.randint(MIN_CIPHER, MAX_CIPHER)) for i in range(ciphers_len)]

    def encrypt(ciphers_len=100,plain_text=PLAIN_TEXT):
    ciphers = generate_ciphers(ciphers_len)
    hashes = []
    msg = plain_text
    hashes.append(hash(msg))
    for i in ciphers:
    cipher = AES.new(i,AES.MODE_ECB)
    msg = cipher.encrypt(msg) # encrypt the msg
    hashes.append(hash(msg)) # save the hash of the msg
    return msg, hashes # TODO: write to file for backup

    def get_bruteforce_rate():
    '''
    return decryptions per second on average, for tuning encryption's parameters
    '''
    pass

    ### decrypt ###
    def decrypt(encrypted_msg,hashes):
    msg = encrypted_msg
    for i in hashes[::-1]: # iterates over hashes, reversed since the last hash should be decrypted first
    j=MIN_CIPHER
    while j<MAX_CIPHER:
    cipher = AES.new(str(j),AES.MODE_ECB)
    candidate_msg = cipher.decrypt(msg) # try to decrypt
    if hash(candidate_msg) == i: # if we found the right hash we can continue to the next hash
    msg = candidate_msg
    # print "hash found!"
    break
    j+=1
    return msg

    ### main ###
    encrypted_msg,hashes = encrypt()
    print decrypt(encrypted_msg,hashes)