vigenere_cipher2


"/home/yossef/notes/personal/hashing/vigenere_cipher2.md"

path: personal/hashing/vigenere_cipher2.md

- **fileName**: vigenere_cipher2
- **Created on**: 2025-03-21 00:30:21

creating a right version of vigenere cipher ;)

# starting creating a vigenere cipher

def encryption(plain_text: str, key: str) -> str:
    """
    function for implementing a encryption for plain text using key 
    -> ((p + k) % 26) this the formal for encryption
    p: is the char after converted to a ascii representation
    k: is the key for that adding to text
    % 26: to check if out of bounds
    """

    #first making the key with length for the plain_text
    if len(plain_text) < len(key):
        key = key[:len(plain_text)].join("")
    else:
        # chatgpt ;(
        # Repeat the key enough times to exceed the length of plain_text
        # (len(plain_text) // len(key)) calculates how many full times the 
        # key fits inside the text
        # +1 ensures the key repeats one extra time to cover any remaining characters
        key = (key * ((len(plain_text) // len(key)) + 1))
        # Trim the repeated key to exactly match the length of plain_text
        key = key[:len(plain_text)]
        
    encrypt_text = ""

    # itration for all char in text
    for i in range(len(plain_text)):
        if i == " " or key[i] == " ":
            continue
        ## starting by convert the char to number ASCII TABLE
        # for more information:
        # [[???]]
        # why subtraction by a to make a range for x ascii numbers between
        # 0 to 26
        x = ord(plain_text[i].lower()) - ord('a')
        x_key = ord(key[i].lower()) - ord('a')
        x = (x + x_key) % 26 # apply formal
        encrypt_text += chr(x + ord('a')) # convert ot char

    return encrypt_text

def main():
    # testing
    plain_text = "we are discovered save yourself"
    print("plain text:" ,plain_text)
    plain_text.replace(" ", "")
    key = "deceptive".replace(" ", "")
    print("encrypted text:",encryption(plain_text=plain_text, key=key))

## output 
# plain text: we are discovered save yourself
# encrypted text: zipegxvymvgqztkmyrvexicrwpvvinj

if __name__ == "__main__":
    main()

continue:./rail_fence.md
before:./vigenere_cipher.md