Sei sulla pagina 1di 3

/* an amateur c program to implement the vigenere cipher

(polyalphabetic substitution cipher) .spaces are not encoded


*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
int tonumber(char);
int main(){
clrscr();
int len,i,j;
char * message;
int * encoded;
int *decoded;
char *key;
char table[26][26]={
'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','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','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',
'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',
'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',
'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',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','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','
a','b','c','d','e','f','g',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',

'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
'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',
};
printf("\nenter the length of the message:");
scanf("%d",&len);
message=(char *)malloc(len*sizeof(char));
encoded=(int *)malloc(len*sizeof(int));
decoded=(int *)malloc(len*sizeof(int));
key=(char *)malloc(len*sizeof(char));
printf("\nenter the message:");
scanf("%s",message);
printf("\nenter the key:");
scanf("%s",key);
printf("\nThe key is:");
for(i=0;i<len;i++){
printf("%c",key[i]);
}
printf("\nThe message is:");
for(i=0;i<len;i++)
printf("%c",message[i]);
printf("\nThe encoded message is:");
for(i=0;i<len;i++){
encoded[i]=table[tonumber(key[i])][tonumber(message[i])];
printf("%c",encoded[i]);
}
printf("\nThe decoded message is:");
for(i=0;i<len;i++){
for(j=0;j<26;j++){

if(table[tonumber(key[i])][j]==encoded[i])
decoded[i]=table[0][j];
}
printf("%c",decoded[i]);
}
getch();
return 0;
}
int tonumber(char letter){
return ((int)(letter)-97);
}

Potrebbero piacerti anche