English 中文(简体)
我已撰写了C区法。 它正确地对上案的函件进行加密/加密,但错误地将加密/加密放在下级文字上。
原标题:I have written the code of affine cipher in C. It correctly encrypts/decrypts the uppercase letters but incorrectly encrypts/decrypts lowercase letter
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

# define MAX_LENGTH 100 

int GCD(int a, int b) {
    if(b == 0) {
        return a;
    }
    GCD(b, a%b);
}

struct Pair{
    int x, y;
};

struct Pair Extended_Euclidean_Algorithm(int a, int b) {
    if(b == 0) {
        struct Pair answer;
        answer.x = 1;
        answer.y = 0;
        return answer;
    }

    struct Pair small_answer = Extended_Euclidean_Algorithm(b, a%b);
    struct Pair answer;
    answer.x = small_answer.y;
    answer.y = small_answer.x - (a/b)*small_answer.y;

    return answer;
}

void Encryption(char Plaintext[], char Ciphertext[], int a, int b) {
    int temp_1, temp_2, i;
    i = 0;

    while(Plaintext[i] !=   ) {
        temp_1 = Plaintext[i];

        if(temp_1 >= 65 && temp_1 <= 90) {
            temp_2 = (a*temp_1 + b)%26;
            Ciphertext[i++] =  A  + temp_2;
        }else if(temp_1 >= 97 && temp_1 <= 122) {
            temp_2 = (a*temp_1 + b)%26;
            Ciphertext[i++] =  a  + temp_2;
        } else if(temp_1 == 32) {
            Ciphertext[i++] =    ;
        }
    }

    Ciphertext[i] =   ;
}

void Decryption(char Ciphertext[], char Decrypted_Plaintext[], int a, int b) {
    struct Pair inverse;
    int temp_a_inverse;
    inverse = Extended_Euclidean_Algorithm(26, a);
    temp_a_inverse = inverse.y;

    if(temp_a_inverse < 0) {
        temp_a_inverse = temp_a_inverse + 26;
    }

    int temp_1, temp_2, i;
    i = 0;

    while(Ciphertext[i] !=   ) {
        temp_1 = Ciphertext[i];

        if(temp_1 >= 65 && temp_1 <= 90) {
            temp_2 = ((temp_1 + 26 - b)*temp_a_inverse)%26;
            Decrypted_Plaintext[i++] =  A  + temp_2;
        } else if(temp_1 >= 97 && temp_1 <= 122) {
            temp_2 = ((temp_1 + 26 - b)*temp_a_inverse)%26;
            Decrypted_Plaintext[i++] =  a  + temp_2;
        } else if(temp_1 == 32) {
            Decrypted_Plaintext[i++] =    ;
        }
    }

    Decrypted_Plaintext[i] =   ;
}

int main() {
    char Plaintext[MAX_LENGTH], Ciphertext[MAX_LENGTH], Decrypted_Plaintext[MAX_LENGTH];
    char c;
    printf("Enter the plaintext: ");
    int i = 0;

    while((c = getchar()) !=  
 ) {
        Plaintext[i] = c;
        i++;
    }

    Plaintext[i] =   ;
    printf("Choose any number from the given numbers: ");
    int a, b, temp_a;

    for(int j = 1; j <= 26; j++) {
        temp_a = GCD(26, j);

        if(temp_a == 1){
            printf("%d |", j);
        }
    }

    printf("
");
    scanf("%d", &a);
    srand(time(0));
    b = (rand()%(26)) + 1;

    Encryption(Plaintext, Ciphertext, a, b); //Encrypting Plaintext
    Decryption(Ciphertext, Decrypted_Plaintext, a, b); //Decrypting Ciphertext
    printf("The Plaintext is: %s
", Plaintext);
    printf("a = %d || b = %d
", a, b);
    printf("The Ciphertext is: %s
", Ciphertext);
    printf("The Decrypted Plaintext is: %s
", Decrypted_Plaintext);
    return 0;
}

我已撰写了C区法。 它正确地对上案的函件进行加密/加密,但错误地将加密/加密放在下级文字上。 因此,当我用上层字母对它进行测试时,每当我试图用低级信函对其进行测试时,它就会失败。 因此,任何人都能够告诉我我我,我在那里会错。

问题回答

Trying out your code with an uppercase word and a lowercase word netted the undesired behavior with the lowercase word.

craig@Vera:~/C_Programs/Console/Decipher/bin/Release$ ./Decipher 
Enter the plaintext: HELLO
Choose any number from the given numbers: 1 |3 |5 |7 |9 |11 |15 |17 |19 |21 |23 |25 |
3
The Plaintext is: HELLO
a = 3 || b = 12
The Ciphertext is: ULGGP
The Decrypted Plaintext is: HELLO
craig@Vera:~/C_Programs/Console/Decipher/bin/Release$ ./Decipher 
Enter the plaintext: Hello
Choose any number from the given numbers: 1 |3 |5 |7 |9 |11 |15 |17 |19 |21 |23 |25 |
3
The Plaintext is: Hello
a = 3 || b = 14
The Ciphertext is: Wfaaj
The Decrypted Plaintext is: Hmttw

即便在做一些简单的偷窃时,我也无法将确切的行为问题与次要性质联系起来,但似乎还有一个问题,即如果对加密和加密都设置障碍,则在下个案件中,计算重复/计算会发生。

Since the end result is to select a character at a time and apply and encryption/offset calculation to that character, the simplest solution was to always treat the character as an uppercase character, perform the encryption or decryption, and then calculate its offset from the lowercase character a instead of the uppercase character A .

Following are the blocks of code showing off the refactored steps.

首先,根据好评,对“绿色测试共同点”功能进行了更正,以将重新唤醒的愤怒情绪退回到这一功能中。

    int GCD(int a, int b)
{
    if(b == 0)
    {
        return a;
    }
    return GCD(b, a%b);     /* Refactored per good comments */
}

然后,加密和加密功能中的公式被重新确定,以作为上层数值处理特性,然后在“组合”中添加“抵消”。

void Encryption(char Plaintext[], char Ciphertext[], int a, int b)
{
    int temp_1, temp_2, i;
    i = 0;

    while(Plaintext[i] !=   )
    {
        temp_1 = Plaintext[i];

        if(temp_1 >= 65 && temp_1 <= 90)
        {
            temp_2 = (a*temp_1 + b)%26;
            Ciphertext[i++] =  A  + temp_2;
        }
        else if(temp_1 >= 97 && temp_1 <= 122)
        {
            temp_2 = (a*(temp_1 - 32) + b) % 26;    /* Refactored to treat character as uppercase       */
            Ciphertext[i++] =  a  + temp_2;         /* Offset value is applied to lowercase reference   */
        }
        else if(temp_1 == 32)
        {
            Ciphertext[i++] =    ;
        }
    }

    Ciphertext[i] =   ;
}

void Decryption(char Ciphertext[], char Decrypted_Plaintext[], int a, int b)
{
    struct Pair inverse;
    int temp_a_inverse;
    inverse = Extended_Euclidean_Algorithm(26, a);
    temp_a_inverse = inverse.y;

    if(temp_a_inverse < 0)
    {
        temp_a_inverse = temp_a_inverse + 26;
    }

    int temp_1, temp_2, i;
    i = 0;

    while(Ciphertext[i] !=   )
    {
        temp_1 = Ciphertext[i];

        if(temp_1 >= 65 && temp_1 <= 90)
        {
            temp_2 = ((temp_1 + 26 - b)*temp_a_inverse)%26;
            Decrypted_Plaintext[i++] =  A  + temp_2;
        }
        else if(temp_1 >= 97 && temp_1 <= 122)
        {
            temp_2 = ((temp_1 - 32 + 26 - b)*temp_a_inverse)%26;    /* Refactored to treat character as uppercase       */
            Decrypted_Plaintext[i++] =  a  + temp_2;                /* Offset value is applied to lowercase reference   */
        }
        else if(temp_1 == 32)
        {
            Decrypted_Plaintext[i++] =    ;
        }
    }

    Decrypted_Plaintext[i] =   ;
}

Following were a couple of tests showing the terminal output with this refactoring.

craig@Vera:~/C_Programs/Console/Decipher/bin/Release$ ./Decipher 
Enter the plaintext: Hello
Choose any number from the given numbers: 1 |3 |5 |7 |9 |11 |15 |17 |19 |21 |23 |25 |
3
The Plaintext is: Hello
a = 3 || b = 14
The Ciphertext is: Wniir
The Decrypted Plaintext is: Hello
craig@Vera:~/C_Programs/Console/Decipher/bin/Release$ ./Decipher 
Enter the plaintext: Welcome home
Choose any number from the given numbers: 1 |3 |5 |7 |9 |11 |15 |17 |19 |21 |23 |25 |
7
The Plaintext is: Welcome home
a = 7 || b = 21
The Ciphertext is: Gkhwcok fcok
The Decrypted Plaintext is: Welcome home

这可能不是希望的解决办法的主导者,但它的确提供了一种简化的解决办法,即放弃确定哪些问题可以简化。

The takeaway from this is probably to delve some more into some "C" tutorials as they pertain to character arrays, encryption methods, and ASCII character sets.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签