2016-04-16 9 views
-2

私はVigenere CipherをCで作成しようとしています。https://www.youtube.com/watch?v=9zASwVoshiMこれはVigenere Cipherに関する情報です。私のコードは暗号化のような特定のケースでは動作しません "世界、こんにちは! "xoqmd、rby gflkp!"と表示されます。代わりにキーワードとして "baz"を使用し、それをxomd、szz flとして暗号化します。もう一つの例は: は "BaZ"をキーワードとして "CaQGon"として "BaRFoo"を暗号化しますが、代わりにCakGoとして暗号化します。私のコードは、私を助けてください以下の通りである:Vigenere Cipher

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

int main(int argc, string argv[]) { 
    //string plaintext; 

    string key; 
    if (argc != 2) { 
     printf("Please run the programme again this time using a command line argument!\n"); 
     return 1; 
    } 
    key = argv[1]; 
    int keys[strlen(key)]; 

    for (int m = 0; m< strlen(key); m++) { 
     if (isalpha(key[m]) == false) { 
      printf("Re-Run The programme without any symbols.\n"); 
      return 1; 
     } 
    } 

    for (int b = 0; b < strlen(key); b++) { 
     if (isupper(key[b]) == false) { 
      keys[b] = key[b] - 'a'; 
     } 
     else { 
      keys[b] = key[b] - 'A'; 
     } 
    } 

    //printf("Enter a string which should be encrypted: \n"); 
    string plaintext = GetString(); 
    int plength = strlen(plaintext); 
    int klength = strlen(key); 
    string ciphertext = key; 

    for (int u = 0; u<plength; u++) { 

     if (isalpha(plaintext[u]) == false) { 
      printf("%c", plaintext[u]); 
      continue; 
     } 

     int value = u % klength; 
     ciphertext[u] = (keys[value] + plaintext[u]); 
     if ((islower(plaintext[u])) && (ciphertext[u])>'z') { 
      ciphertext[u] = ciphertext[u] - 'z' + 'a' - 1; 
     } 

     if ((isupper(plaintext[u])) && (ciphertext[u])>'z') { 
      ciphertext[u] = ciphertext[u] - 'Z' + 'A' - 1; 
     } 

     printf("%c", ciphertext[u]); 
    } 

    printf("\n"); 

    return 0; 
} 
+0

1)'文字列暗号文=キーとして "BaRFooを" 暗号化キー "バズ" と(isupper(plaintext [u]))&&(暗号文[u])> 'z'){' - >' if(isupper(plaintext [u] )&&暗号文[u]> 'Z'){' – BLUEPIXY

+1

インデントはあなたの友人です。 –

答えて

2
string ciphertext = key; 

ciphertext空白として出始めるべきですが、それがキーに設定すべきではありません。

ciphertext[u] = ciphertext[u] - 'z' + 'a' - 1; 

ciphertext[u]が範囲外になる可能性があるため、これは正しくありません。文字は 'a'〜 'z'の間でなければなりません。 mod %演算子を使用して、文字が範囲内にあることを確認します。たとえば、次のように動的に割り当てられたとplaintextのコピーとして開始するために少し問題(ciphertextニーズの

int j = 0; 
for (int u = 0; u<plength; u++) 
{ 
    int c = plaintext[u]; 
    if (isalpha(plaintext[u])) 
    { 
     int k = keys[j % klength]; 
     if (islower(c)) 
     { 
      c = 'a' + (c - 'a' + k) % 26; 
     } 
     else 
     { 
      c = 'A' + (c - 'A' + k) % 26; 
     } 
     j++; 
    } 
    ciphertext[u] = c; 
} 
printf("%s\n", ciphertext); 
2

たくさん、ないkey;間違った計算;印刷エラーに対するニーズアルファベットの長さにMODの計算に)、多数の小さな最適化を行うことができます(ループを結合します; 句を組み合わせて、先に可変長に保存してください)。あなたのコードを改訂しました:

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

int main(int argc, string argv[]) { 

    if (argc != 2) { 
     fprintf(stderr, "Please run the program again with a command line argument!\n"); 
     return 1; 
    } 

    string key = argv[1]; 
    int key_length = strlen(key); 
    int keys[key_length]; 

    for (int i = 0; i < key_length; i++) { 
     if (!isalpha(key[i])) { 
      fprintf(stderr, "Re-run The program without any symbols.\n"); 
      return 1; 
     } 

     keys[i] = toupper(key[i]) - 'A'; 
    } 

// printf("Enter a string which should be encrypted: \n"); 

    string plaintext = GetString(); 
    int text_length = strlen(plaintext); 
    string ciphertext = strcpy(malloc(text_length + 1), plaintext); 

    for (int i = 0, j = 0; i < text_length; i++) { 
     if (!isalpha(plaintext[i])) { 
      continue; 
     } 

     int index = j++ % key_length; 

     ciphertext[i] = (toupper(plaintext[i]) - 'A' + keys[index]) % (1 + 'Z' - 'A'); 

     ciphertext[i] += isupper(plaintext[i]) ? 'A' : 'a'; 
    } 

    printf("%s\n", ciphertext); 

    free(ciphertext); 

    return 0; 
} 

"world、say hello!" "xoqmd、rby gflkp!"と表示されます。 > `はunsigned char *暗号文=のmalloc(PLENGTH) - `; `(

キーと "CaQGon" "バズ"

関連する問題