解密函数的奇怪输出
Weird output for decipher 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 == '[=10=]')
{
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] != '[=10=]'; 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] != '[=10=]'; j++){
msg[j] = cip[j];
}
Decipher(msg, i);
cout<< msg <<endl;
}
return '[=10=]';
}
这是我必须遍历的加密密钥:
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▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒
▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒
对此有什么想法吗?
您的问题是您正在将字符串 "all the way" 中的值更改为 MAXMSGLEN
。现在发生的事情是你解密消息的主体(比如 10 个字符)并且不要停止但继续前进,更新范围之外的值。
解决方法是在看到 "end-of-string" 空终止符后停止 '[=14=]
:
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN && cip[i] != '[=10=]'; i++){
if (cip[i] != ' '){
cip[i] = char(cip[i] - ((key - 'A')% 26));
if (cip[i] < 65){
cip[i] = ((65 - cip[i])-90);
}
}
}
}
或者,如果您更喜欢可读性更强的代码(不要在循环的控制块中塞入太多内容):
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN; i++){
if(cip[i] == '[=11=]'){
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);
}
}
}
}
更新:避免像65
和90
一样使用"magic numbers"。如果您改用 'A'
和 'Z'
,您的代码会更容易理解。这是固定的解密函数:
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN; i++){
if(cip[i] == '[=12=]'){
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
}
}
}
}
只需将字符串作为 string
:
传递
void Decipher(std::string& cip, char key) {
for (char& ch : cip) {
if (ch != ' ') {
ch = char(ch - ((key - 'A') % 26));
if (ch < 65) {
ch = ((65 - ch)-90);
}
}
}
}
这种方式也传递了有关其长度的信息,您可以简单地对字符进行基于范围的循环。
我有这个函数可以用特定的密钥解密凯撒加密的消息,它为我提供了正确的输出字母,但它后面有一些奇怪的空格。
函数如下:
#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 == '[=10=]')
{
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] != '[=10=]'; 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] != '[=10=]'; j++){
msg[j] = cip[j];
}
Decipher(msg, i);
cout<< msg <<endl;
}
return '[=10=]';
}
这是我必须遍历的加密密钥:
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▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒
▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒
对此有什么想法吗?
您的问题是您正在将字符串 "all the way" 中的值更改为 MAXMSGLEN
。现在发生的事情是你解密消息的主体(比如 10 个字符)并且不要停止但继续前进,更新范围之外的值。
解决方法是在看到 "end-of-string" 空终止符后停止 '[=14=]
:
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN && cip[i] != '[=10=]'; i++){
if (cip[i] != ' '){
cip[i] = char(cip[i] - ((key - 'A')% 26));
if (cip[i] < 65){
cip[i] = ((65 - cip[i])-90);
}
}
}
}
或者,如果您更喜欢可读性更强的代码(不要在循环的控制块中塞入太多内容):
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN; i++){
if(cip[i] == '[=11=]'){
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);
}
}
}
}
更新:避免像65
和90
一样使用"magic numbers"。如果您改用 'A'
和 'Z'
,您的代码会更容易理解。这是固定的解密函数:
void Decipher(char cip[], char key){
for(int i = 0; i < MAXMSGLEN; i++){
if(cip[i] == '[=12=]'){
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
}
}
}
}
只需将字符串作为 string
:
void Decipher(std::string& cip, char key) {
for (char& ch : cip) {
if (ch != ' ') {
ch = char(ch - ((key - 'A') % 26));
if (ch < 65) {
ch = ((65 - ch)-90);
}
}
}
}
这种方式也传递了有关其长度的信息,您可以简单地对字符进行基于范围的循环。