Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

c - Vigenere Cipher

I am trying to make Vigenere Cipher in C. https://www.youtube.com/watch?v=9zASwVoshiM this is info about Vigenere Cipher. My code works doesnt work for certain cases like encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword instead it encrypts it as xomd, szz fl. Another example is: encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword but instead it encrypts it as CakGo. My code is given below please help me out:

#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!
");
        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.
");
            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: 
");
    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("
");

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
string ciphertext = key;

ciphertext should start out as blank, it should not be set to key.

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

This is not correct because ciphertext[u] can go out of range. Character should be between 'a' to 'z'. Use the mod % operator to make sure character is within range. For example:

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
", ciphertext);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...