AES C ++ implementation (128-bit key)

xiaoxiao2021-03-30  191

// Write a AES's C implementation, only support 128-bit keys, write in a hurry, not standardized, for reference only.

// aes.h

#ifndef AES_H_

#define aes_h_

#include

#include

Using namespace std;

Class AES

{

PUBLIC:

TYPEDEF UNSIGNED CHAR BYTE

Static const Int key_size = 16; // key length is 128 bits

Static const INT n_round = 11;

Byte plaintext [16]; // 明文

Byte State [16]; // Current packet.

Byte CipherKey [16]; // Key

Byte runkkey [n_round] [16]; // Ranker

Byte Ciphertext [16]; // Ciphertext

Byte SBOX [16] [16]; // S box

BYTE INVSBOX [16] [16]; // reverse S box

Void EncryptionProcess ();

Void DecryptionProcess ();

Void Round (Const Int & Round);

Void Invround; Const Int (Const INT & ROUND);

Void finalRound ();

Void invfinalRound ();

Void keyexpansion ();

Void AddroundKey (const INT & ROUND);

Void subbytes ();

VoidInvsubbytes ();

Void Shiftrows ();

Void invshifty ();

void mixcolumns ();

Void InvmixColumns ();

Void Buildsbox ();

Void Buildinvsbox ();

Void InitialState (Const Byte * text);

Void initialciphertext ();

Void InitialPlainText ();

BYTE GFMULTPLYBYTE (Const Byte & LEFT, Const Byte & Right);

Const byte * gfmultplybytesmatrix (const Byte * Right);

PUBLIC:

AES ();

Const Byte * Cipher (const byte * text, const byte * key, const INT & Keysize);

Const Byte * Invcipher (const byte * text, const type * key, const INT & Keysize)

}

Void AES :: EncryptionProcess ()

{// encryption process

InitialState (Plaintext);

KeyExpansion (); // key extension

AddroundKey (0); // Round key plus

For (INT I = 1; I

{

Round (i);

}

FinalRound ();

Initialciphertext ();

}

Void AES :: DecryptionProcess ()

{// decryption process

InitialState (Ciphertext);

KeyExpansion ();

InvfinalRound ();

For (int i = n_round-2; i> 0; --I) {

Invround (i);

}

AddroundKey (0);

InitialPlaintext ();

}

Void AES :: Round (Const Int & Round)

{/ Normal wheel

Subbytes ();

Shiftrows ();

Mixcolumns ();

AddroundKey (Round);

}

Void AES :: Invround (Const Int & Round)

{// Normal wheel reverse

AddroundKey (Round);

InvmixColumns ();

INVSHIFTROWS ();

INVSUBBYTES ();

}

Void AES :: FinalRound ()

{// last wheel

Subbytes ();

Shiftrows ();

AddroundKey (n_round - 1);

}

Void AES :: InvfinalRound ()

{// the reverse of the last wheel

AddroundKey (n_round - 1);

INVSHIFTROWS ();

INVSUBBYTES ();

}

Void AES :: Keyexpansion ()

{// key extension

Const byte rcon [n_round] [4] = {

{0x00, 0x00, 0x00, 0x00},

{0x01, 0x00, 0x00, 0x00},

{0x02, 0x00, 0x00, 0x00},

{0x04, 0x00, 0x00, 0x00},

{0x08, 0x00, 0x00, 0x00},

{0x10, 0x00, 0x00, 0x00},

{0x20, 0x00, 0x00, 0x00},

{0x40, 0x00, 0x00, 0x00},

{0x80, 0x00, 0x00, 0x00},

{0x1b, 0x00, 0x00, 0x00},

{0x36, 0x00, 0x00, 0x00}};

For (int i = 0; i <16; i)

{

Roundkey [0] [I] = CipherKey [i];

}

For (int i = 0; i <4; i)

{// RoundKey [0] [16] is a transparent matrix of CipherKey

For (int J = 0; j <4; j)

{

Roundkey [0] [4 * i j] = CipherKey [4 * J i];

}

}

For (int roundIndex = 1; roundindex

{

BYTE ROTWORD [4] = {0x00};

RotWord [0] = ROUNDKEY [ROUNDINDEX - 1] [3];

Rotword [1] = roundkey [RoundIndex - 1] [7];

Rotword [2] = roundkey [RoundIndex - 1] [11];

RotWord [3] = Roundkey [RoundIndex - 1] [15];

Std :: swap (Rotword [0], Rotword [1]);

Std :: swap (Rotword [1], Rotword [2]);

Std :: swap (Rotword [2], Rotword [3]);

For (int i = 0; i <4; i)

{

Rotword [i] = sbox [rotword [i] >> 4] [Rotword [i] & 0x0f]; Roundkey [RoundIndex] [4 * i] = roundkey [RoundIndex - 1] [4 * i] ^ rotword [i] ^ rcon [roundindex] [i];

}

For (int J = 1; j <4; J)

{

For (int i = 0; i <4; i)

{

Roundkey [RoundIndex] [4 * i j] = runkkey [RoundIndex - 1] [4 * i j] ^ Roundkey [RoundIndex] [4 * i J - 1];

}

}

}

}

Void AES :: AddroundKey (const INT & ROUND)

{// round key plus

For (int i = 0; i <16; i)

{/ Use the current packet State and the Round group extended key to position or

State [i] ^ = runkkey [round] [i];

}

}

Void AES :: Subbytes ()

{// byte replacement

For (int i = 0; i <16; i)

{

State [i] = SBOX [State [i] >> 4] [state [i] & 0x0f];

}

}

Void AES :: INVSUBBYTES ()

{// Reverse byte

For (int i = 0; i <16; i)

{

State [i] = invsbox [state [i] >> 4] [State [i] & 0x0f];

}

}

Void AES :: ShifTrows ()

{// row transformation

// State first line remains unchanged

// do nothing.

// State second line loop left shift one byte

Std :: swap (State [4], State [5]);

Std :: swap (State [5], State [6]);

Std :: swap (State [6], State [7]);

// State third line loop left shift two bytes

Std :: swap (State [8], State [10]);

Std :: swap (State [9], State [11]);

// State Third line loop left shift three bytes

Std :: swap (State [14], State [15]);

Std :: swap (State [13], State [14]);

Std :: swap (State [12], State [13]);

}

Void AES :: INVSHIFTROWS ()

{// row transformation inversion

// State first line remains unchanged

// do nothing.

// State second line loop right shift one byte

Std :: swap (State [6], State [7]);

Std :: swap (State [5], State [6]);

Std :: swap (State [4], State [5]);

// State Third line loop right shift two bytes

Std :: swap (State [9], State [11]);

Std :: swap (State [8], State [10]);

// State Third line loop right shift three bytes std :: swap (state [12], state [13]);

Std :: swap (State [13], State [14]);

Std :: swap (State [14], State [15]);

}

Void AES :: MixColumns ()

{// column confusion

Byte Matrix [4] [4] = {

{0x02, 0x03, 0x01, 0x01},

{0x01, 0x02, 0x03, 0x01},

{0x01, 0x01, 0x02, 0x03},

{0x03, 0x01, 0x01, 0x02}};

Const byte * temp = gfmultplybytesmatrix ((byte *) matrix, state);

For (int i = 0; i <16; i)

{

State [i] = temp [i];

}

DELETE [] TEMP;

}

Void AES :: InvmixColumns ()

{// Column confused inversion

Byte Matrix [4] [4] = {

{0x0e, 0x0b, 0x0d, 0x09},

{0x09, 0x0e, 0x0b, 0x0d},

{0x0D, 0x09, 0x0e, 0x0b},

{0x0b, 0x0d, 0x09, 0x0e}};

Const byte * temp = gfmultplybytesmatrix ((byte *) matrix, state);

For (int i = 0; i <16; i)

{

State [i] = temp [i];

}

DELETE [] TEMP;

}

Void AES :: buildsbox ()

{// build S box

BYTE BOX [16] [16] =

{

/ * 0 1 2 3 4 5 6 7 8 9 A b C D e f * /

/ * 0 * / {0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xAb, 0x76},

/ * 1 * / {0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xA2, 0xAD, 0x9c, 0xA2, 0xAF, 0x9c, 0xA4, 0x72, 0xc0},

/ * 2 * / {0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15},

/ * 3 * / {0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75},

/ * 4 * / {0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84},

/ * 5 * / {0x53, 0x20, 0x00, 0xed, 0x20, 0x09, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf},

/ * 6 * / {0xD0, 0x1f, 0xAa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0x19, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8}, / * 7 * / {0x51, 0xa3, 0x40 , 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2},

/ * 8 * / {0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73},

/ * 9 * / {0x60, 0x81, 0x4f, 0xDC, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb},

/ * a * / {0xE0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0x91, 0x95, 0x62, 0x91, 0x95, 0xe4, 0x79},

/ * b * / {0xE7, 0xc8, 0x37, 0x6d, 0x8d, 0x6c, 0x4e, 0xa9, 0x6c, 0x56, 0x1, 0xea, 0x65, 0x7a, 0xae, 0x08},

/ * c * / {0xBA, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0x4b, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a},

/ * D * / {0x70, 0x3e, 0x03, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0x/9, 0x86, 0xc1, 0x1d, 0x9e},

/ * E * / {0xe1, 0xf8, 0x98, 0x11, 0x69, 0x9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf},

/ * f * / {0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16}

}

For (int i = 0; i <16; i)

{

For (int J = 0; j <16; J)

{

SBOX [I] [J] = Box [i] [j];

}

}

}

Void AES :: buildinvsbox ()

{// build an inverse S box

BYTE BOX [16] [16] =

{

/ * 0 1 2 3 4 5 6 7 8 9 A b C D e f * /

/ * 0 * / {0x52, 0x09, 0x6a, 0xD5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb},

/ * 1 * / {0x7c, 0xE3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb},

/ * 2 * / {0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e},

/ * 3 * / {0x08, 0x2e, 0xa1, 0x66, 0x28, 0x76, 0x24, 0xa2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0x21, 0x25}, / * 4 * / {0x72, 0xf8, 0xf6 , 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92},

/ * 5 * / {0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0x46, 0x57, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84},

/ * 6 * / {0x90, 0x8, 0xAb, 0x00, 0x8c, 0xbc, 0x2, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06},

/ * 7 * / {0xD0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b},

/ * 8 * / {0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73},

/ * 9 * / {0x96, 0xAc, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1c, 0x75, 0xDF, 0x6e},

/ * a * / {0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b},

/ * b * / {0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0x9a, 0x79, 0x20, 0x9a, 0x78, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4},

/ * c * / {0x1f, 0xDD, 0xA8, 0x333, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f},

/ * d * / {0x60, 0x51, 0x7f, 0xa9, 0x19, 0x2d, 0x4a, 0x0d, 0x2d, 0x9, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef},

/ * E * / {0xA0, 0xE0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0x83, 0x53, 0x99, 0x61},

/ * f * / {0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d}

}

For (int i = 0; i <16; i)

{

For (int J = 0; j <16; J)

{

INVSBOX [I] [J] = Box [i] [j];

}

}

}

Void AES :: InitialState (const Byte * text)

{// state initial time is the transposition matrix of the matrix

For (int i = 0; i <4; i)

{// Transfer text stores in State

For (int J = 0; j <4; j)

{

State [4 * i j] = text [4 * j i];

}

}

}

Void AES :: InitialCiphertext ()

{// State is copied into the output matrix

For (int i = 0; i <4; i)

{// Transfer State stored in Ciphertext

For (int J = 0; j <4; j)

{

Ciphertext [4 * i j] = state [4 * j i];

}

}

}

Void AES :: InitialPlainText ()

{// State is copied into the input matrix

For (int i = 0; i <4; i)

{// Transfer State stored in PlainText

For (int J = 0; j <4; j)

{

Plaintext [4 * i j] = state [4 * j i];

}

}

}

AES :: Byte AES :: GFmultPlyByte (Const Byte & Right)

{// limited domain GF (2 ^ 8) Multiplication

Byte Temp [8];

BitSet <8> BITS ((unsigned long); // put Right into 8 binary bits in BITS

Temp [0] = Left;

For (int i = 1; i <8; i)

{

IF (Temp [i-1]> = 0x80) // (TEMP [I-1] first is "1"

{

Temp [i] = Temp [i-1] << 1;

Temp [i] = TEMP [i] ^ 0x1b; // and (00011011) distant or

}

Else

{

Temp [i] = Temp [i-1] << 1;

}

}

BYTE Result = 0x00;

For (int i = 0; i <8; i)

{

IF (BITS [I] == 1)

{

Result ^ = Temp [i];

}

}

Return Result;

}

Const AES :: Byte * AES :: GFMultPlybytesmatrix (const byte * ip, const byte * right)

{// Timber domain GF (2 ^ 8) matrix (4 * 4) multiplication

AES :: Byte * Result = New AES :: Byte [16];

For (int i = 0; i <4; i)

{

For (int J = 0; j <4; j)

{

Result [4 * i j] = gfmultplybyte (Left [4 * I], Right [J]);

For (int K = 1; k <4; k)

{

Result [4 * i j] ^ = gfmultplybyte (Left [4 * i k], Right [4 * K J]);

}

}

}

Return Result;

}

AES :: AES ()

{

Buildsbox ();

Buildinvsbox ();

}

Const AES :: Byte * AES :: Cipher (const byte * text, const Byte * key, const Int & keysize)

{/ K 给 t t t t

For (int i = 0; i <16; i) {

Plaintext [i] = text [i];

}

For (int i = 0; i

{

CipherKey [i] = key [i];

}

EncryptionProcess ();

Return Ciphertext;

}

Const AES :: Byte * AES :: Invcipher (const Byte * Text, const Byte * key, const Int & keySize)

{/ Decryption with key to Text

For (int i = 0; i <16; i)

{

Ciphertext [I] = text [i];

}

For (int i = 0; i

{

CipherKey [i] = key [i];

}

DecryptionProcess ();

Return PLAINTEXT;

}

#ENDIF / * AES_H_ * /

// main.cpp

#include

#include

#include

#include "aes.h"

Using namespace std;

Int main (int Argc, char * argv [])

{

Const string usage = "usage: aes [-e | -d] destinationfile sourcefile keyfile";

IF (argc! = 5)

{

Cout << usage << Endl;

Return 1;

}

IFStream IS (Argv [3], iOS :: in | os :: binary);

IF (! IS)

{

CERR << "INPUTFILENOTFOUNDEXCEPTION" << ENDL;

Return 2;

}

IFStream Ks (Argv [4], ios :: in | os :: binary);

IF (! ks)

{

CERR << "KeyFilenotFoundException" << Endl;

Return 2;

}

AES AES;

Const unsigned char * key = new unsigned char [16];

KS.READ ((char *) key, 16);

Ofstream OS (Argv [2], ios :: out | ios :: binary);

IF (strCMP (Argv [1], "-e") == 0 || StrCMP (Argv [1], "-e") == 0)

{

Const unsigned char * ciphertext;

Const unsigned char * plaintext = new unsigned char [16];

While (is.read ((char *) Plaintext, 16))

{

Ciphertext = AES.CIPHER (PlainText, Key, 16);

Os.write (Const char *) Ciphertext, 16);

}

}

IF (strcmp (argv [1], "-d") == 0 || strcmp (Argv [1], "-d") == 0)

{

Const unsigned char * plaintext; const unsigned char * ciphertext = new unsigned char [16];

While (is.read ((char *) ciphertext, 16))

{

PlainText = AES.INVCIPHER (Ciphertext, Key, 16);

Os.Write (const char *) plaintext, 16);

}

DELETE [] Ciphertext;

}

Delete [] key;

Is.close ();

Ks.close ();

Os.Close ();

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-130568.html

New Post(0)