//解密运算,inputSize必须为128,192,256位 void Decrypt(RijndaelContextPtr context,void* input) { int round; //明文的最大长度 unsigned long state[8] = {0}; unsigned char* inputStringPtr = (unsigned char*)input; unsigned int nb = context->nb; state[0] = GETINT32(inputStringPtr,0,nb); state[1] = GETINT32(inputStringPtr,1,nb); state[2] = GETINT32(inputStringPtr,2,nb); state[3] = GETINT32(inputStringPtr,3,nb); if (nb >= (KeySize_192/4)) { state[4] = GETINT32(inputStringPtr,4,nb); state[5] = GETINT32(inputStringPtr,5,nb); } if (nb >= (KeySize_256/4)) { state[6] = GETINT32(inputStringPtr,6,nb); state[7] = GETINT32(inputStringPtr,7,nb); } InvAddRoundKey(context,context->nr,state); InvShiftRow(context,state); InvSubByte(context,state); for (round = (context->nr-1); round > 0; round--) { InvAddRoundKey(context,round,state); InvMixColumn(context,state); InvShiftRow(context,state); InvSubByte(context,state); } InvAddRoundKey(context,0,state); StateToChars((unsigned char*)input,state,context->nb); }
void dechiffrerBloc(unsigned char mat[TAILLEMATRICE][TAILLEMATRICE], unsigned char cleEtendue[TAILLEMATRICE*(NBROUND+1)][TAILLEMATRICE]){ int ligne, colonne, round; // Traitement du bloc //BLOC CHIFFRE------------------------------------------ printf("\n---------------------------------\n"); printf("\nBLOC CHIFFRE \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n"); //FIN BLOC CHIFFRE----------------------------------- AddRoundKey(cleEtendue,NBROUND,mat); /* //ADD ROUND KEY------------------------------------------ printf("ADD ROUND KEY [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); //FIN ADD ROUND KEY-----------------------------------*/ for(round=NBROUND-1; round>=0; round--){ InvShiftRow(mat); /* //INV SHIFT ROW------------------------------------------ printf("SHIFT ROW [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); //INV SHIFT ROW-----------------------------------*/ InvSubBytes(mat); /*//INV SUB BYTES------------------------------------------ printf("INV SUBBYTES[%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); //INV SUBBYTES------------------------------------------*/ AddRoundKey(cleEtendue,round,mat); /* //ADD ROUND KEY------------------------------------------ printf("ADD ROUND KEY [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); //FIN ADD ROUND KEY-----------------------------------*/ if(round!=(0)) { InvMixColumns(mat); /*//DEBUG INV MIXCOLUMNS------------------------------------------ printf("DEBUG INV MIXCOLUMNS [%d] \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } printf("\n---------------------------------\n"); // FIN DEBUG INV MIXCOLUMNS---------------------------------------*/ } } //DEBUG MESSAGE CRYPTE EN HEXA------------------------------------------ printf("BLOC DECHIFRRE EN HEXA \n",round); for (ligne=0; ligne<TAILLEMATRICE; ligne++){ for (colonne=0; colonne<TAILLEMATRICE; colonne++){ printf("0x%.2X\t" ,mat[colonne][ligne]); } printf("\n"); } }