Question

I'm trying to write a program for Brute Force Attack on DES, the key is a string of 8 characters made only from decimal digits for example (12345678).

So i need some help writing a program where i can set the testing key to (00000000) and start looping it incrementally (00000001), (00000002) all the way til (99999999) and trying the key each time of course.

I've done the DES program already and now my brain is stuck with this part.

Update: many thanks for Hyperboreus, for the key generator function, however when i used it in my program, i get error message

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 1: invalid start byte

for any key larger than (00000002)

here is my code

from Crypto.Cipher import DES 
import os 
import base64

print ("key size is fixed at 16 Bytes including parities (56 bits effective)")
size = 16

key = '00000002'
cipher = DES.new(key)            
BS = 8


def getMode():
    while True:
        mode = input('Do you wish to Encrypt "e", Decrypt "d" or Brute force "b" a message?\n')
        if mode in 'e d b'.split():
            return mode
        else:
            print('Enter either "e" for encrypt,"d" for decrypt, or "b" for brute force.\n')


def encrypt(text):
    length = len(plaintext)
    pad = lambda s: s + (BS - len(s) % BS) * ('~')
    paddedtext = pad(plaintext)
    encrypted = DES.new(key, DES.MODE_ECB)
    ciphertext = base64.b64encode(encrypted.encrypt(paddedtext)).decode("utf-8")
    return ciphertext


def decrypt(text):
    decrypted = DES.new(key, DES.MODE_ECB)
    paddedtext = decrypted.decrypt(base64.b64decode(cipher)).decode("utf-8")
    l = paddedtext.count ('~')
    return paddedtext[:len(paddedtext)-l]


def brute(text):
     text2 = input("enter a part of the plain text: ")

    def testkeys ():
        for i in range (100000000):
            yield '{:08d}'.format (i)

    for testkey in testkeys ():
        testkey = format (testkey)
        decrypted = DES.new(testkey, DES.MODE_ECB)
        paddedtext = decrypted.decrypt(base64.b64decode(cipher)).decode("utf-8")
        if paddedtext.find(text2) != -1:
            print ("the key is ",testkey)
            l = paddedtext.count ('~')
            return paddedtext[:len(paddedtext)-l]
            break

mode = getMode()

print ("key is:", key)

if mode[0] == 'e':
    plaintext = input("Enter the plaintext: ")
    encrypted = encrypt(plaintext)
    print ("encrypted:\n", encrypted)

elif mode [0] == 'd':
    cipher = input("Enter the ciphertext: ")
    decrypted = decrypt(cipher)
    print ("decrypted:\n", decrypted)

else:
    cipher = input("Enter the ciphertext: ")
    brute = brute(cipher)
    print ("decrypted:\n", brute)

once i solve this problem, the next step is to incorporate the alphabets (capital and small) into the key

Était-ce utile?

La solution

Strings are immutable, hence you cannot change them. But you can generate all your desired strings.

def keys ():
    for i in range (100000000):
        yield '{:08d}'.format (i)

for key in keys ():
    print ('Testing key {}.'.format (key) )
    #test the key
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top