Programme passerelle dans le cadre du projet Prosecco
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

180 lignes
4.5 KiB

  1. /* systeme simple de chiffrement d'octets inspire de la methode PlayFair */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include "chiffrePF.h" /* pour verifier la coherence a la compilation */
  10. /* ATTENTION : Ce no de version sert aux utilisateurs a bien verifier
  11. qu'ils echangent avec LA MEME VERSION !!
  12. Donc, si vous faites des modifications il faut changer ce no
  13. *******************************************************/
  14. #define VERSION "E2l 1.1"
  15. /* ATTENTION : le resultat etant ecrit sur la sortie standard (stdout),
  16. les messages de DEBUG sont tous diriges vers la sortie erreur (stderr)!!
  17. *********************************************************************/
  18. #define NBO 256 /* nb d'octets */
  19. #define LCM 16 /* Longueur du cote de la matrice */
  20. static char buf[NBO]; /* table des octets a chiffrer */
  21. static char buf2[NBO]; /* table pour noter le remplissage des octets */
  22. static void coordCar(char C, int *l, int * c)
  23. {
  24. int i;
  25. for (i=0; i< NBO; i++) if (buf[i]==C) break;
  26. *c = i%LCM;
  27. *l = i/LCM;
  28. }
  29. static char carLC(int l, int c) /*fct qui retourne le car. de la ligne l col. c*/
  30. {
  31. return(buf[(l*LCM)+c]);
  32. }
  33. #ifdef DEBUGPF
  34. static void Trace(void) /* affiche buf */
  35. {
  36. int i,j;
  37. unsigned char c;
  38. for (i=0;i<LCM;i++) {
  39. for (j=0;j<LCM;j++) {
  40. c = (unsigned char)buf[(i*LCM)+j];
  41. if ((c >= ' ') && (c <= '~')) fprintf(stderr," %c ",c);
  42. else fprintf(stderr,"%.2x ",c);
  43. }
  44. fprintf(stderr,"\n");
  45. }
  46. }
  47. #endif
  48. /* fonction qui chiffre directement dans le buffer des donnees a chiffrer */
  49. void PF_chiffreD(char *B, int T) /* B adresse du buf, T le nb d'octets */
  50. {
  51. int i,l1,c1,l2,c2;
  52. for (i=0; i<T; i+=2) {
  53. if (i==T-1) { /* cas particulier du dernier octet */
  54. coordCar(B[i],&l1,&c1);
  55. l1+=2; /* on descend de 2 ligne */
  56. if (l1 >=LCM) l1-=LCM;
  57. B[i] = carLC(l1,c1);
  58. } else { /* cas du couple */
  59. coordCar(B[i],&l1,&c1);
  60. coordCar(B[i+1],&l2,&c2);
  61. if (l1 == l2) { /* sur la meme ligne */
  62. /* on decale vers le haut */
  63. l1--;
  64. if (l1 < 0) l1+=LCM;
  65. B[i] = carLC(l1,c1);
  66. B[i+1] = carLC(l1,c2);
  67. } else {
  68. if (c1 == c2) { /* sur la meme colonne */
  69. /* on decale vers la droite */
  70. c1++;
  71. if (c1 >=LCM) c1-=LCM;
  72. B[i] = carLC(l1,c1);
  73. B[i+1] = carLC(l2,c1);
  74. } else { /* on a un rectangle */
  75. /* schema
  76. c1 c2
  77. l1 x b
  78. l2 a y
  79. On fait la permutation x,y => a,b
  80. **************************************/
  81. B[i] = carLC(l2,c1);
  82. B[i+1] = carLC(l1,c2);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. void PF_dechiffreD(char *B, int T) /* B adresse du buf, T le nb d'octets */
  89. {
  90. int i,l1,c1,l2,c2;
  91. for (i=0; i<T; i+=2) {
  92. if (i==T-1) { /* cas particulier du dernier octet */
  93. coordCar(B[i],&l1,&c1);
  94. l1-=2; /* on monte de 2 ligne */
  95. if (l1 < 0) l1+=LCM;
  96. B[i] = carLC(l1,c1);
  97. } else { /* cas du couple */
  98. coordCar(B[i],&l1,&c1);
  99. coordCar(B[i+1],&l2,&c2);
  100. if (l1 == l2) { /* sur la meme ligne */
  101. /* on decale vers le bas */
  102. l1++;
  103. if (l1 >= LCM) l1-=LCM;
  104. B[i] = carLC(l1,c1);
  105. B[i+1] = carLC(l1,c2);
  106. } else {
  107. if (c1 == c2) { /* sur la meme colonne */
  108. /* on decale vers la gauche */
  109. c1--;
  110. if (c1 < 0) c1+=LCM;
  111. B[i] = carLC(l1,c1);
  112. B[i+1] = carLC(l2,c1);
  113. } else { /* on a un rectangle */
  114. /* schema
  115. c1 c2
  116. l1 x b
  117. l2 a y
  118. On fait la permutation x,y => a,b
  119. **************************************/
  120. B[i] = carLC(l2,c1);
  121. B[i+1] = carLC(l1,c2);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. #define SAUT 29 /* valeur du saut pour remplir le reste de la table */
  128. void PF_init(char * cle) /* on passe la cle en parametre */
  129. {
  130. /* initialisation de la table */
  131. int i,I=0,j;
  132. char c;
  133. for (i=0; i<strlen(cle); i++) {
  134. c = cle[i];
  135. if (buf2[(int)c]) continue;
  136. buf[I++] = c;
  137. buf2[(int)c] = 1;
  138. }
  139. /*
  140. for (i=0; i<NBO; i++) {
  141. if (buf2[i]) continue;
  142. buf[I++] = (char)i;
  143. buf2[i] = 1;
  144. }
  145. */
  146. for (j=0;j<SAUT;j++) {
  147. for (i=j; i<NBO; i+=SAUT) {
  148. if (buf2[i]) continue;
  149. buf[I++] = i;
  150. buf2[i] = 1;
  151. }
  152. }
  153. #ifdef DEBUG
  154. for (i=0; i<NBO; i++) {
  155. if (buf2[i]) continue;
  156. fprintf(stderr,"Valeur %d non affectee !!\n",i);
  157. }
  158. #endif
  159. }