Question

In: Computer Science

DO IN C++ Secret Codes! Create a program that will accept a message from the user...

DO IN C++

Secret Codes!

Create a program that will accept a message from the user and either
encrypt ordecrypt it with the following algorithms:

To encrypt:

- get the character you wish to encrypt

- find the index of that character in the alphabet array

- add the shift offset to the index

- add the increment to the index

- "wrap around" the new index so the result is between 0 and 29

- find the character at the new index

To decrypt:

- get the character you wish to decrypt

- find the index of that character in the alphabet array

- subtract the shift offset from the index

- subtract the increment from the index

- "wrap around" the new index so the result is between 0 and 29

- find the character at the new index

- repeat steps 1-6 for every shift offset

The alphabet is <space>,a-z(lowercase),<period>,<question mark>,
and <comma> in that order.

The increment should be +3 per letter position.

Here is a sample message. Who said it?

f..yqjrnvrpuabefw.j,ddxgcndpxmsdehsomjkcydfygtd.rrp?dgstmvxmf.wo?jxgrneslzifrgzyqv.v

Solutions

Expert Solution

#include <iostream>
using namespace std;

char c[]= {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm','n','o','p','q','r','s','t','u','v','w','x','y','z', '.', '?', ','};
int inc = 3;

string encrypt(string text, int s)
{
string result = "";

// traverse text
for (int i=0;i<text.length();i++)
{
if (islower(text[i]))
result += c[((text[i] - 'a' + 1) + s + inc)%30];
else if (text[i] == ' ')
{
result += c[(0 + s + inc)%30];
}
else if (text[i] == '.')
{
result += c[(27 + s + inc)%30];
}
else if (text[i] == '?')
{
result += c[(28 + s + inc)%30];
}
else if (text[i] == ',')
{
result += c[(29 + s + inc)%30];
}
else
{
result += text[i];
}
}

// Return the resulting string
return result;
}

int wrapIndex(int i, int i_max) {
return ((i % i_max) + i_max) % i_max;
}

string decryptHelper(string text, int s)
{
string result = "";

// traverse text
for (int i=0;i<text.length();i++)
{
if (islower(text[i]))
result += c[wrapIndex((text[i] - 'a' + 1) - s - inc, 30)];
else if (text[i] == ' ')
{
result += c[wrapIndex(0 - s - inc, 30)];
}
else if (text[i] == '.')
{
result += c[wrapIndex(27 - s - inc, 30)];
}
else if (text[i] == '?')
{
result += c[wrapIndex(28 - s - inc, 30)];
}
else if (text[i] == ',')
{
result += c[wrapIndex(29 - s - inc, 30)];
}
else
{
result += text[i];
}
}

// Return the resulting string
return result;
}


void decrypt(string text)
{
for (int i = 0; i < 30; i++)
{
cout << "Shift s: " << i << " decrypted string: " << decryptHelper(text, i) << endl;
}
}

// Driver program to test the above function
int main()
{
string text;
cout << "Enter a message to encryot or decrypt: ";
cin >> text;

string choice;
cout << "Enter 1. for encrypting this message and any other key for decryting: ";
cin >> choice;

if (choice == "1")
{
    int s;
    cout << "Enter shift : ";
    cin >> s;

    cout << "Text : " << text;
    cout << "\nShift: " << s;
    string cipher = encrypt(text, s);
    cout << "\nCipher: " << cipher << endl;

}
else
{
    cout << "Possible decryption: " << endl;
    decrypt(text);
}

return 0;
}

/*
Text : attackatonce
Shift: 4
Cipher: h..hjrh.vujlPossible decryption:
Shift s: 0 decrypted string: exxegoexsrgi
Shift s: 1 decrypted string: dwwdfndwrqfh
Shift s: 2 decrypted string: cvvcemcvqpeg
Shift s: 3 decrypted string: buubdlbupodf
Shift s: 4 decrypted string: attackatonce
Shift s: 5 decrypted string: ss bj snmbd
Shift s: 6 decrypted string: ,rr,ai,rmlac
Shift s: 7 decrypted string: ?qq? h?qlk b
Shift s: 8 decrypted string: .pp.,g.pkj,a
Shift s: 9 decrypted string: zooz?fzoji?
Shift s: 10 decrypted string: ynny.eynih.,
Shift s: 11 decrypted string: xmmxzdxmhgz?
Shift s: 12 decrypted string: wllwycwlgfy.
Shift s: 13 decrypted string: vkkvxbvkfexz
Shift s: 14 decrypted string: ujjuwaujedwy
Shift s: 15 decrypted string: tiitv tidcvx
Shift s: 16 decrypted string: shhsu,shcbuw
Shift s: 17 decrypted string: rggrt?rgbatv
Shift s: 18 decrypted string: qffqs.qfa su
Shift s: 19 decrypted string: peeprzpe ,rt
Shift s: 20 decrypted string: oddoqyod,?qs
Shift s: 21 decrypted string: nccnpxnc?.pr
Shift s: 22 decrypted string: mbbmowmb.zoq
Shift s: 23 decrypted string: laalnvlazynp
Shift s: 24 decrypted string: k kmuk yxmo
Shift s: 25 decrypted string: j,,jltj,xwln
Shift s: 26 decrypted string: i??iksi?wvkm
Shift s: 27 decrypted string: h..hjrh.vujl
Shift s: 28 decrypted string: gzzgiqgzutik
Shift s: 29 decrypted string: fyyfhpfytshj
Given cipher: f..yqjrnvrpuabefw.j,ddxgcndpxmsdehsomjkcydfygtd.rrp?dgstmvxmf.wo?jxgrneslzifrgzyqv.v
Possible decryption:
Shift s: 0 decrypted string: cxxvngoksomr?,bctxgzaaud kamujpabepljgh vacvdqaxoomyadpqjsujcxtlygudokbpiwfcodwvnsxs
Shift s: 1 decrypted string: bwwumfnjrnlq.?abswfy tc,j ltio adokifg,u bucp wnnlx copirtibwskxftcnjaohvebncvumrwr
Shift s: 2 decrypted string: avvtlemiqmkpz. arvex,,sb?i,kshn, cnjhef?t,atbo,vmmkw,bnohqshavrjwesbmi ngudambutlqvq
Shift s: 3 decrypted string: uuskdlhpljoyz, qudw??ra.h?jrgm?,bmigde.s? san?ulljv?amngprg uqivdralh,mftc latskpup
Shift s: 4 decrypted string: ,ttrjckgokinxy?,ptcv..q zg.iqfl.?alhfcdzr.,r m.tkkiu. lmfoqf,tphucq kg?lesb,k srjoto
Shift s: 5 decrypted string: ?ssqibjfnjhmwx.?osbuzzp,yfzhpekz. kgebcyqz?q,lzsjjhtz,klenpe?sogtbp,jf.kdra?j,rqinsn
Shift s: 6 decrypted string: .rrphaiemiglvwz.nratyyo?xeygodjyz,jfdabxpy.p?kyriigsy?jkdmod.rnfsao?iezjcq .i?qphmrm
Shift s: 7 decrypted string: zqqog hdlhfkuvyzmq sxxn.wdxfncixy?iec awoxzo.jxqhhfrx.ijclnczqmer n.hdyibp,zh.poglql
Shift s: 8 decrypted string: yppnf,gckgejtuxylp,rwwmzvcwembhwx.hdb, vnwynziwpggeqwzhibkmbypldq,mzgcxhao?ygzonfkpk
Shift s: 9 decrypted string: xoome?fbjfdistwxko?qvvlyubvdlagvwzgca?,umvxmyhvoffdpvyghajlaxokcp?lyfbwg n.xfynmejoj
Shift s: 10 decrypted string: wnnld.eaiechrsvwjn.puukxtauck fuvyfb .?tluwlxguneecouxfg ik wnjbo.kxeavf,mzwexmldini
Shift s: 11 decrypted string: vmmkczd hdbgqruvimzottjws tbj,etuxea,z.sktvkwftmddbntwef,hj,vmianzjwd ue?lyvdwlkchmh
Shift s: 12 decrypted string: ulljbyc,gcafpqtuhlynssivr,sai?dstwd ?yzrjsujveslccamsvde?gi?ulh myivc,td.kxucvkjbglg
Shift s: 13 decrypted string: tkkiaxb?fb eopstgkxmrrhuq?r h.crsvc,.xyqirtiudrkbb lrucd.fh.tkg,lxhub?sczjwtbujiafkf
Shift s: 14 decrypted string: sjjh wa.ea,dnorsfjwlqqgtp.q,gzbqrub?zwxphqshtcqjaa,kqtbczegzsjf?kwgta.rbyivsatih eje
Shift s: 15 decrypted string: riig,v zd ?cmnqreivkppfsozp?fyapqta.yvwogprgsbpi ?jpsabydfyrie.jvfs zqaxhur shg,did
Shift s: 16 decrypted string: qhhf?u,yc,.blmpqdhujooernyo.ex ops zxuvnfoqfraoh,,.ior axcexqhdziuer,yp wgtq,rgf?chc
Shift s: 17 decrypted string: pgge.t?xb?zaklopcgtinndqmxnzdw,nor,ywtumenpeq ng??zhnq, wbdwpgcyhtdq?xo,vfsp?qfe.bgb
Shift s: 18 decrypted string: offdzs.wa.y jknobfshmmcplwmycv?mnq?xvstldmodp,mf..ygmp?,vacvofbxgscp.wn?uero.pedzafa
Shift s: 19 decrypted string: neecyrzv zx,ijmnaergllbokvlxbu.lmp.wurskclnco?lezzxflo.?u buneawfrbozvm.tdqnzodcy e
Shift s: 20 decrypted string: mddbxqyu,yw?hilm dqfkkanjukwatzklozvtqrjbkmbn.kdyyweknz.t,atmd veqanyulzscpmyncbx,d,
Shift s: 21 decrypted string: lccawpxt?xv.ghkl,cpejj mitjv syjknyuspqiajlamzjcxxvdjmyzs? slc,udp mxtkyrbolxmbaw?c?
Shift s: 22 decrypted string: kbb vows.wuzfgjk?bodii,lhsiu,rxijmxtroph ik lyibwwucilxyr.,rkb?tco,lwsjxqankwla v.b.
Shift s: 23 decrypted string: jaa,unvrzvtyefij.anchh?kgrht?qwhilwsqnog,hj,kxhavvtbhkwxqz?qja.sbn?kvriwp mjvk ,uzaz
Shift s: 24 decrypted string: i ?tmuqyusxdehiz mbgg.jfqgs.pvghkvrpmnf?gi?jwg uusagjvwpy.pi zram.juqhvo,liuj,?ty y
Shift s: 25 decrypted string: h,,.sltpxtrwcdghy,laffziepfrzoufgjuqolme.fh.ivf,ttr fiuvoxzoh,yq lzitpgun?khti?.sx,x
Shift s: 26 decrypted string: g??zrksowsqvbcfgx?k eeyhdoeqyntefitpnkldzegzhue?ssq,ehtunwyng?xp,kyhsoftm.jgsh.zrw?w
Shift s: 27 decrypted string: f..yqjrnvrpuabefw.j,ddxgcndpxmsdehsomjkcydfygtd.rrp?dgstmvxmf.wo?jxgrneslzifrgzyqv.v
Shift s: 28 decrypted string: ezzxpiqmuqot adevzi?ccwfbmcowlrcdgrnlijbxcexfsczqqo.cfrsluwlezvn.iwfqmdrkyheqfyxpuzu
Shift s: 29 decrypted string: dyywohpltpns, cduyh.bbvealbnvkqbcfqmkhiawbdwerbyppnzbeqrktvkdyumzhveplcqjxgdpexwotyt

*/


Related Solutions

Assignment in C: prompt the user to enter secret message that is terminated by presding Enter....
Assignment in C: prompt the user to enter secret message that is terminated by presding Enter. You can assume that the the length of this message will be less than 100 characters. You will then parae this message, character by character, converting them to lower case, and find corresponding characters in the words found in the key text word array. Once a character match is found, you will write the index of the word and the index of the character...
Need to write a c program Program prints a message telling the user to push a...
Need to write a c program Program prints a message telling the user to push a key to start the game. Once in game, output tells the player which key to push. The key to press should be determined randomly. Game runs continuously until the user reaches a loose condition. A wrong key is pressed
create a c++ proram to accept a positive number in the main program. Create and call...
create a c++ proram to accept a positive number in the main program. Create and call the following functions 1. 1. OddorEven -to determine if the number is an odd or even number ( do not use $ or modul opeartor) and will return " even number " or " odd number ". 2. Factorial - to compute the factorial of N. 3. Power - to compute the power of N without using pow function. display the result in the...
Create a C program that performs the following (please comment the codes): a) Create a Stack...
Create a C program that performs the following (please comment the codes): a) Create a Stack ADT. Stack should be implemented using the linked list. b) Enter 10 random integer numbers between 0 to 50 in the stack. c) After pushing each element, print the content of the top of the stack. c) Then pop out those 10 integer numbers and print those numbers. d) Finally destroy the Stack.
Create a C++ program that will accept any number of grades for an exam. The grades...
Create a C++ program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category. Using arrays.
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much...
Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much stuff a truck can carry, in pounds. Then ask them for the weight of each thing they add until they stop. Don't forget to be safe.
Create JAVA PROGRAM, and write comment for codes also. 1) Let the user enter in candidate...
Create JAVA PROGRAM, and write comment for codes also. 1) Let the user enter in candidate names for a ballot. Then imagine the program is moved to a voting booth. Each voter enters their unique name and who they vote for, and when there are no more voters display who won. (Imagine something outside your program is telling it there are no more voters.)
Write a program that accept an integer input from the user and display the least number...
Write a program that accept an integer input from the user and display the least number of combinations of 500s, 100s, 50s, 20s, 10s, 5s, and 1s. Test your solution using this samples] a. Input: 250 Output: 1x200s, 1x50s b. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s c. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s d. Input: 19 Output: 1x10s, 1x5s, 4x1s ​[Hints] o Use division to determine the number of occurrence of each element (i.e. 200, 100)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT