你能看看这段代码吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char encrypt(char *abc, int *key,char text, int counter)
{
int i;
int encryptedletter;
for(i=0;i<=25;i++)
{
if(text==abc[i])
{
encryptedletter = (i + key[counter])%26 ;
return abc[encryptedletter];
}
}
}
char decrypt(char *abc, int *key,char text, int counter)
{
int i;
int decryptedletter;
for(i=0;i<=25;i++)
{
if(text==abc[i])
{
decryptedletter = (i-key[counter])%26 ;
return abc[decryptedletter];
}
}
}
int main(void)
{
char text[100];
char encryped[100];
char decrypted[100];
char key[20];
int i,z,q,keylength,counter=0;
char abc[27]= "abcdefghijklmnopqrstuvwxyz"; //ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .";
int keyint[70];
printf("Give Text: ");
gets(text);
printf("Give Password: ");
gets(key);
keylength = strlen(key);
for(z=0;z<strlen(key);z++)
{
for(i=0;i<=25;i++)
{
if(key[z]==abc[i]) keyint[z] = i;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~ENCRYPTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for(i=0;i<strlen(text);i++)
{
if(counter>=keylength) counter=0;
encryped[i] = encrypt(abc,keyint,text[i],counter);
counter++;
}
encryped[strlen(text)]='\0';
printf("\nEncrypted text: %s\n", encryped);
//~~~~~~~~~~~~~~~~~~~~~~~~~~DECRYPTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
counter =0;
for(q=0;q<=strlen(text);q++)
{
if(counter>=keylength) counter=0;
decrypted[q] = decrypt(abc,keyint,encryped[q],counter);
counter++;
printf("%c", decrypted[q]);
}
return 0;
}
我正在尝试制作一个基于 Vigenere 的加密/解密程序。
我认为它工作正常.. 但是当我的 abc 数组上的文本或密码字母高于“T”(不确定)时,我会得到错误的解密文本。