|
|
@@ -0,0 +1,179 @@ |
|
|
|
/* systeme simple de chiffrement d'octets inspire de la methode PlayFair */ |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <string.h> |
|
|
|
#include <sys/types.h> |
|
|
|
#include <sys/stat.h> |
|
|
|
#include <fcntl.h> |
|
|
|
#include <unistd.h> |
|
|
|
|
|
|
|
#include "chiffrePF.h" /* pour verifier la coherence a la compilation */ |
|
|
|
|
|
|
|
/* ATTENTION : Ce no de version sert aux utilisateurs a bien verifier |
|
|
|
qu'ils echangent avec LA MEME VERSION !! |
|
|
|
Donc, si vous faites des modifications il faut changer ce no |
|
|
|
*******************************************************/ |
|
|
|
#define VERSION "E2l 1.1" |
|
|
|
|
|
|
|
/* ATTENTION : le resultat etant ecrit sur la sortie standard (stdout), |
|
|
|
les messages de DEBUG sont tous diriges vers la sortie erreur (stderr)!! |
|
|
|
*********************************************************************/ |
|
|
|
|
|
|
|
#define NBO 256 /* nb d'octets */ |
|
|
|
#define LCM 16 /* Longueur du cote de la matrice */ |
|
|
|
static char buf[NBO]; /* table des octets a chiffrer */ |
|
|
|
static char buf2[NBO]; /* table pour noter le remplissage des octets */ |
|
|
|
|
|
|
|
static void coordCar(char C, int *l, int * c) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
for (i=0; i< NBO; i++) if (buf[i]==C) break; |
|
|
|
*c = i%LCM; |
|
|
|
*l = i/LCM; |
|
|
|
} |
|
|
|
|
|
|
|
static char carLC(int l, int c) /*fct qui retourne le car. de la ligne l col. c*/ |
|
|
|
{ |
|
|
|
return(buf[(l*LCM)+c]); |
|
|
|
} |
|
|
|
|
|
|
|
#ifdef DEBUGPF |
|
|
|
static void Trace(void) /* affiche buf */ |
|
|
|
{ |
|
|
|
int i,j; |
|
|
|
unsigned char c; |
|
|
|
for (i=0;i<LCM;i++) { |
|
|
|
for (j=0;j<LCM;j++) { |
|
|
|
c = (unsigned char)buf[(i*LCM)+j]; |
|
|
|
if ((c >= ' ') && (c <= '~')) fprintf(stderr," %c ",c); |
|
|
|
else fprintf(stderr,"%.2x ",c); |
|
|
|
} |
|
|
|
fprintf(stderr,"\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
#endif |
|
|
|
|
|
|
|
/* fonction qui chiffre directement dans le buffer des donnees a chiffrer */ |
|
|
|
void PF_chiffreD(char *B, int T) /* B adresse du buf, T le nb d'octets */ |
|
|
|
{ |
|
|
|
int i,l1,c1,l2,c2; |
|
|
|
for (i=0; i<T; i+=2) { |
|
|
|
if (i==T-1) { /* cas particulier du dernier octet */ |
|
|
|
coordCar(B[i],&l1,&c1); |
|
|
|
l1+=2; /* on descend de 2 ligne */ |
|
|
|
if (l1 >=LCM) l1-=LCM; |
|
|
|
B[i] = carLC(l1,c1); |
|
|
|
} else { /* cas du couple */ |
|
|
|
coordCar(B[i],&l1,&c1); |
|
|
|
coordCar(B[i+1],&l2,&c2); |
|
|
|
if (l1 == l2) { /* sur la meme ligne */ |
|
|
|
/* on decale vers le haut */ |
|
|
|
l1--; |
|
|
|
if (l1 < 0) l1+=LCM; |
|
|
|
B[i] = carLC(l1,c1); |
|
|
|
B[i+1] = carLC(l1,c2); |
|
|
|
} else { |
|
|
|
if (c1 == c2) { /* sur la meme colonne */ |
|
|
|
/* on decale vers la droite */ |
|
|
|
c1++; |
|
|
|
if (c1 >=LCM) c1-=LCM; |
|
|
|
B[i] = carLC(l1,c1); |
|
|
|
B[i+1] = carLC(l2,c1); |
|
|
|
} else { /* on a un rectangle */ |
|
|
|
/* schema |
|
|
|
c1 c2 |
|
|
|
|
|
|
|
l1 x b |
|
|
|
|
|
|
|
|
|
|
|
l2 a y |
|
|
|
|
|
|
|
On fait la permutation x,y => a,b |
|
|
|
**************************************/ |
|
|
|
B[i] = carLC(l2,c1); |
|
|
|
B[i+1] = carLC(l1,c2); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void PF_dechiffreD(char *B, int T) /* B adresse du buf, T le nb d'octets */ |
|
|
|
{ |
|
|
|
int i,l1,c1,l2,c2; |
|
|
|
for (i=0; i<T; i+=2) { |
|
|
|
if (i==T-1) { /* cas particulier du dernier octet */ |
|
|
|
coordCar(B[i],&l1,&c1); |
|
|
|
l1-=2; /* on monte de 2 ligne */ |
|
|
|
if (l1 < 0) l1+=LCM; |
|
|
|
B[i] = carLC(l1,c1); |
|
|
|
} else { /* cas du couple */ |
|
|
|
coordCar(B[i],&l1,&c1); |
|
|
|
coordCar(B[i+1],&l2,&c2); |
|
|
|
if (l1 == l2) { /* sur la meme ligne */ |
|
|
|
/* on decale vers le bas */ |
|
|
|
l1++; |
|
|
|
if (l1 >= LCM) l1-=LCM; |
|
|
|
B[i] = carLC(l1,c1); |
|
|
|
B[i+1] = carLC(l1,c2); |
|
|
|
} else { |
|
|
|
if (c1 == c2) { /* sur la meme colonne */ |
|
|
|
/* on decale vers la gauche */ |
|
|
|
c1--; |
|
|
|
if (c1 < 0) c1+=LCM; |
|
|
|
B[i] = carLC(l1,c1); |
|
|
|
B[i+1] = carLC(l2,c1); |
|
|
|
} else { /* on a un rectangle */ |
|
|
|
/* schema |
|
|
|
c1 c2 |
|
|
|
|
|
|
|
l1 x b |
|
|
|
|
|
|
|
|
|
|
|
l2 a y |
|
|
|
|
|
|
|
On fait la permutation x,y => a,b |
|
|
|
**************************************/ |
|
|
|
B[i] = carLC(l2,c1); |
|
|
|
B[i+1] = carLC(l1,c2); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#define SAUT 29 /* valeur du saut pour remplir le reste de la table */ |
|
|
|
void PF_init(char * cle) /* on passe la cle en parametre */ |
|
|
|
{ |
|
|
|
/* initialisation de la table */ |
|
|
|
int i,I=0,j; |
|
|
|
char c; |
|
|
|
for (i=0; i<strlen(cle); i++) { |
|
|
|
c = cle[i]; |
|
|
|
if (buf2[(int)c]) continue; |
|
|
|
buf[I++] = c; |
|
|
|
buf2[(int)c] = 1; |
|
|
|
} |
|
|
|
/* |
|
|
|
for (i=0; i<NBO; i++) { |
|
|
|
if (buf2[i]) continue; |
|
|
|
buf[I++] = (char)i; |
|
|
|
buf2[i] = 1; |
|
|
|
} |
|
|
|
*/ |
|
|
|
for (j=0;j<SAUT;j++) { |
|
|
|
for (i=j; i<NBO; i+=SAUT) { |
|
|
|
if (buf2[i]) continue; |
|
|
|
buf[I++] = i; |
|
|
|
buf2[i] = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
#ifdef DEBUG |
|
|
|
for (i=0; i<NBO; i++) { |
|
|
|
if (buf2[i]) continue; |
|
|
|
fprintf(stderr,"Valeur %d non affectee !!\n",i); |
|
|
|
} |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
|