10.3 - Array encryption
Problem
Write a program that will encrypt an array of strings using a +1 Caesar cipher.
The program is only expected to work with lower case letters.
The letter ‘z’ should wrap back to ‘a’.
Array
array = ["pizza", "pasta", "pancakes"]
Example run
Passwords pizza pasta pancakes Encrypted passwords qjaab qbtub qbodblft
| 1 | Encrypt array of strings | IN | array |
| OUT | encrypted_array |
| 2 | Display both arrays | IN | array, encrypted_array |
| OUT |
1.1 create empty array for encrypted strings
1.2 loop through array of strings
1.3 encrypt current string (This will require multiple steps)
1.4 add encrypted string to encrypted array
1.5 return array
# Main array =["pizza", "pasta", "pancakes"] encrypted_array = encrypt(array) display(array, encrypted_array)
def encrypt(array):
encrypted_array = [str]*len(array)
for i in range(len(array)):
encrypted_string = ""
for j in range(len(array[i])):
ascii_val = ord(array[i][j])
ascii_val = ascii_val + 1
if ascii_val > 122:
ascii_val = ascii_val - 26
new_char = chr(ascii_val)
encrypted_string = encrypted_string + new_char
encrypted_array[i] = encrypted_string
return encrypted_array
def display(array, encrypted_array):
print("Passwords")
for i in range(len(array)):
print(array[i])
print("Encrypted passwords")
for i in range(len(encrypted_array)):
print(encrypted_array[i])
# Main
array =["super", "study", "party"]
encrypted_array = encrypt(array)
display(array, encrypted_array)
"""
This program encrypts an array of strings (like a list of passwords)
using a Caesar shift of +1 on lowercase letters (a–z).
Each character is converted into its ASCII code with ord(),
shifted forward by 1, and then converted back to a character with chr().
If the value goes past 'z' (ASCII 122), it wraps back around to 'a'.
Finally, the program displays both the original and the encrypted arrays.
"""
def encrypt(array):
# Create an empty array of the same length to store encrypted strings
encrypted_array = [str] * len(array)
# Loop through each string in the array
for i in range(len(array)):
encrypted_string = "" # start empty for each string
# Loop through each character of the current string
for j in range(len(array[i])):
# Convert the character into its ASCII number
ascii_val = ord(array[i][j])
# Add 1 to shift the character
ascii_val = ascii_val + 1
# If the value goes past 'z' (122), wrap around to 'a'
if ascii_val > 122:
ascii_val = ascii_val - 26
# Convert the number back into a character
new_char = chr(ascii_val)
# Add the new character to the encrypted string
encrypted_string = encrypted_string + new_char
# Save the encrypted string in the result array
encrypted_array[i] = encrypted_string
# Return the array of encrypted strings
return encrypted_array
def display(array, encrypted_array):
# Print the original strings
print("Passwords")
for i in range(len(array)):
print(array[i])
print() # blank line for readability
# Print the encrypted strings
print("Encrypted passwords")
for i in range(len(encrypted_array)):
print(encrypted_array[i])
# Main program
# Define an array of strings
array = ["pizza", "pasta", "pancakes"]
# Encrypt the array
encrypted_array = encrypt(array)
# Display original and encrypted versions
display(array, encrypted_array)
Extension
Add a new function that can decrypt an array of strings.