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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
|
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include "cle.h"
#include "permutations.h"
#include "sbox.h"
#include <fstream>
#include <string>
#include <iomanip>
void permutation_initiale(bool , bool , bool );
void expansion(bool ,bool );
void sbox(bool ,bool );
bool lecture(ifstream& , int& );
void bin(int ,bool );
void bin2(bool ,int& , int& , int );
void KEY(int , bool , bool );
void Permutation(bool , bool );
void bin88(char , bool );
void permutation_finale(bool , bool , bool );
void f(bool R[][4],bool K[][6],bool permutation[][4]){
bool expansion_R[8][6];
expansion(R,expansion_R);
bool ou[8][6];
for(int i=0;i<8;i++){
for(int j=0;j<6;j++){
ou[i][j]=expansion_R[i][j]^K[i][j];
}
}
bool SBOX[8][4];
sbox(ou,SBOX);
Permutation(SBOX,permutation);
}
using namespace std;
const char* nomfichier_sortie="output.txt";
int des(char* pp,char cle[],bool choix)
{
ifstream test_fichier(pp);
if(!test_fichier.is_open()){
cout<<"Le fichier ne peut pas etre ouvert, il n'existe peut-etre pas"<<endl;
cout<<"ou vous avez fait une faute de frappe!"<<endl;
return 1;
}
test_fichier.close();
if(choix==false){
//premier cas : dechiffrage
ifstream longueur_file(pp);
longueur_file.seekg(0,ios::end);
int longueur=longueur_file.tellg();
longueur_file.seekg(0,ios::beg);
//cette partie du programme sert uniquement a calculer la longueur du texte a chiffrer
char texte_clair[longueur];
longueur_file.read(texte_clair,(longueur+1));
longueur_file.close();
// dans cette partie, le texte a chiffrer est lu depuis le fichier d'entree et est "copie" dans le tableau "texte_clair"
int texte_clair_ascii_int[(longueur+1)];
for(int i=0;i<(longueur+1);i++){
texte_clair_ascii_int[(i+1)]=(int)(unsigned char)(texte_clair[i]);
}
//conversion du texte en code ascii
texte_clair_ascii_int[0]=longueur;
longueur++;
//Ces deux lignes permettent de connaitre la longueur du texte clair lors du dechiffrage
int longueur_int=longueur;
while(longueur_int%8!=0){
longueur_int++;
}
int texte_clair_ascii[longueur_int];
int texte_chiffre1[longueur_int];
int n=0;
for(int i=0;i<longueur;i++){
texte_clair_ascii[i]=texte_clair_ascii_int[i];
}
while (longueur%8!=0)
{
texte_clair_ascii[(longueur)]=texte_clair_ascii[n];
n++;
longueur++;
}
//comme le IDEA chiffre uniquement avec des blocs de 8 caracteres, le programme copie le debut du texte a la fin du texte, jusqu'a ce que la longueur de ce dernier soit divisible par 8.
int temp_longueur=longueur;
n=0;
longueur=strlen(cle);
while(longueur%8!=0){
cle[longueur]=cle[n];
n++;
longueur++;
}
//comme le IDEA chiffre uniquement avec une cle de 16 caracteres, si l'utilisateur entre une cle de moins de 16 caracteres, le debut de la cle est rajoute a la fin de la chaine jusqu'a ce que la longueur de cette derniere soit de 8
bool chaine_cle[16][16];
int cle_ascii[16];
for(int i=0;i<16;i++){
cle_ascii[i]=int(cle[i]);
}
//avec cette fonction, le programme transforme les valeurs ascii des lettres de la cle en code binaire
bin88(cle_ascii,chaine_cle);
longueur=temp_longueur;
n=0;
while(n<longueur)
{
bool chaine_texte_clair[16][16];
int chaine_intermediaire[16];
for(int i=n;i<(n+8);i++){
chaine_intermediaire[(i-n)]=texte_clair_ascii[i];
}
// ici, le programme prend 8 caracteres du texte clair pour les transformer en code binaire, lors de la premiere iteration, le programme prendra les 8 premiers caracteres, a la deuxieme iteration, les 8 suivants, jusqu'a ce qu'il parvienne a la fin du texte.
bin88(chaine_intermediaire,chaine_texte_clair);
bool L[8][4];
bool R[8][4];
permutation_initiale(chaine_texte_clair,L,R);
// la permutation initiale scinde le code binaire issu des 8 caracteres du texte clair en deux, d'un cote, les bits pairs, de l'autre cote, les bits impairs.
for(int i=1;i<=16;i++){
bool temp[8][4];
bool CLE[8][6];
KEY(i,chaine_cle,CLE);
// calcul de la cle...celle-ci changeant a chaque tour du DES
f(R,CLE,temp);
bool temp2[8][4];
for(int i=0;i<8;i++){
for(int j=0;j<4;j++){
temp2[i][j]=R[i][j];
}
}
for(int i=0;i<8;i++){
for(int j=0;j<4;j++){
R[i][j]=L[i][j]^temp[i][j];
}
}
for(int i=0;i<8;i++){
for(int j=0;j<4;j++){
L[i][j]=temp2[i][j];
}
}
}
//on peut dire que cette partie est le corps du programme, c'est dans cette derniere que le texte clair est chiffre
bool octet_crypte[8][8];
permutation_finale(octet_crypte,L,R);
// cette permutation est l'inverse de la permutation initiale et rassemble les deux chaines qui ont subit l'algorithme de cryptage
int octet_crypte_decimal[8];
int nombre;
for(int i=0;i<8;i++){
nombre=0;
for(int j=0;j<8;j++){
if((octet_crypte[i][j])==false){
continue;
}
else{
nombre+=pow(2,double(7-j));
}
}
octet_crypte_decimal[i]=nombre;
}
// ces boucles servent a convertir le code binaire en code decimal
for(int i=n;i<(n+8);i++){
texte_chiffre1[i]=octet_crypte_decimal[(i-n)];
}
//les valeurs de l'octet crypte sont stockees dans le tableau text_chiffre
n+=8;
}
n=0;
ofstream texte_chiffre(nomfichier_sortie);
for(int i=0;i<longueur_int;i++){
if(i%8==0&&i!=0){
texte_chiffre<<endl;
}
texte_chiffre<<texte_chiffre1[i]<<" ";
}
//ici, le programme ecrit le texte chiffre dans le fichier "output.txt"
cout<<"Vous pourrez trouver le texte chiffre dans le fichier"<<endl;
cout<<"\"output.txt\"."<<endl;
}
else{
//deuxieme cas : déchiffrage
ifstream longueur_texte(pp);
int longueur_fichier=0;
int test;
while(longueur_texte>>test){
longueur_fichier++;
}
//comme le programme ne doit tenir compte que des chiffres, on peut calculer la longueur du texte de cette maniere
longueur_texte.close();
int texte_chiffre[longueur_fichier];
int texte_dechiffre[longueur_fichier];
ifstream texte_chiffre_fichier(pp);
bool indice;
indice=lecture(texte_chiffre_fichier,test);
int longueur=0;
while(indice){
texte_chiffre[longueur]=test;
longueur++;
indice=lecture(texte_chiffre_fichier,test);
}
//les valeurs lues dans le fichier texte sont enregistrees dans le tableau "texte_chiffre"
texte_chiffre_fichier.close();
int longueur_cle=strlen(cle);
test=0;
while(longueur_cle%8!=0){
cle[longueur_cle]=cle[test];
longueur_cle++;
test++;
}
//meme chose que lors du chiffrage...
int cle_ascii[8];
for(int i=0;i<8;i++){
cle_ascii[i]=int(cle[i]);
}
bool chaine_cle[8][8];
bin88(cle_ascii,chaine_cle);
test=0;
while(test<longueur){
int chaine_intermediaire[8];
bool chaine_chiffre[8][8];
for(int i=test;i<(test+8);i++){
chaine_intermediaire[(i-test)]=texte_chiffre[i];
}
bin88(chaine_intermediaire,chaine_chiffre);
bool L[8][4], R[8][4];
permutation_initiale(chaine_chiffre,L,R);
for(int i=16;i>=1;i--){
bool temp[8][4];
bool CLE[8][6];
KEY(i,chaine_cle,CLE);
f(L,CLE,temp);
bool temp2[8][4];
for(int j=0;j<8;j++){
for(int k=0;k<4;k++){
temp2[j][k]=L[j][k];
}
}
for(int j=0;j<8;j++){
for(int k=0;k<4;k++){
L[j][k]=R[j][k]^temp[j][k];
}
}
for(int j=0;j<8;j++){
for(int k=0;k<4;k++){
R[j][k]=temp2[j][k];
}
}
}
//le dechiffrage utilise les memes operations que le chiffrage...
bool octet_decrypte[8][8];
permutation_finale(octet_decrypte,L,R);
int octet_decrypte_decimal[8];
for(int i=0;i<8;i++){
int nombre=0;
for(int j=0;j<8;j++){
if(octet_decrypte[i][j]==true){
nombre+=pow(2,double(7-j));
}
else{
continue;
}
}
octet_decrypte_decimal[i]=nombre;
}
//c'est dans ces lignes que les valeurs binaires des octets dechiffres sont converties en valeurs decimales, puis stockees dans le tableau "octet_decrypte_decimal"
for(int i=0;i<8;i++){
texte_dechiffre[test+i]=octet_decrypte_decimal[i];
}
test+=8;
}
//la totalite de ces valeurs sont ensuite stockees dans le tableau "text_dechiffre"
int texte_dechiffre_decimal[texte_dechiffre[0]];
for(int i=0;i<texte_dechiffre[0];i++){
texte_dechiffre_decimal[i]=texte_dechiffre[(i+1)];
}
//c'est ici que la longueur du texte dechiffre est extraite du texte chiffre, cette longueur avait ete placee au tout debut du texte a chiffrer lors du cryptage
ofstream texte_dechiffre_final(nomfichier_sortie);
for(int i=0;i<texte_dechiffre[0];i++){
texte_dechiffre_final<<char(texte_dechiffre_decimal[i]);
}
//les valeurs sont ensuite converties en caracteres, puis, ecrite dans le fichier "output.txt"
cout<<"Vous pourrez trouver le texte dechiffre"<<endl;
cout<<"dans le fichier \"output.txt\"."<<endl;
}
return 0;
}
le chiffrage IDEA.
Jai le code source
#include "projet.h"
int main (void)
{
do {
initialisation();
ouverture_des_fichiers();
choix_crypt();
fermeture_des_fichiers();
recommencer();
}while (choix == 'o');
}
void initialisation(void)
{
char curl;
for (curl=0;curl<MAX_CARAC;curl++)
{
nom_fichier_a_lire[curl] = 0;
nom_fichier_a_ecrire[curl] = 0;
password[curl] = 0;
}
lettre = 0;
probleme_ouverture = 0;
system ("CLS");// effacer la console
}
void ouverture_des_fichiers (void)
{
printf("Saisissez le nom du fichier %c lire : ",133);
scanf ("%40s",nom_fichier_a_lire);
printf("\n\nSaisissez le nom du fichier o%c il faut %ccrire : ",151,130);
scanf ("%40s",nom_fichier_a_ecrire);
rajout_extension(nom_fichier_a_lire);
rajout_extension(nom_fichier_a_ecrire);
fichier_a_lire = fopen (nom_fichier_a_lire,"rt");
if (fichier_a_lire == NULL)
{
printf ("\n\nProbl%cme d'ouverture du fichier a lire",138);
probleme_ouverture = 1;
}
else
{
fichier_a_ecrire = fopen (nom_fichier_a_ecrire,"wt");
if (fichier_a_ecrire == NULL)
{
printf ("\n\nProbl%cme d'ouverture du fichier o%c il faut %ccrire",151,130);
probleme_ouverture = 1;
}
}
}
void rajout_extension (unsigned char nom_fichier[MAX_CARAC])
{
char curl,extension = 0;
for (curl=0;curl<MAX_CARAC;curl++)
{
if (nom_fichier[curl] == 0 && extension == 0)
{
extension = 1;
nom_fichier[curl] = '.'; nom_fichier[curl+1] = 't';
nom_fichier[curl+2]= 'x'; nom_fichier[curl+3] = 't';
}
}
}
void choix_crypt (void)
{
if (probleme_ouverture == 0)
{
printf ("\n\nVoulez vous effectuer un cryptage ou un d%ccryptage (c/d) : ",130);
do {
choix = getch();
rewind (stdin);
}while(choix != 'c' && choix != 'd');
printf ("%c",choix);
saisie_du_mot_de_passe();
if (choix == 'c')
cryptage();
if (choix == 'd')
decryptage();
}
}
void saisie_de_cle (void)
{
char curl;
printf ("\n\n\nsaisissez le mot de passe : ");
fgets(password, sizeof password, stdin);
size_password = strlen(password);
for (curl=0;curl<size_password;curl++)
password[curl] = 255 - password[curl];
}
/* cryptage */
void cryptage(unsigned IN[5],unsigned OUT[5],unsigned Z[7][10])
{
unsigned r,x1,x2,x3,x4,kk,t1,t2,a;
x1=IN[1]; x2=IN[2]; x3=IN[3]; x4=IN[4];
for(r=1;r<=8;r++) /* the round function */
{
/* the group operation on 64-bits block */
x1 = mul(x1,Z[1][r]); x4 = mul(x4,Z[4][r]);
x2 = (x2 + Z[2][r]) & one; x3 = (x3 + Z[3][r]) & one;
/* the function of the MA structure */
kk = mul(Z[5][r],(x1^x3));
t1 = mul(Z[6][r],(kk+(x2^x4)) & one);
t2 = (kk+t1) & one;
/* the involutary permutation PI */
x1 = x1^t1; x4=x4^t2;
a = x2^t2; x2=x3^t1; x3=a;
printf("\n\t%1u-th rnd %6u\t%6u\t%6u\t%6u",r,x1,x2,x3,x4);
}
/* the output transformation */
OUT[1] = mul(x1,Z[1][round+1]);
OUT[4] = mul(x4,Z[4][round+1]);
OUT[2] = (x3+Z[2][round+1]) & one;
OUT[3] = (x2+Z[3][round+1]) & one;
}
/* multiplication using the Low-High algorithm */
unsigned mul(unsigned a,unsigned b)
{
long int p;
long unsigned q;
if(a==0) p=maxim-b;
else
if(b==0) p=maxim-a;
else {
q=(unsigned long)a*(unsigned long)b;
p=(q & one) - (q>>16);
if(p<=0) p=p+maxim;
}
return (unsigned)(p&one);
}
/* compute inverse of xin by Euclidean gcd alg. */
unsigned inv(unsigned xin)
{
long n1,n2,q,r,b1,b2,t;
if(xin==0) b2=0;
else
{ n1=maxim; n2=xin; b2=1; b1=0;
do { r = (n1 % n2); q = (n1-r)/n2;
if(r==0) { if(b2<0) b2=maxim+b2; }
else { n1=n2; n2=r; t=b2; b2=b1-q*b2; b1=t; }
} while (r!=0);
}
return (unsigned)b2;
}
/* generate encryption subkeys Z's */
void key(short unsigned uskey[9], unsigned Z[7][10])
{
short unsigned S[54];
int i,j,r;
for(i=1;i<9;i++) S[i-1]=uskey[i];
/* shifts */
for(i=8;i<54;i++)
{
if((i+2)%8 == 0) /* for S[14],S[22],... */
S[i] = ((S[i-7]<<9) ^ (S[i-14]>>7)) & one;
else if((i+1)%8==0) /* for S[15],S[23],... */
S[i] = ((S[i-15]<<9) ^ (S[i-14]>>7)) & one;
else
S[i] = ((S[i-7]<<9) ^ (S[i-6]>>7)) & one;
}
/* get subkeys */
for(r=1;r<=round+1;r++)
for(j=1;j<7;j++)
Z[j][r]=S[6*(r-1)+j-1];
}
/* compute decryption subkeys DK's */
void de_key(unsigned Z[7][10],unsigned DK[7][10])
{
int j;
for(j=1;j<=round+1;j++)
{
DK[1][round-j+2] = inv(Z[1][j]);
DK[4][round-j+2] = inv(Z[4][j]);
if (j==1 || j==round+1) {
DK[2][round-j+2] = (fuyi-Z[2][j]) & one;
DK[3][round-j+2] = (fuyi-Z[3][j]) & one;
} else {
DK[2][round-j+2] = (fuyi-Z[3][j]) & one;
DK[3][round-j+2] = (fuyi-Z[2][j]) & one;
}
}
for(j=1;j<=round+1;j++)
{ DK[5][round+1-j]=Z[5][j];
DK[6][round+1-j]=Z[6][j];
}
}
void cryptage (void)
{
do {
saisie_du_caractere();
if (fin_fichier != 255)
{
codage();
ecriture_du_code();
}
}while(fin_fichier != 255);
}
void saisie_du_caractere(void)
{
buffer_caractere = fgetc(fichier_a_lire);
fin_fichier = buffer_caractere;
}
void codage (void)
{
buffer_caractere = buffer_caractere ^ password[lettre];
lettre++;
if (lettre == size_password)
lettre = 0;
C = buffer_caractere / 100;
D = (buffer_caractere - (100 * C)) / 10;
U = buffer_caractere - (100 * C) - (10 * D);
I_n = C + D;
II_n = D + U;
III_n = C + U;
if ( (I_n % 2) == 0 ) I_p = MIN_B + (char)(rand()%(MAX_B+1-MIN_B));
else I_p = MIN_H + (char)(rand()%(MAX_H+1-MIN_H));
if ( (II_n % 2) == 0 ) II_p = MIN_B + (char)(rand()%(MAX_B+1-MIN_B));
else II_p = MIN_H + (char)(rand()%(MAX_H+1-MIN_H));
if ( (III_n % 2) == 0 ) III_p = MIN_B + (char)(rand()%(MAX_B+1-MIN_B));
else III_p = MIN_H + (char)(rand()%(MAX_H+1-MIN_H));
I_n = I_n / 2;
II_n = II_n / 2;
III_n = III_n / 2;
I_a = I_n + (char)( rand() % ( MAX_H+1-( I_n) ) );
II_a = II_n + (char)( rand() % ( MAX_H+1-( II_n) ) );
III_a = III_n + (char)( rand() % ( MAX_H+1-(III_n) ) );
buffer_code[0] = I_p; buffer_code[1] = II_p; buffer_code[2] = III_p;
buffer_code[3] = I_a; buffer_code[4] = II_a; buffer_code[5] = III_a;
buffer_code[6] = I_a-I_n; buffer_code[7] = II_a-II_n; buffer_code[8] = III_a-III_n;
buffer_code[9] = MIN_B + (char)(rand()%(MAX_H+1-MIN_B));
}
void ecriture_du_code (void)
{
char curl;
for (curl=0;curl<TRAME;curl++)
fprintf(fichier_a_ecrire,"%d",buffer_code[curl]);
}
void decryptage (void)
{
do {
saisie_du_code();
if (fin_fichier != 255)
{
decodage();
ecriture_du_caractere();
}
}while(fin_fichier != 255);
}
void saisie_du_code(void)
{
char curl;
for (curl=0;curl<TRAME;curl++)
{
buffer_code[curl] = fgetc(fichier_a_lire);
if (buffer_code[curl] != 255)
buffer_code[curl] = buffer_code[curl] & 0x0F;
}
fin_fichier = buffer_code[0];
}
void decodage (void)
{
I_p = buffer_code[0]; II_p = buffer_code[1]; III_p = buffer_code[2];
I_a = buffer_code[3]; II_a = buffer_code[4]; III_a = buffer_code[5];
I_n = I_a - buffer_code[6]; II_n = II_a - buffer_code[7]; III_n = III_a - buffer_code[8];
if (I_p < 5) I_n = I_n * 2;
else I_n = I_n * 2 + 1;
if (II_p < 5) II_n = II_n * 2;
else II_n = II_n * 2 + 1;
if (III_p < 5) III_n = III_n * 2;
else III_n = III_n * 2 + 1;
C = ( + I_n - II_n + III_n) / 2;
D = ( + I_n + II_n - III_n) / 2;
U = ( - I_n + II_n + III_n) / 2;
buffer_caractere = (100 * C) + (10 * D) + U;
buffer_caractere = buffer_caractere ^ password[lettre];
lettre++;
if (lettre == size_password)
lettre = 0;
}
void ecriture_du_caractere (void)
{
fputc(buffer_caractere,fichier_a_ecrire);
}
void fermeture_des_fichiers (void)
{
if (probleme_ouverture == 0)
{
fclose (fichier_a_lire);
fclose (fichier_a_ecrire);
}
}
void recommencer (void)
{
printf ("\n\nVoulez vous r%ceffectuer un cryptage ou un d%ccryptage (o/n) : ",130,130);
do {
choix = getch();
}while(choix != 'o' && choix != 'n');
} |
Partager