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...
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.
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer...
PLEASE DO IN C++ Create an object-oriented program that initially allows the user to save customer information in the list and to search for a customer by specifying the customer’s ID. Sample Run Customer Information Management System --------------------------------------------------------------- CUSTOMER DATA ENTRY: Full Name (First, Last): Jenny Ha Company: Convergent Laser Technologies Street: 1000 International Ave City: Oakland State: CA Zip Code: 94506 ID: 100 Continue Your Data Entry? (y/n): y Full Name (First, Last): Bill Martinez Company: Cisco Systems Street:...
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)...
2) Write a C++ program that accepts a sentence as an input from the user. Do...
2) Write a C++ program that accepts a sentence as an input from the user. Do the following with the sentence. Please use C++ style string for this question. 1) Count the number of letters in the input 2) Change all lower case letters of the sentence to the corresponding upper case
Create a working C# Program that will accept an input and has the following class. areaCircle...
Create a working C# Program that will accept an input and has the following class. areaCircle – computes the area of the circle volumeCube – computes the volume of a cube perimeterTraingle – computes the perimeter of a triangle surfaceAreaRect – computes the surface area of a rectangle *You may usePass by Value and/or Pass by Reference *Using ConsoleApp
Write a program in C that lets the user enter a message using SMS Language(e.g. lol,...
Write a program in C that lets the user enter a message using SMS Language(e.g. lol, omg, omw etc.), then translates it into English (e.g. laughing out loud, oh my god, on my way etc.). Also provide a mechanism to translate text written in English into SMS Language. needs to be able to translate at least 10 sms words
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
Using MatLab Write a program which will: a. Accept two numbers from the user m and...
Using MatLab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Accept from the user two row numbers. Multiply the two rows (element by element) and find the sum. Print the result. d. Accept from the user two column numbers. Add the two columns (element by element) and find the sum....
Creating a list/tuple 4. Write a program to accept a string from the user, then replace...
Creating a list/tuple 4. Write a program to accept a string from the user, then replace the first letter with the last letter, the last letter with the middle letter and the middle letter with the first letter. E.g. “Billy” -> “yiBll”.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT