Esempio n. 1
0
File: aes.c Progetto: rutvik11/nslab
//------------------------------------------------------
void decryption()
{
        int i,temp[16],output_1[16],output_2[16];

        printf("Decryption :");

        printf("Round 1:");
        AddRoundKey(Cipher_txt,temp,key[2]);
        display("After Add Round Key2:",temp,16);
        ShiftRow(temp);
        display("After Shift Row:",temp,16);
        InverseSubstituteNibble(temp,16,output_1);
        display("After Inverse Nibble Substitution:",output_1,16);

        printf("Round 2:");
        AddRoundKey(output_1,output_1,key[1]);
        display("After Add Round Key1:",output_1,16);
        MixColumn(output_1,DecMatrix,output_1);
        display("After Inverse Mix Column:",output_1,16);
        ShiftRow(output_1);
        display("After Inverse Shift Row:",output_1,16);
        InverseSubstituteNibble(output_1,16,output_2);
        display("After Inverse Nibble Substitution:",output_2,16);

        AddRoundKey(output_2,output_2,key[0]);
        display("Decrypted text:",output_2,16);
}
Esempio n. 2
0
File: aes.c Progetto: rutvik11/nslab
//------------------------------------------------------
void encryption()
{
        int i,temp[16],output_1[16],output_2[16];

        printf("Encryption:");
        AddRoundKey(plain_txt,temp,key[0]);
        display("After Add Round Key0",temp,16);

        printf("Round 1:");
        SubstituteNibble(temp,16,output_1);
        display("After Substitution of Nibble",output_1,16);
        ShiftRow(output_1);
        display("After Shift Row:",output_1,16);
        MixColumn(output_1,EncMatrix,output_1);
        display("After Mix Column:",output_1,16);
        AddRoundKey(output_1,output_1,key[1]);
        display("After Add Round Key1:",output_1,16);

        printf("Round 2:");
        SubstituteNibble(output_1,16,output_2);
        display("After Substitution of Nibble",output_2,16);
        ShiftRow(output_2);
        display("After Shift Row:",output_2,16);
        AddRoundKey(output_2,Cipher_txt,key[2]);
        display("Cipher text:",Cipher_txt,16);
}
/*-------------------------------------------------------------------
 * Rijndael encryption function. Takes 16-byte input and creates
 * 16-byte output (using round keys already derived from 16-byte
 * key).
 *-----------------------------------------------------------------*/
void RijndaelEncrypt( u8 input[16], u8 output[16] )
{
  u8 state[4][4];
  int i, r;

  /* initialise state array from input byte string */
  for (i=0; i<16; i++)
    state[i & 0x3][i>>2] = input[i];

  /* add first round_key */
  KeyAdd(state, roundKeys, 0);

  /* do lots of full rounds */
  for (r=1; r<=9; r++) {
    ByteSub(state);
    ShiftRow(state);
    MixColumn(state);
    KeyAdd(state, roundKeys, r);
  }

  /* final round */
  ByteSub(state);
  ShiftRow(state);
  KeyAdd(state, roundKeys, r);

  /* produce output byte string from state array */
  for (i=0; i<16; i++) {
    output[i] = state[i & 0x3][i>>2];
  }

  return;
} /* end of function RijndaelEncrypt */
Esempio n. 4
0
int rijndaelDecryptRound (word8 a[4][MAXBC], int keyBits, int blockBits, 
	word8 rk[MAXROUNDS+1][4][MAXBC], int rounds)
/* Decrypt only a certain number of rounds.
 * Only used in the Intermediate Value Known Answer Test.
 * Operations rearranged such that the intermediate values
 * of decryption correspond with the intermediate values
 * of encryption.
 */
{
	int r, BC, ROUNDS;
	
	switch (blockBits) {
	case 128: BC = 4; break;
	case 192: BC = 6; break;
	case 256: BC = 8; break;
	default : return (-2);
	}

	switch (keyBits >= blockBits ? keyBits : blockBits) {
	case 128: ROUNDS = 10; break;
	case 192: ROUNDS = 12; break;
	case 256: ROUNDS = 14; break;
	default : return (-3); /* this cannot happen */
	}


	/* make number of rounds sane */
	if (rounds > ROUNDS) rounds = ROUNDS;

        /* First the special round:
	 *   without InvMixColumn
	 *   with extra KeyAddition
	 */
	KeyAddition(a,rk[ROUNDS],BC);
	Substitution(a,Si,BC);
	ShiftRow(a,1,BC);              
	
	/* ROUNDS-1 ordinary rounds
	 */
	for(r = ROUNDS-1; r > rounds; r--) {
		KeyAddition(a,rk[r],BC);
		InvMixColumn(a,BC);      
		Substitution(a,Si,BC);
		ShiftRow(a,1,BC);                
	}
	
	if (rounds == 0) {
		/* End with the extra key addition
		 */	
		KeyAddition(a,rk[0],BC);
	}    

	return 0;
}
Esempio n. 5
0
bit16 SAES_Decrypt(QVector<bit16> KeyExpanInv, bit16 CiphertextInv)
{
    bit16 PT1 = CiphertextInv ^ KeyExpanInv[2];
    bit16 PT2 = ShiftRow(PT1);
    bit16 PT3 = InverseNibSub(PT2);
    bit16 PT4 = PT3 ^ KeyExpanInv[1];
    bit16 PT5 = MixColumnInverse(PT4);
    bit16 PT6 = ShiftRow(PT5);
    bit16 PT7 = InverseNibSub(PT6);
    bit16 PT8 = PT7 ^ KeyExpanInv[0];

    return PT8;
}
Esempio n. 6
0
bit16 SAES_Encrypt(QVector<bit16> KeyExpan, bit16 Plaintext)
{
    bit16 CT1 = Plaintext ^ KeyExpan[0];
    bit16 CT2 = NibSub(CT1);
    bit16 CT3 = ShiftRow(CT2);
    bit16 CT4 = MixColumn(CT3);
    bit16 CT5 = CT4 ^ KeyExpan[1];
    bit16 CT6 = NibSub(CT5);
    bit16 CT7 = ShiftRow(CT6);
    bit16 CT8 = CT7 ^ KeyExpan[2];

    return CT8;
}
Esempio n. 7
0
int _rijndaelDecrypt (word8 a[4][MAXBC], int keyBits, int blockBits, word8 rk[MAXROUNDS+1][4][MAXBC])
{
	int r, BC, ROUNDS;
	
	switch (blockBits) {
	case 128: BC = 4; break;
	case 192: BC = 6; break;
	case 256: BC = 8; break;
	default : return (-2);
	}

	switch (keyBits >= blockBits ? keyBits : blockBits) {
	case 128: ROUNDS = 10; break;
	case 192: ROUNDS = 12; break;
	case 256: ROUNDS = 14; break;
	default : return (-3); /* this cannot happen */
	}

	/* To decrypt: apply the inverse operations of the encrypt routine,
	 *             in opposite order
	 * 
	 * (KeyAddition is an involution: it 's equal to its inverse)
	 * (the inverse of Substitution with table S is Substitution with the inverse table of S)
	 * (the inverse of Shiftrow is Shiftrow over a suitable distance)
	 */

        /* First the special round:
	 *   without InvMixColumn
	 *   with extra KeyAddition
	 */
	KeyAddition(a,rk[ROUNDS],BC);
	Substitution(a,Si,BC);
	ShiftRow(a,1,BC);              
	
	/* ROUNDS-1 ordinary rounds
	 */
	for(r = ROUNDS-1; r > 0; r--) {
		KeyAddition(a,rk[r],BC);
		InvMixColumn(a,BC);      
		Substitution(a,Si,BC);
		ShiftRow(a,1,BC);                
	}
	
	/* End with the extra key addition
	 */
	
	KeyAddition(a,rk[0],BC);    

	return 0;
}
Esempio n. 8
0
int rijndaelEncryptRound (word8 a[4][MAXBC], int keyBits, int blockBits, 
		word8 rk[MAXROUNDS+1][4][MAXBC], int rounds)
/* Encrypt only a certain number of rounds.
 * Only used in the Intermediate Value Known Answer Test.
 */
{
	int r, BC, ROUNDS;

	switch (blockBits) {
	case 128: BC = 4; break;
	case 192: BC = 6; break;
	case 256: BC = 8; break;
	default : return (-2);
	}

	switch (keyBits >= blockBits ? keyBits : blockBits) {
	case 128: ROUNDS = 10; break;
	case 192: ROUNDS = 12; break;
	case 256: ROUNDS = 14; break;
	default : return (-3); /* this cannot happen */
	}

	/* make number of rounds sane */
	if (rounds > ROUNDS) rounds = ROUNDS;

	/* begin with a key addition
	 */
	KeyAddition(a,rk[0],BC);
        
	/* at most ROUNDS-1 ordinary rounds
	 */
	for(r = 1; (r <= rounds) && (r < ROUNDS); r++) {
		Substitution(a,S,BC);
		ShiftRow(a,0,BC);
		MixColumn(a,BC);
		KeyAddition(a,rk[r],BC);
	}
	
	/* if necessary, do the last, special, round: 
	 */
	if (rounds == ROUNDS) {
		Substitution(a,S,BC);
		ShiftRow(a,0,BC);
		KeyAddition(a,rk[ROUNDS],BC);
	}

	return 0;
}   
FReply FGridSlotExtension::HandleShiftRow(int32 ShiftAmount)
{
	BeginTransaction(LOCTEXT("MoveWidget", "Move Widget"));

	for ( FWidgetReference& Selection : SelectionCache )
	{
		ShiftRow(Selection.GetPreview(), ShiftAmount);
		ShiftRow(Selection.GetTemplate(), ShiftAmount);
	}

	EndTransaction();

	FBlueprintEditorUtils::MarkBlueprintAsModified(Blueprint);

	return FReply::Handled();
}
Esempio n. 10
0
//加密运算,inputSize必须为128,192,256位
void Encrypt(RijndaelContextPtr context,void* input)
{
	unsigned int round;
	//明文的最大长度
	unsigned long state[8] = {0};
	unsigned char* inputStringPtr = (unsigned char*)input;
	unsigned int nb = context->nb;
	//经过这个过程,由a0 a1 a2 a3 a4 a5组成的字节序列就组成了
	// a0 a4 a8  a12 a16 ...
	// a1 a5 a9  a13 a17 ...
	// a2 a6 a10 a14 a18 ...
	// a3 a7 a11 a15 a19 ...
	//这样的字节矩阵
	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);
	}
	AddRoundKey(context,0,state);
	for (round = 1; round<context->nr; round++)
	{
		SubByte(context,state);
		ShiftRow(context,state);
		MixColumn(context,state);
		AddRoundKey(context,round,state);
	}
	SubByte(context,state);
	ShiftRow(context,state);
	AddRoundKey(context,context->nr,state);
	//就这个格式输出的密文,其实也没有关系的
	//将格式纠正
	StateToChars((unsigned char*)input,state,context->nb);
}
Esempio n. 11
0
int _rijndaelEncrypt (word8 a[4][MAXBC], int keyBits, int blockBits, word8 rk[MAXROUNDS+1][4][MAXBC])
{
	/* Encryption of one block. 
	 */
	int r, BC, ROUNDS;

	switch (blockBits) {
	case 128: BC = 4; break;
	case 192: BC = 6; break;
	case 256: BC = 8; break;
	default : return (-2);
	}

	switch (keyBits >= blockBits ? keyBits : blockBits) {
	case 128: ROUNDS = 10; break;
	case 192: ROUNDS = 12; break;
	case 256: ROUNDS = 14; break;
	default : return (-3); /* this cannot happen */
	}

	/* begin with a key addition
	 */
	KeyAddition(a,rk[0],BC); 

        /* ROUNDS-1 ordinary rounds
	 */
	for(r = 1; r < ROUNDS; r++) {
		Substitution(a,S,BC);
		ShiftRow(a,0,BC);
		MixColumn(a,BC);
		KeyAddition(a,rk[r],BC);
	}
	
	/* Last round is special: there is no MixColumn
	 */
	Substitution(a,S,BC);
	ShiftRow(a,0,BC);
	KeyAddition(a,rk[ROUNDS],BC);

	return 0;
}   
Esempio n. 12
0
int rijndaelDecryptRound (word8 a[4][4],  
	word8 rk[MAXROUNDS+1][4][4], int rounds)
/* Decrypt only a certain number of rounds.
 * Only used in the Intermediate Value Known Answer Test.
 * Operations rearranged such that the intermediate values
 * of decryption correspond with the intermediate values
 * of encryption.
 */
{
	int r;
	

	/* make number of rounds sane */
	if (rounds > ROUNDS) rounds = ROUNDS;

        /* First the special round:
	 *   without InvMixColumn
	 *   with extra KeyAddition
	 */
	KeyAddition(a,rk[ROUNDS],4);
	Substitution(a,Si,4);
	ShiftRow(a,1,4);              
	
	/* ROUNDS-1 ordinary rounds
	 */
	for(r = ROUNDS-1; r > rounds; r--) {
		KeyAddition(a,rk[r],4);
		InvMixColumn(a,4);      
		Substitution(a,Si,4);
		ShiftRow(a,1,4);                
	}
	
	if (rounds == 0) {
		/* End with the extra key addition
		 */	
		KeyAddition(a,rk[0],4);
	}    

	return 0;
}
Esempio n. 13
0
void  chiffrerBloc(unsigned char mat[TAILLEMATRICE][TAILLEMATRICE], unsigned char cleEtendue[TAILLEMATRICE*(NBROUND+1)][TAILLEMATRICE]){

int ligne, colonne, round;
        // Traitement du bloc

        for(round=0; round<NBROUND; round++){

        /*//DEBUG BLOC EN CLAIR ------------------------------------------

            printf("\nBLOC EN CLAIR \n");

            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 BLOC EN CLAIR---------------------------------------*/

            AddRoundKey(cleEtendue,round,mat);

            /*//DEBUG ADDROUNDKEY----------------------------------------------

            printf("\nADDROUNDKEY [%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 BLOC EN CLAIR---------------------------------------*/

            SubBytes(mat);

            /*//DEBUG SUBBYTES------------------------------------------

            printf("DEBUG 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");
            // FIN DEBUG SUBBYTES---------------------------------------*/

            ShiftRow(mat);

            /*//DEBUG SHIFTROW------------------------------------------

            printf("DEBUG SHIFTROW[%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 SHIFTROW---------------------------------------*/

            if(round!=(NBROUND-1))
            {
                MixColumns(mat);

               /* //DEBUG MIXCOLUMNS------------------------------------------

                printf("DEBUG 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 MIXCOLUMNS---------------------------------------*/
            }
        }

        AddRoundKey(cleEtendue,round,mat);



       //DEBUG  MESSAGE CRYPTE EN HEXA------------------------------------------

        printf("BLOC CHIFFRE 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");
        }

        // FIN DEBUG MESSAGE CRYPTE EN HEXA---------------------------------------

}