In: Computer Science
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
#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
*/