In: Computer Science
Encryption is a technique used to keep information private. It involves transforming text into a new unreadable sequence of characters by changing the text in some way using a key. The goal is to do this in such a way that someone else who has the key can decrypt the information. In this question, you will write a program to encrypt an input string with the help of a key and an alphabet string.
The program will start by setting up the alphabet string in your code as the following:
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789.,!# " Note the white space at the end of the alphabet string.
You can copy and paste the above statement at the start of your program.
Your program will first prompt the user to enter some text, a phrase, to be encrypted. Your program will then prompt for an integer number, i.e., a numerical code, between 2 and 10 (including 2 and 10). You can assume the program’s user will enter the correct input (i.e., an integer between and including 2 and 10). The program will use this numerical code to compute the key.
To compute the key, your program should first sum up the ordinal values of all the characters in the input phrase that you want encrypted. The program should then compute the remainder of the division of the ordinal sum by the numerical code, and add one to the result, such as: key = sum % code + 1
Your program will then transform the input text into an encrypted sequence of characters using the above alphabet string and the key. To transform the input into an encrypted phrase, your program should go over each character in the input text and locate the character in the alphabet string. Your program will then use the index of the input character from the alphabet string to determine what the encryption will be for this character. The encryption will be the character found in the alphabet string at an index that precedes the index of the input character the alphabet string by key places. For example, if the input character is the letter a and the computed key is 2 then the encryption will be the # character. If the input character is 2 and the computed key is 6 then the encryption will be w.
3
Note, the computed key and the numerical input code are different. The numerical input code is used in computing the key. To obtain the key 2 when the input text is a the numerical input code has to be 2. To obtain the key 6 when the input text is 2 the input code has to be 9.
The program is phyton
PYTHON CODE:
# Encrypt function
def encrypt(text,s):
# alphabet string
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789.,!# ";
# Sum of all the ordinal values of all the characters in the input phrase are computed
# and stored in the sum_values variable
sum_values = 0;
for char in text.lower():
sum_values += ord(char);
# Encrypted text is stored in the result variable
result = "";
# The key value is computed by finding the remainder of the division of the ordinal sum
# by the numerical code, and by adding one to the result, such as: key = sum % code + 1
key = sum_values % code + 1;
# index values are found and the output is stored in the result variable
index = 0;
for char in text.lower():
index = (alphabet.index(char) - key);
result += alphabet[index];
return result;
# User will be asked to enter a phrase or a text and the value will be stored in the text variable
text = input("Enter a phrase / text to encrypt: ");
# User will be asked to enter a numerical code between 2 and 10 and the value will be stored
# in the code variable
code = int(input("Enter a numerical key value between 2 and 10: "));
# Input and Output values are displayed
print("Plain Phrase / Text : " + text);
print("Numerical Code : " + str(code));
print("Encrypted text: " + encrypt(text,code));
OUTPUT: