2015-11-01 94 views
0

我對Vigenere有點問題,並希望得到一些幫助。CS50 Vigenere - 奇怪的圖案

/* 
This program is a Vigenere Cipher. 
I am Daniel of Asguard. 
*/ 

#include <cs50.h> 
#include <ctype.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main(int argc, string argv[]) 
{ 
string cipher; //this is a placeholder for the ciphered message. 
char * key = argv[1]; 
int i = 0; 


if (argc != 2) //this is meant to trigger if you don't enter the right call. So 
{ 
    printf("Please enter the cipher key when you call the program, such as './CaesarCipher 7'.\n"); // 
    return 1; 
} 

if (!isalpha(key[i])) //this is meant to trigger if you don't enter the right call. So 
{ 
    printf("Please only enter a word, no numerical numbers please."); // 
    return 1; 
} 

do 
{ 
    //printf("Please enter the message you would like to have converted, please. \n"); 
    cipher = GetString(); 
} 
while (cipher == NULL); 

for (int i = 0, k = 0, n = strlen(cipher); i < n; i++, k++) //this is so the code knows to change only the characters in the sting cipher. 
    { 
     if (k >= strlen(key)) 
     { 
      k = 0; 
     } 
      { 
      if (isupper(cipher[i])) 
       { 
        //cipher[i] = 'A' + (((cipher[i] - 'A') + (key[k]) - 'A') % 26); 
        cipher[i] = ((key[k] - 65) + (cipher[i] - 65)) % 26; 
        printf("%s\n", cipher); 
       } 
      else (islower(cipher[i])); 
       { 
        //cipher[i] = 'a' + (((cipher[i] - 'a') + (key[i]) - 'a') % 26); 
        cipher[i] = ((key[k] - 97) + (cipher[i] - 97)) % 26; 
        printf("%s\n", cipher); 
       } 
     } 
    } 
printf("%s\n", cipher); 
return 0; 
} 

當我做我的結果得到奇怪的字符:⎽c▒⎺e┼├⎼▒┤└e⎼@☃de5▮:·/┬⎺⎼┐⎽⎻▒ce/⎻⎽e├ 2 $└▒┐e┴☃±e┼e⎼e於完成後,我的終端所有的信件。

我的結果落得這樣看的巴茲:注意

  • 任何
  • nything注
  • ything注
  • ything注
  • 事情注
  • 備註
  • 備註
  • NG注注
  • F注意
  • F注意
  • OTE
  • OTE
  • OTE
  • ├e
  • 電子商務

回答

0

以下工作如預期對ACC例子的代碼在https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher

#include <ctype.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main (void) 
{ 
    const char *cipher = "ATTACKATDAWN"; 
    const char *key = "LEMON"; 
    char *newkey; 
    char *p, *q; 
    int i =0; 
    int col, row; 
    if (strlen (key) < strlen (cipher)) 
    { 
printf ("key %s \n", key); 
printf ("cipher \t%s \n", cipher); 
newkey = malloc (strlen (cipher) +1); 
strcpy (newkey, key); 
p = (char *) (newkey + strlen (newkey)) ; 
q = (char *) key; 
i =strlen (key); 
while (i < strlen (cipher)) 
{ 
     i++; 
     if (*q == 0) 
    q = (char *) key; 
     *p = *q; 
     p++; 
     q++; 
} 
    *p = 0; 
    printf ("newk \t%s \n", newkey); 
    } 
    p = (char *) newkey; 
    q= (char *) cipher; 
    int a[1] ; 
    a[1] =0; 
    for (i =0 ; i < strlen(newkey); i++) 
    { 
row = *p -65; 
col = *q -65; 
if (col+row > 26) 
     a [0] = 65 + col+row -26; 
else 
     a [0] = 65 +col+row; 
printf ("%s",(char *) &a[0]); 
p++; 
q++; 
    } 
    printf ("\n"); 
    free(newkey); 
/*L X F O P V E F R N H R*/ 
return 0; 
} 
0

就行了:

cipher[i] = ((key[k] - 65) + (cipher[i] - 65)) % 26; 

你忘了加回65(的A的ASCII代碼)結果。因此,cipher[i]將是一個從0到25的字符,它們都是不可打印的控制代碼(特別是,空字符/ 0字符被C字符串函數視爲字符串結束標記)。

線:

cipher[i] = ((key[k] - 97) + (cipher[i] - 97)) % 26; 

具有相同的錯誤。

另外,在兩行中,假設關鍵字符與您正在加密的消息字符具有相同的大小寫。如果情況並非如此,您的加密結果將不正確。爲了解決這個問題,我建議將整個密鑰轉換爲主要加密循環之前的所有上(或下)情況之前的

您也可以更進一步,在循環前將鍵轉換爲0到25之間的數字。但是,執行此操作後,您將無法在密鑰上使用strlen(),因爲strlen()會在字符串中查找第一個空(= 0)字符的位置。相反,在轉換密鑰之前,您必須運行strlen(),並將其結果保存在變量中供以後使用。實際上,這在任何情況下都是非常有用的優化(你也應該這樣做)。