1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
//char *alphabet = "abcdefghijklmnopqrstuvwxyz";
char texte[200]; //texte à coder
char *ptexte = texte;
char clef[20]; //la clé
char *pclef = clef;
char crypte[200];
char *p = crypte; //le texte crypte
char decrypte[200];
char *pdec=decrypte;
int lgtexte; //longueure du texte à crypter
int lgclef; //longueure de la clef
/* cryptage */
void cryptage(char* ptexte, char* pclef)
{
int i = 0;
do {
int j = 0;
do {
/* attention aux parenthèses */
if (isalpha(*(ptexte + i))!= 0)
{
/* = à la place de == */
*(p + i) = (*(ptexte + i) + *(pclef + j) - 2 * 'a') % 26 + 'a';
i++;
j++;
}
else
{
*(p + i) = *(ptexte + i);
i++;
}
}
/* attention aux condition de sortie */
while (j < lgclef);
}
while (i < lgtexte);
puts("/*message crypte*/");
puts(crypte);
}
void decryptage(char* p, char* pclef)
{
int i = 0;
do
{
int j = 0;
do {
/* attention aux parenthèses */
if (isalpha(*(p + i))!= 0)
{
/* = à la place de == */
*(pdec + i) =(*(p + i) - *(pclef + j))%26 +'a' ;
// *(pdec + i) =26+(*(p + i) - *(pclef + j)) +'a';
i++;
j++;
}
else
{
*(pdec+i)=*(p+i);
i++;
}
}
/* attention aux condition de sortie */
while (j < lgclef);
}
while (i < lgtexte);
puts("/*message decrypte*/");
puts(decrypte);
}
void main()
{
puts("+++taper le texte à coder+++ \n");
gets(texte);
lgtexte = strlen(texte);
printf("+++taper la clef+++ \n");
gets(clef);
lgclef = strlen(clef);
cryptage(ptexte, pclef);
decryptage(p, pclef);
} |
Partager