征求最好的算法(100)
下面是题目的介绍以及题目本身,最后给出了我的算法,最常见的那种.想看看大家的想法,想找出更好的算法来,如果想出来了,可以和大家分享一下吗?
=========================================================================
INTRODUCTION
Ciphers are used to produce messages which can be understood only by the sender and authorized receivers, so ciphers can be used for secret correspondence in situations where the message may fall into the wrong hands. The process of converting a readable message (the plaintext) to an unreadable form (the ciphertext) is called “encryption”; converting ciphertext to plaintext is called “decryption”.
The Vigenere cipher was developed by Biaise de Vigenere around 1586, for French diplomatic and military communications. The Vigenere cipher uses a keyword and a 26-by-26 matrix of letters to substitute plaintext letters with ciphertext letters, and vice versa. The message sender and receiver must both use the same matrix and keyword. For this experiment, the matrix will be:
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 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
b 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
c 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
.... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..
z 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
The matrix contains the (uppercase) ciphertext letters. The lowercase letters are the row and column indices.
To encrypt a plaintext letter:
1. The plaintext letter is matched with a keyword letter, as shown below.
2. The keyword letter determines the ROW of the matrix. Find the row that begins with the keyword letter.
3. The plaintext letter determines the COLUMN of the matrix.
4. The ciphertext letter is found at the intersection of ROW and COLUMN.
For example, if the keyword is “calculus” and the plaintext is “beammeupscotty”:
Keword: calculus
Plaintext: beammeupscotty
The ciphertext will be “DELOGPOHUCZVNJ”.
The first plaintext letter, ‘b’, is matched with the first keyword letter, ‘c’, so the first ciphertext letter, ‘D’,
is found in the matrix at row ‘c’, column ‘b’. The second ciphertext letter, ‘E’, is found at row ‘a’, column
‘e’, etc.
To decrypt a message, the processs is reversed. The ciphertext is matched with the same keyword as
was used in the encryption. As before, a keyword letter determines a row of the matrix. To decrypt a
ciphertext letter:
1. Match the ciphertest letter to a keyword letter.
2. Search the row corresponding to the keyword letter, then locate the ciphertext letter.
3. Find the index of the column in which the ciphertext letter appears. The column index is the plaintext
letter that corresponds to the given ciphertext letter.
=========================================================================
Write an interactive program to:
· Allow the user to enter a keyword of 20 characters or less.
· Encrypt a plaintext message of 80 characters or less, using the method described above with the keyword supplied by the user.
· Decrypt a ciphertext message of 80 characters or less, using the method described above with the keyword supplied by the user.
· You should print ciphertext in uppercase letters.
Test case:
· Use the keyword “zygote”, encrypt “zoology” and “aardvark” (separately).
· Use the same keyword “zygote”, decrypt “YSIQAMMG” and “ZPIVTINNZSKCW”.
· Exit the program.
· Use the keyword “apple”, encrypt “zeppelin” and decrypt “ZNBFVGN”.
#########################################################################
#include <stdio.h>
void encrypt(char str1[], char str2[]);
void decrypt(char str1[], char str2[]);
int length(char str[]);
main (void)
{
int choice;
char str1[20];
char str2[80];
char str3[80]; /*variable declaration*/
printf("Enter a keyword: ");
gets(str1);
do {
printf("You have the following options:\n");
printf("1: encrypt a plaintext message\n");
printf("2: decrypt a ciphertext message\n");
printf("3: quit the program\n"); /*declare different kinds of choices*/
printf("Enter the number of your choice=>");
scanf("%d", &choice); /*user select choice*/
fflush(stdin);
switch(choice){
case 1: printf("Enter a line of text (only lowercase letters and are valid): ");
gets(str2);
printf("\tplaintext = %s \n", str2);
printf("\tciphertext = ");
encrypt(str1,str2);
break;
case 2: printf("Enter a line of text: ");
gets(str3);
printf("\tplaintext = %s \n", str3);
printf("\tciphertext = ");
decrypt(str1,str3);
break;
case 3: printf("Program is terminating......\n");
break;
}
}while(choice<=2); /*user call these choices*/
return 0;
}
void encrypt(char str1[], char str2[])
{
char str[26][26];
int i,j,x;
int m, n;
for(i=0;i<26; i++){
for(j=0;j<26; j++){
str[i][j]='A'+(i+j)%26;}}
m=length(str1);
n=length(str2);
for(x=0;x<n;x++){
printf("%c", str[str2[x]-97][str1[x%m]-97]);}
printf("\n");
}
void decrypt(char str1[], char str2[])
{
char str[26][26];
int i,j, x;
int m,n;
for(i=0;i<26; i++){
for(j=0;j<26; j++){
str[i][j]='a'+(26+i-j)%26;;}}
m=length(str1);
n=length(str2);
for(x=0; x<n;x++)
printf("%c", str[str2[x]-65][str1[x%m]-97]);
printf("\n");
}
int length(char str[])
{
int count =0;
while(str[count]!='\0')
count++;
return count;
}
#########################################################################