-1

我有这个函数可以用某个密钥解密凯撒加密的消息,它给了我正确的输出字母,但后面有一些奇怪的空格。

这是功能:

#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▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒
▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒ ▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒ ▒▒▒▒

关于这个有什么想法吗?

4

2 回答 2

0

您的问题是您正在将字符串“一直”中的值更改为MAXMSGLEN. 现在发生的事情是您解密消息的主体(例如 10 个字符)并且不要停止而是继续前进,更新超出范围的值。

解决方法是在您看到“字符串结尾”空终止符后停止'\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);
        }
    }
  }
}

或者,如果您更喜欢可读性更高的代码(不要在循环的控制块中塞进太多内容):

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);
        }
    }
  }
}

更新65:避免使用像and之类的“幻数” 90。如果您使用'A'and'Z'代替,您的代码会更容易理解。这是固定的解密函数:

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
        }
    }
  }
}
于 2015-09-24T22:12:35.580 回答
0

只需将字符串作为 a 传递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);
      }
    }
  }
}

这种方式也传递了有关其长度的信息,您可以简单地对字符进行基于范围的循环。

于 2015-09-24T22:12:46.977 回答