Skip to content

Instantly share code, notes, and snippets.

@tarunbod
Last active April 16, 2022 03:31
Show Gist options
  • Save tarunbod/4cf03b3dddb128f97cf914cf3559d50d to your computer and use it in GitHub Desktop.
Save tarunbod/4cf03b3dddb128f97cf914cf3559d50d to your computer and use it in GitHub Desktop.

Lab 5.15

def caesar_cipher_encrypt(text, key, a):
    text = text.upper()
    encrypted_Text = ""
    for i in text:
        if i in a:
            index = (a.find(i) + key) % len(a)
            encrypted_Text = encrypted_Text + a[index]
        else:
            encrypted_Text = encrypted_Text + i
    return encrypted_Text

def caesar_cipher_decrypt(text, key, a):
    text = text.upper()
    decrypted_Text = ""
    for i in text:
        if i in a:
            index = (a.find(i) - key) % len(a)
            decrypted_Text = decrypted_Text + a[index]
        else:
            decrypted_Text = decrypted_Text + i
    return decrypted_Text

def caesar_hack(text, a, check):
    text = text.upper()
    check = check.upper()
    for i in range(len(text)):
        text_pos = -1
        check_pos = -1
        for j in range(len(a)):
            if text[i] == a[j]:
                text_pos = j
            if check[i] == a[j]:
                check_pos = j
        key = (text_pos - check_pos) % len(a)
        decrypted = caesar_cipher_decrypt(text, key, a)
        if decrypted == check:
            return key, decrypted
    return 99, "Error: Key not found!"
        

if __name__ == "__main__":
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    text = "This is a secret message"
    shift = int(input())
    encrypted_data = caesar_cipher_encrypt(text, shift, alphabet)
    print('Brute Force Hack Attempt: ')
    print('Encrypted Message: ', encrypted_data)

    k, h = caesar_hack(encrypted_data, alphabet, text)
    if k == 99:
        print(h)
    else:
        print("Successful Attempt found! Key =", k)
        print("Secret message:", h)

Lab 5.16

import hashlib

def hash_function(password, salt, al):
    if al == 'md5':
        hash = hashlib.md5(salt.encode() + password.encode())
        hash.update(password.encode('utf-8'))
        return hash.hexdigest() + ':' + salt
    elif al == 'sha1':
        hash = hashlib.sha1()
        hash.update(password.encode('utf-8'))
        return hash.hexdigest() + ':' + salt
    elif al == 'sha224':
        hash = hashlib.sha224()
        hash.update(password.encode('utf-8'))
        return hash.hexdigest() + ':' + salt
    elif al == 'sha256':
        hash = hashlib.sha256()
        hash.update(password.encode('utf-8'))
        return hash.hexdigest() + ':' + salt
    elif al == 'blake2b':
        hash = hashlib.blake2b()
        hash.update(password.encode('utf-8'))
        return hash.hexdigest() + ':' + salt
    else:
        print("Error: No Algorithm!")

if __name__ == "__main__":
    hash_list = ['md5', 'sha1', 'sha224', 'sha256', 'blake2b']
    password = input()
    salt = hex(4458585599393)
    for al in hash_list:
        hashed = hash_function(password, salt, al)
        print("Testing hash algorithm: ", al)
        print("Hashed Password = ", hashed)
        print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment