解密函数的奇怪输出

时间:2015-09-24 22:04:53

标签: c++ caesar-cipher

我有这个函数用一个密钥解密一个caesar加密的消息,它给了我正确的输出字母,但是后面有一些奇怪的黑色空格。

这是功能:

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

关于这个的任何想法?

2 个答案:

答案 0 :(得分:0)

您的问题是您正在更改字符串中的值&#34;一直&#34;到MAXMSGLEN。现在发生的事情是你解密消息的主体(比如10个字符)并且不要停止但继续前进,更新范围之外的值。

修复是在看到&#34;字符串结尾&#34; 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);
        }
    }
  }
}

或者,如果您更喜欢更易读的代码(不要在循环和控制块中过多地填写):

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

更新:避免使用&#34;魔术数字&#34;例如6590。如果您使用'A''Z',您的代码将更容易理解。这是固定的decypher函数:

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

答案 1 :(得分:0)

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

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