2016-04-16 13 views
-2

Ich versuche, Vigenere Cipher in C. https://www.youtube.com/watch?v=9zASwVoshiM das ist info über Vigenere Cipher. Mein Code arbeitet nicht für bestimmte Fälle wie verschlüsselt "Welt, sagen Hallo!" als "xoqmd, rby gflkp!" Verwenden Sie stattdessen "baz" als Schlüsselwort und verschlüsseln es als xomd, szz fl. Ein anderes Beispiel ist: verschlüsselt "BaRFoo" als "CaQGon" mit "BaZ" als Schlüsselwort, verschlüsselt es aber stattdessen als CakGo. Mein Code ist unten bitte helfen Sie mir heraus gegeben: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) 'Zeichenfolge ciphertext = key;' -> 'unsigned char * ciphertext = malloc (PLength);' (Später mache 'frei (ciphertext); ') 2)' if ((isupper (Klartext [u])) && (Chiffretext [u])>' z ') {'->' if (isupper (Klartext [u]) && ciphertext [u]> 'Z') {' – BLUEPIXY

+1

Einrückung ist dein Freund. –

Antwort

2
string ciphertext = key; 

ciphertext sollte leer ausgehen, sollte es nicht zu Taste eingestellt werden.

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

Dies ist nicht korrekt, da ciphertext[u] außer Reichweite gehen kann. Der Buchstabe sollte zwischen 'a' und 'z' liegen. Verwenden Sie den Operator mod %, um sicherzustellen, dass sich das Zeichen innerhalb des Bereichs befindet. Zum Beispiel:

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

Viele kleine Probleme (ciphertext Bedürfnisse werden dynamisch und beginnen als eine Kopie von plaintext zugeordnet, nicht key; Notwendigkeit, mod Berechnungen der Länge des Alphabets, falsche Berechnungen, Druckfehler stderr) und viele kleine Optimierungen, die gemacht werden können (kombinieren Sie Schleifen; kombinieren Sie if Klauseln; speichern Sie key Länge auf eine Variable früher). Eine Überarbeitung Ihres Codes:

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

Verschlüsselt "Welt, sag Hallo!" als "xoqmd, rby gflkp!" mit der Taste "Baz"

Verschlüsselt "BaRFoo" as "CaQGon" mit der Taste "BaZ"