[Reprinted] aes-encrypted source code version C (2)

Source: Internet
Author: User
# Define decrypt true # define encrypt false # define type bool typedef struct _ AES {int NB; int NR; int NK; unsigned long * word; unsigned long * State;} AEs; /* encrypt data byte * input plaintext byte * insize plaintext Length Byte * out ciphertext storage location byte * Key key byte * keysize key length */void cipher (unsigned char * input, int insize, unsigned char * Out, unsigned char * Key, int keysize);/* decrypt data byte * input ciphertext int * insize ciphertext long byte * out Byte * Key key int * keysize key length */void invcipher (unsigned char * input, int insize, unsigned char * Out, unsigned char * key, int keysize);/* generate the parameter AES structure for encryption int insize block size byte * Key int key length unsigned long attribute (standard type) returns the AES structure pointer */AES * initaes (AES * AES, int insize, unsigned char * Key, int keysize, type ); /* generate the parameter AES structure for encryption int insize block size byte * Key int key length return AES structure pointer */AES * initaes (INT insize, Unsigned char * Key, int keysize, bool);/* perform the NR round operation during encryption. AES * parameter during AES runtime */void cipherloop (AES * AES ); /* re-calculate the NR round during decryption AES * AES runtime parameter */void invcipherloop (AES * AES ); /* release the AES structure and state and keystore word */void freeaes (AES * AES); // AES. CPP # include "stdafx. H "# include" AES. H "unsigned char * subword (unsigned char * Word); unsigned long * keyexpansion (unsigned char * Key, int NK, int NR, INT );/ * Encrypted data byte * input plaintext byte * insize plaintext Length Byte * out ciphertext storage location byte * Key key byte * keysize key length */void cipher (unsigned char * input, int insize, unsigned char * Out, unsigned char * Key, int keysize) {AES AEs; initaes (& AES, insize, key, keysize, encrypt); memcpy (AES. state, input, insize); cipherloop (& AES); memcpy (Out, AES. state, insize);}/* decrypt data byte * input ciphertext int * insize ciphertext long byte * out plaintext location byte * Key key Ke Y int * keysize key length */void invcipher (unsigned char * input, int insize, unsigned char * Out, unsigned char * Key, int keysize) {AEs; initaes (& AES, insize, key, keysize, decrypt); memcpy (AES. state, input, insize); invcipherloop (& AES); memcpy (AES. state, out, insize );} /* generate the parameter AES structure for encryption int insize block size byte * Key int key length return AES structure pointer */AES * initaes (AES * AES, int insize, unsigned char * Key, int keysize, type) {Int NB = insize> 2, nk = keysize> 2, Nr = Nb <NK? NK: Nb + 6; AES-> NB = Nb; AES-> NK = NK; AES-> Nr = nR; AES-> word = keyexpansion (Key, Nb, NR, NK); AES-> state = new unsigned long [Nb + 3]; If (type) AES-> state + = 3; return AEs ;} /* generate the parameter AES structure for encryption int insize block size byte * Key int key length return AES structure pointer */AES * initaes (INT insize, unsigned char * Key, int keysize, unsigned long type) {return initaes (New AES (), insize, key, keysize, type);}/**/void cipherloop (AES * AES) {unsigned char temp [4]; unsigned long * word8 = aes-> word, * State = aes-> state; int NB = aes-> NB, nr = aes-> NR; int R; For (r = 0; r <NB; ++ R) {State [R] ^ = word8 [R];} for (INT round = 1; round {word8 + = Nb;/* assume that Nb = 4; california | S0 | S1 | S2 | S3 | --------------------- | S4 | S5 | S6 | S7 | ------------------- | S8 | S9 | sa | Sb | ----------------------- | SC | SD | se | SF | --------------------- | ----------------------- | --------------------- */memcpy (State + NB, state, 12);/* NB = 4; california | S0 | --------------------- | S4 | S5 | small | S8 | S9 | sa | ------------------- | SC | SD | se | SF | ----------------------- | S1 | S2 | S3 | --------------------- | S6 | S7 | --------------------- | Sb | --------------------- */For (r = 0; r {/* temp = {sbox [S0], sbox [S5], sbox [SA], sbox [SF]}; */temp [0] = sbox [* (unsigned char *) State)]; temp [1] = sbox [* (unsigned char *) (State + 1) + 1)]; temp [2] = sbox [* (unsigned char *) (State + 2) + 2)]; temp [3] = sbox [* (unsigned char *) (State + 3) + 3)]; * (unsigned char *) State) = log_02 [temp [0] ^ log_03 [temp [1] ^ temp [2] ^ temp [3]; * (unsigned char *) State + 1) = log_02 [temp [1] ^ log_03 [temp [2] ^ temp [3] ^ temp [0]; * (unsigned char *) State + 2) = log_02 [temp [2] ^ log_03 [temp [3] ^ temp [0] ^ temp [1]; * (unsigned char *) State + 3) = log_02 [temp [3] ^ log_03 [temp [0] ^ temp [1] ^ temp [2]; * State ^ = word8 [R]; state ++;} state-= Nb;} memcpy (State + NB, state, 12); word8 + = Nb; For (r = 0; r {* (unsigned char *) State) = sbox [* (unsigned char *) State]; * (unsigned char *) State + 1) = sbox [* (unsigned char *) (State + 1) + 1)]; * (unsigned char *) State + 2) = sbox [* (unsigned char *) (State + 2) + 2)]; * (unsigned char *) State + 3) = sbox [* (unsigned char *) (State + 3) + 3)]; * State ^ = word8 [R]; State ++ ;}} /* re-calculate the NR round during decryption AES * AES runtime parameter */void invcipherloop (AES * AES) {unsigned long * word = aes-> word, * State = aes-> state; int NB = aes-> NB, Nr = aes-> NR; unsigned char temp [4]; int r = 0; word + = Nb * NR; For (r = 0; r <NB; ++ R) {State [R] ^ = word [R];} state-= 3; for (INT round = Nr-1; Round> 0; -- round) {/* assume that Nb = 4; --------------------- | S0 | S1 | S2 | S3 | S3 | S4 | S5 | S6 | S7 | ------------------- | s8 | S9 | sa | Sb | ------------------- | SC | SD | se | SF | --------------------- */memcpy (state, state + NB, 12);/* NB = 4; certificate | S7 | ------------------- | sa | Sb | certificate | SD | se | SF | --------------------- | S0 | S1 | S2 | S3 | --------------------- | S4 | S5 | s6 | --------------------- | S8 | S9 | --------------------- | SC | --------------------- */word-= Nb; state + = Nb + 2; for (r = Nb-1; r> = 0; r --) {/* temp = {isbox [S0], isbox [SD], isbox [SA], isbox [S7]}; */temp [0] = isbox [* (byte *) State]; temp [1] = isbox [* (byte *) (State-1) + 1)]; temp [2] = isbox [* (byte *) (State-2) + 2)]; temp [3] = isbox [* (byte *) (State-3) + 3)]; * (unsigned long *) temp ^ = word [R]; * (unsigned char *) state = log_0e [temp [0] ^ log_0b [temp [1] ^ log_0d [temp [2] ^ log_09 [temp [3]; * (unsigned char *) State + 1) = log_0e [temp [1] ^ log_0b [temp [2] ^ log_0d [temp [3] ^ log_09 [temp [0]; * (unsigned char *) state + 2) = log_0e [temp [2] ^ log_0b [temp [3] ^ log_0d [temp [0] ^ log_09 [temp [1]; * (unsigned char *) State + 3) = log_0e [temp [3] ^ log_0b [temp [0] ^ log_0d [temp [1] ^ log_09 [temp [2]; State --;} state-= 2;} Word-= Nb; memcpy (State, State + NB, 12); State + = Nb + 2; for (r = Nb-1; r> = 0; r --) {* (unsigned char *) State = isbox [* (unsigned char *) State]; * (unsigned char *) State + 1) = isbox [* (unsigned char *) (State-1) + 1)]; * (unsigned char *) State + 2) = isbox [* (unsigned char *) (State-2) + 2)]; * (unsigned char *) State + 3) = isbox [* (unsigned char *) (State-3) + 3)]; * State ^ = word [R]; State --;}} /** ------------------------------------------ * | K0 | K1 | K2 | K3 | K4 | K5 | K6 | K7 | K8 | K9 | ....... | NK * 4 | * authorization * Nr wheel keystore * each key column has a length of Nb * --------------------- * | K0 | K1 | K2 | K3 | * --------------------- * | K4 | K5 | K6 | K7 | * kernel * | K8 | K9 | Ka | kb | * --------------------- * | KC | KD | ke | KF | * ------------------- */unsigned long * keyexpansion (byte * key, int Nb, int NR, int NK) {unsigned long * w = new unsigned long [NB * (NR + 1)]; // 4 columns of bytes corresponds to a word memcpy (W, key, NK <2); unsigned long temp; For (INT c = NK; c <Nb * (NR + 1); ++ c) {// put the last line of the previous round into temp = W [C-1]; // determine whether the first line of each key is if (C % nk = 0) {// left-hand 8-bit temp = (temp <8) | (temp> 24); // query the sbox table subword (byte *) & temp); temp ^ = rcon [C/nk];} else if (NK> 6 & (C % nk = 4) {subword (byte *) & temp );} // W [C-nk] is the first line of the previous key W [c] = W [C-nk] ^ temp;} return W ;} unsigned char * subword (unsigned char * Word) {word [0] = sbox [word [0]; word [1] = sbox [word [1]; word [2] = sbox [word [2]; word [3] = sbox [word [3]; return word ;} /* release the AES structure and state and keystore word */void freeaes (AES * AES) {// For (INT I = 0; INB; I ++) // {// printf ("% d \ n", I); // free (AES-> State [I]); // free (AES-> word [I]); //} // printf ("sdffd ");}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.