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