The following two strings represent encryption keys which can be used for a simp
ID: 3824837 • Letter: T
Question
The following two strings represent encryption keys which can be used for a simple character- substitution encryption program. Plain Key = "ABCDEFGHIJKLMNOPORS 1234567890., " EnigmaKey = "T' 60US7, ORW 89HC3YJMKEIB1QVDXFP52ZN4ALG. To encrypt a string, you would: 1. Find that character's index (position) in PlainKey. 2. Substitute the character at the same index in EnigmaKey. To decrypt a string, you would do the same thing with the opposite keys, i.e. 1. Find the encrypted characters index position) in EnigmaKey. 2. Substitute the character at the same index in PlainKey. The following string represents a message that has already been encrypted using these keys and algorithm: Cypher = "OV HEUV6TMJGV' KMVOV6T9.MVUTMVTV H UVH9UL" Write a program to decrypt and display this message.Explanation / Answer
Python
def findPos(string,c):
for i in range(0, len(string)):
if string[i] == c:
return i
return -1
def encrypt(text, plainkey, enigmakey):
result = ""
for i in range(0, len(text)):
pos = findPos(plainkey, text[i])
if (pos != -1):
result += enigmakey[pos]
return result;
def decrypt(cypher, plainkey, enigmakey):
result = ""
for i in range(0, len(cypher)):
pos = findPos(enigmakey, cypher[i])
if (pos != -1):
result += plainkey[pos];
return result;
def main():
plainkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890.,'"
enigmakey = "T'60US7,ORW 89HC3YJMKEIB1QVDXFP52ZN4ALG."
cypher = "OV HEUV6TMJGV'KMVOV6T9.MVUTMVTVI,H UVH9UL"
print("Cypher : " + cypher)
print("Message: " + decrypt(cypher, plainkey, enigmakey))
main()
# code link for python as indentation mess up here: https://pastebin.com/zb2Rn92b
c++
#include <iostream>
#include <string>
using namespace std;
int findPos(string str, char c)
{
for(int i = 0; i <str.length(); i++)
{
if(str[i] == c)
{
return i;
}
}
return -1;
}
string encrypt(string text, string plainkey, string enigmakey)
{
string result = "";
for(int i = 0; i < text.length(); i++)
{
int pos = findPos(plainkey, text[i]);
if (pos != -1)
result += enigmakey[pos];
}
return result;
}
string decrypt(string cypher, string plainkey, string enigmakey)
{
string result = "";
for(int i = 0; i < cypher.length(); i++)
{
int pos = findPos(enigmakey, cypher[i]);
if (pos != -1)
result += plainkey[pos];
}
return result;
}
int main()
{
string plainkey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890.,'";
string enigmakey = "T'60US7,ORW 89HC3YJMKEIB1QVDXFP52ZN4ALG.";
string cypher = "OV HEUV6TMJGV'KMVOV6T9.MVUTMVTVI,H UVH9UL";
cout << "Cypher : " << cypher << endl;
cout << "Message: " << decrypt(cypher, plainkey, enigmakey) << endl;
return 0;
}
Sample run:
Cypher : OV HEUV6TMJGV'KMVOV6T9.MVUTMVTVI,H UVH9UL
Message: I LOVE CATS, BUT I CAN'T EAT A WHOLE ONE.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.