Caesar Cipher

Easy
9
67.5% Acceptance

In this lab, you will create two functions, encrypt and decrypt, to implement the Caesar cipher technique. These functions should handle both encryption and decryption of a given string based on a user-defined shift value. The primary objectives and constraints for your functions are as follows:

  • The functions should be case insensitive, converting all characters to lowercase during encryption and decryption.
  • Non-alphabetical characters, including spaces, should not be altered.

Function Requirements

  1. encrypt(str, shift):

    • Input: A string (str) and an integer (shift).
    • Output: The encrypted string, with each alphabetical character shifted forward by shift positions in the alphabet.
  2. decrypt(str, shift):

    • Input: An encrypted string (str) and the original shift value (shift).
    • Output: The decrypted string, reversing the shift applied during encryption.

Examples

  1. Example 1:

    • Encrypt: encrypt("hello world", 5) should return "mjqqt btwqi".
    • Decrypt: decrypt("mjqqt btwqi", 5) should revert back to "hello world".
  2. Example 2:

    • Encrypt: encrypt("Python 3.8!", 12) should return "dbftbs 3.8!".
    • Decrypt: decrypt("dbftbs 3.8!", 12) should revert back to "python 3.8!".

Your task is to implement these functions accurately and ensure they meet the specified requirements. Pay close attention to case insensitivity and the handling of non-alphabetical characters.