我有这个函数可以用某个密钥解密凯撒加密的消息,它给了我正确的输出字母,但后面有一些奇怪的空格。
这是功能:
#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';
}
这是我必须循环的加密密钥:
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"
};
仅供参考,我需要保持参数不变。我不能将它们更改为字符串或类似的东西。
如果我通过黑色空间蛮力循环进入的话
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▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒
▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒
关于这个有什么想法吗?