I have this function that decrypts a caesar encrypted message with a certain key and it gives me the right letters for the output but theres some weird blackspace after it.
Here's the function:
#include <iostream>
#include <string.h>
#include "proj1.h"
#include <cstdlib>
using namespace std;
int main()
{
char dec[MAXMSGLEN];
int i = 0;
while(i < NUMMSGS){
char solve = SolveCipher(cipher[i], dec);
if(solve == '\0')
{
cout << "Message #" << (i + 1) << " was not intertesting. " <<endl;
}
else
{
cout << "Message #" << (i + 1) << ",Key " << solve << endl;
cout << "\t" << dec << endl;
}
i++;
}
return 0;
}
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN && cip[i] != '\0'; i++){
if (cip[i] != ' '){
cip[i] = char(cip[i] - ((key - 'A')% 26));
if (cip[i] < 65){
cip[i] = ((65 - cip[i])-90);
}
}
else{
cip[i] = cip[i];
}
}
}
char SolveCipher(const char cip[], char dec[]){
char msg[MAXMSGLEN];
char check[MAXMSGLEN];
char word[MAXCRIBLEN];
//Deciphers message with every key
for(char i = 'A'; i <= 'Z'; i++){
//clears msg
memset(msg, 0, (sizeof(msg)/sizeof(msg[0])));
//stores cip in msg
for(int j = 0; cip[j] != '\0'; j++){
msg[j] = cip[j];
}
Decipher(msg, i);
cout<< msg <<endl;
}
return '\0';
}
Here's the encrypted keys I have to loop through:
const char cipher[NUMMSGS][MAXMSGLEN] = {
"HAAHJR HA KHDU AVTVYYVD",
"DHFGS NBKNBJ ZMC ZKK HR VDKK",
"Q PIDM JQMJMZ NMDMZ",
"JCTFGT DGVVGT HCUVGT UVTQPIGT",
"LRPYE I HTWW XPPE JZF LE ESP NZXXZYD",
"KLSQ GML LGG DSLW YGL FGLZAF AF EQ TJSAF",
"QEBC GUR ZVPEBSVYZ ORUVAQ GUR FGNGHR",
"GZSGZWD NX XTRJBMJWJ JQXJ FY UWJXJSY",
"RZVOCZM AJM OJHJMMJR HJMIDIB RVMH RDOC GJR XGJPYN",
"ROBO MYWO LKN XOGC DKVUSXQ DRSC KXN DRKD"
};
And FYI I need to keep the parameters as they are now. I cannot change them to strings or anything like that.
If I brute force loop through the black spaces come inside the words
ROBO MYWO LKN XOGC DKVUSXQ DRSC KXN DRKD
QNAN LXVN KJM WNFB CJUTRWP CQRB JWM CQJC
PM▒M KWUM JIL VMEA BITSQVO BPQA IVL BPIB
OL▒L JVTL IHK ULD▒ AHSRPUN AOP▒ HUK AOHA
NK▒K IUSK HGJ TKC▒ ▒GRQOTM ▒NO▒ GTJ ▒NG▒
MJ▒J HTRJ GFI SJB▒ ▒FQPNSL ▒MN▒ FSI ▒MF▒
LI▒I GSQI FEH RIA▒ ▒EPOMRK ▒LM▒ ERH ▒LE▒
KH▒H FRPH EDG QH▒▒ ▒DONLQJ ▒KL▒ DQG ▒KD▒
JG▒G EQOG DCF PG▒▒ ▒CNMKPI ▒JK▒ CPF ▒JC▒
IF▒F DPNF CBE OF▒▒ ▒BMLJOH ▒IJ▒ BOE ▒IB▒
HE▒E COME BAD NE▒▒ ▒ALKING ▒HI▒ AND ▒HA▒
GD▒D BNLD A▒C MD▒▒ ▒▒KJHMF ▒GH▒ ▒MC ▒G▒▒
FC▒C AMKC ▒▒B LC▒▒ ▒▒JIGLE ▒FG▒ ▒LB ▒F▒▒
EB▒B ▒LJB ▒▒A KB▒▒ ▒▒IHFKD ▒EF▒ ▒KA ▒E▒▒
DA▒A ▒KIA ▒▒▒ JA▒▒ ▒▒HGEJC ▒DE▒ ▒J▒ ▒D▒▒
C▒▒▒ ▒JH▒ ▒▒▒ I▒▒▒ ▒▒GFDIB ▒CD▒ ▒I▒ ▒C▒▒
B▒▒▒ ▒IG▒ ▒▒▒ H▒▒▒ ▒▒FECHA ▒BC▒ ▒H▒ ▒B▒▒
A▒▒▒ ▒HF▒ ▒▒▒ G▒▒▒ ▒▒EDBG▒ ▒AB▒ ▒G▒ ▒A▒▒
▒▒▒▒ ▒GE▒ ▒▒▒ F▒▒▒ ▒▒DCAF▒ ▒▒A▒ ▒F▒ ▒▒▒▒
▒▒▒▒ ▒FD▒ ▒▒▒ E▒▒▒ ▒▒CB▒E▒ ▒▒▒▒ ▒E▒ ▒▒▒▒
▒▒▒▒ ▒EC▒ ▒▒▒ D▒▒▒ ▒▒BA▒D▒ ▒▒▒▒ ▒D▒ ▒▒▒▒
▒▒▒▒ ▒DB▒ ▒▒▒ C▒▒▒ ▒▒A▒▒C▒ ▒▒▒▒ ▒C▒ ▒▒▒▒
▒▒▒▒ ▒CA▒ ▒▒▒ B▒▒▒ ▒▒▒▒▒B▒ ▒▒▒▒ ▒B▒ ▒▒▒▒
▒▒▒▒ ▒B▒▒ ▒▒▒ A▒▒▒ ▒▒▒▒▒A▒ ▒▒▒▒ ▒A▒ ▒▒▒▒
▒▒▒▒ ▒A▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒
▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒
Any ideas on this one?
Your problem is that you are changing the values in a string "all the way" to the MAXMSGLEN
. Now what happens is you decrypt the main body of the message (say 10 characters) and don't stop but keep on going, updating the values outside of range.
The fix is to stop once you see the "end-of-string" null-terminator '\0
:
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN && cip[i] != '\0'; i++){
if (cip[i] != ' '){
cip[i] = char(cip[i] - ((key - 'A')% 26));
if (cip[i] < 65){
cip[i] = ((65 - cip[i])-90);
}
}
}
}
Or, if you prefer a more readable code (don't cram too much into the loops' control block):
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN; i++){
if(cip[i] == '\0'){
break; // end the loop once we reached the string terminator
}
if (cip[i] != ' '){
cip[i] = char(cip[i] - ((key - 'A')% 26));
if (cip[i] < 65){
cip[i] = ((65 - cip[i])-90);
}
}
}
}
Update: Avoid using "magic numbers" like 65
and 90
. your code would be way easier to understand if you use 'A'
and 'Z'
instead. Here's the fixed decypher function:
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN; i++){
if(cip[i] == '\0'){
break; // end the loop once we reached the string terminator
}
if (cip[i] != ' '){
cip[i] = char(cip[i] - ((key - 'A')% 26));
if (cip[i] < 'A'){
cip[i] = ('Z' - ('A' - cip[i])); // <-- this is where you were getting negative values
}
}
}
}