Python code

 avatar
unknown
plain_text
a month ago
3.0 kB
4
Indexable
CAESAR
def caesar_cipher_encryption(text, key):
    result = ""

    for char in text:
        if char.isalpha():
            base = ord('A') if char.isupper() else ord('a')
            result += chr((ord(char) - base + key) % 26 + base)
        else:
            result += char

    return result


def caesar_cipher_decryption(text, key):
    return caesar_cipher_encryption(text, -key)


# Example
encrypted = caesar_cipher_encryption("hello", 3)
print("Encrypted:", encrypted)

decrypted = caesar_cipher_decryption(encrypted, 3)
print("Decrypted:", decrypted)
_____________________________________
VEGILENCS 

def vigenere_en(text, key):
    text = text.lower()
    key = key.lower()
    result = ""
    index = 0

    for char in text:
        if char.isalpha():
            p = ord(char) - ord('a')
            k = ord(key[index % len(key)]) - ord('a')
            c = (p + k) % 26
            result += chr(c + ord('a'))
            index += 1
        else:
            result += char

    return result


def vigenere_de(text, key):
    text = text.lower()
    key = key.lower()
    result = ""
    index = 0

    for char in text:
        if char.isalpha():
            p = ord(char) - ord('a')
            k = ord(key[index % len(key)]) - ord('a')
            c = (p - k) % 26
            result += chr(c + ord('a'))
            index += 1
        else:
            result += char

    return result


# Predefined values
plaintext = "actsick"
key = "amity"

# Encryption
encrypted_text = vigenere_en(plaintext, key)
print("Encrypted Text:", encrypted_text)

# Decryption
decrypted_text = vigenere_de(encrypted_text, key)
print("Decrypted Text:", decrypted_text)

__________________________
HASH
import hashlib


def generate_hashes(file_path):
    # Create hash objects
    md5_hash = hashlib.md5()
    sha256_hash = hashlib.sha256()

    # Open file in binary mode
    with open(file_path, "rb") as file:

        # Read file in chunks
        while chunk := file.read(4096):
            md5_hash.update(chunk)
            sha256_hash.update(chunk)

    # Return hexadecimal hash values
    return md5_hash.hexdigest(), sha256_hash.hexdigest()


# File path
file_path = "example.txt"

# Generate hashes
md5_hash, sha256_hash = generate_hashes(file_path)

# Display results
print("MD5 Hash:", md5_hash)
print("SHA256 Hash:", sha256_hash)

_________________________________________
DES
from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes

key = b'8bytekey'

iv = get_random_bytes(8)

plaintext = "Secure encryption"

# ---------------- ENCRYPTION ----------------

cipher = DES.new(key, DES.MODE_CBC, iv)

encrypted_text = cipher.encrypt(
    pad(plaintext.encode(), DES.block_size)
)

print("Encrypted Text:", encrypted_text.hex())

# ---------------- DECRYPTION ----------------
decipher = DES.new(key, DES.MODE_CBC, iv)

decrypted_text = unpad(
    decipher.decrypt(encrypted_text),
    DES.block_size
)

print("Decrypted Text:", decrypted_text.decode())

Editor is loading...
Leave a Comment