簡體   English   中英

凱撒密碼問題(C)

[英]Caesar Cipher issue (C)

此代碼的目的是創建一個Caesar密碼,只用3個密鑰對字母數字字符進行編碼,其中第一個字母由第一個鍵增加,第二個字符由第二個鍵增加,第三個字符由第三個鍵增加,第四個字符由第一個鍵增加密鑰等...用環繞(Z + 1 - > a),(a - 1 - > Z)。

我完全完成了作業,唯一的問題是我的負面包裹不起作用(a - 1 - > Z)。 它要求輸入密鑰,接受輸入,然后不返回任何內容(但仍然允許用戶鍵入並按Enter鍵而不會產生任何結果)。 這是我的代碼:

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

我發現了錯誤。 我的負循環for negative wrap將值分配給key1而不是key2 / 3。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM