//------------------------------------------------------ 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); }
//------------------------------------------------------ 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); }
char rijndaelEncrypt () { /* Encryption of one block. */ char r; /* begin with a key addition */ KeyAddition ( 0 ); /* ROUNDS-1 ordinary rounds */ for ( r = 1; r < ROUNDS; r++ ) { SubstitutionS(); ShiftRow0(); MixColumn(); KeyAddition ( r ); } /* Last round is special: there is no MixColumn */ SubstitutionS(); ShiftRow0(); KeyAddition ( ROUNDS ); return 0; }
/*------------------------------------------------------------------- * 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 */
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; }
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; }
//加密运算,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); }
void Cipher(WORD block[Nb], WORD key[Nb*(Nr+1)]) { for(int round=1; round<Nr; round++) { log_it("Round %d started with:\n", block); // ByteSub for(int i=0; i<Nb; i++) { BYTE* temp = (BYTE*)&block[i]; for (int j = 0; j < 4; j++) temp[j] = SubBytesTab[temp[j]]; block[i] = pack(temp); } log_it("After ByteSub\n", block); // ShiftRows ShiftRows(block); log_it("After ShiftRows\n", block); // MixColumn MixColumn(block); log_it("After MixColumn\n", block); // AddRoundKey AddRoundKey(block, &key[4*round]); log_it("After AddRoundKey\n", block); } for(int i=0; i<Nb; i++) { BYTE* temp = (BYTE*)&block[i]; for (int j = 0; j < 4; j++) temp[j] = SubBytesTab[temp[j]]; block[i] = pack(temp); } ShiftRows(block); AddRoundKey(block, &key[4*Nr]); log_it("After AddRoundKey\n", block); }
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; }
void aes(TCryptoEngine* engine) { TAES key; TAES state; uint8_t t[4]; uint8_t x, rcon; uint8_t round; memcpy(&key, &engine->key, AES_BLOCK_SIZE); memcpy(&state, &engine->in, AES_BLOCK_SIZE); aes_add_round_keys(key, state); rcon = 1; for (round = 0; round < AES_ROUNDS; round++) { /* unroll SubBytes */ SubBytes(0); SubBytes(1); SubBytes(2); SubBytes(3); SubBytes(4); SubBytes(5); SubBytes(6); SubBytes(7); SubBytes(8); SubBytes(9); SubBytes(10); SubBytes(11); SubBytes(12); SubBytes(13); SubBytes(14); SubBytes(15); /* row 2 */ x = state[1]; state[1] = state[5]; state[5] = state[9]; state[9] = state[13]; state[13] = x; /* row 3 */ x = state[2]; state[2] = state[10]; state[10] = x; x = state[6]; state[6] = state[14]; state[14] = x; /* row 4 */ x = state[3]; state[3] = state[15]; state[15] = state[11]; state[11] = state[7]; state[7] = x; if (round < (AES_ROUNDS-1)) { MixColumn(0); MixColumn(4); MixColumn(8); MixColumn(12); } key[0] ^= g_sbox[key[13]] ^ rcon; key[1] ^= g_sbox[key[14]]; key[2] ^= g_sbox[key[15]]; key[3] ^= g_sbox[key[12]]; key[4] ^= key[0]; key[5] ^= key[1]; key[6] ^= key[2]; key[7] ^= key[3]; key[8] ^= key[4]; key[9] ^= key[5]; key[10] ^= key[6]; key[11] ^= key[7]; key[12] ^= key[8]; key[13] ^= key[9]; key[14] ^= key[10]; key[15] ^= key[11]; aes_add_round_keys(key, state); /* update rcon */ rcon = aes_xtime(rcon); } memcpy(&engine->out, &state, AES_BLOCK_SIZE); }