简体   繁体   English

凯撒密码问题(C)

[英]Caesar Cipher issue (C)

The purpose of this code is to create a Caesar cipher that only encodes alphanumeric characters with 3 keys, where the 1st letter is incremented by the 1st key, 2nd character by the 2nd key, 3rd character by the 3rd key, 4th character by the 1st key etc... with wrap around (Z + 1 --> a), (a - 1 --> Z). 此代码的目的是创建一个Caesar密码,只用3个密钥对字母数字字符进行编码,其中第一个字母由第一个键增加,第二个字符由第二个键增加,第三个字符由第三个键增加,第四个字符由第一个键增加密钥等...用环绕(Z + 1 - > a),(a - 1 - > Z)。

I've completed the assignment completely, the only issue I have is that my negative wrap around doesn't work (a - 1 --> Z). 我完全完成了作业,唯一的问题是我的负面包裹不起作用(a - 1 - > Z)。 It asks for the keys, takes input, and then returns nothing (but still allows the user to type and hit enter without any result). 它要求输入密钥,接受输入,然后不返回任何内容(但仍然允许用户键入并按Enter键而不会产生任何结果)。 Here is my code: 这是我的代码:

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

char sentence[101] = { '\0' };
char alphabet[52]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

int locateletter(char tolocate){
    int i=0;
    for(i=0; i<52; i++){
            if(alphabet[i] == tolocate)
                    return i;
    }
}

int main(void){
    int key1 = 0;
    int key2 = 0;
    int key3 = 0;
    printf("Sentence: ");
    scanf("%101[^\n]", sentence);
    if( sentence[100] != '\0' ){
        printf("You entered more than 100 characters. Block Caesar Cipher is exiting. Goodbye.\n");
        exit;
    }else{
        printf("Keys: ");

        scanf("%d %d %d", &key1, &key2, &key3);
        int i=0;
        for(i=0; i<100; i=i+3){
                if(isalpha(sentence[i])){
                        int position = locateletter(sentence[i]);
                        while((position+key1)>51){
                                key1 = position+key1-52;
                                position=0;
                        }
                        while((position+key1)<0){
                                key1 = key1+position+52;
                                position=0;
                        }
                        sentence[i] = alphabet[position+key1];
                }
        }
        int k=1;
        for(k=1; k<100; k=k+3){
                if(isalpha(sentence[k])){
                        int position = locateletter(sentence[k]);
                        while((position+key2)>51){
                                key2 = position+key2-52;
                                position=0;
                        }
                        while((position+key2)<0){
                                key1 = key2+position+52;
                                position=0;
                        }
                        sentence[k] = alphabet[position+key2];
                }
        }
        int t=2;
        for(t=2; t<100; t=t+3){
                if(isalpha(sentence[t])){
                        int position = locateletter(sentence[t]);
                        while((position+key3)>51){
                                key3 = position+key3-52;
                                position=0;
                        }
                        while((position+key3)<0){
                                key1 = key3+position+52;
                                position=0;
                        }
                        sentence[t] = alphabet[position+key3];
                }
        }
        printf("Cipher: %s\nDone.\n", sentence);

    }
    return 0;
}

I found the error. 我发现了错误。 My while loop for negative wrap around assigns the values to key1 instead of key2/3. 我的负循环for negative wrap将值分配给key1而不是key2 / 3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM