#ifndef aes_h
#define aes_h
#include #include typedef unsigned char byte;
enum keysize // key size, in bits, for construtor
;class qfile;
class aes
;#endif // aes_h
#include #include #include #include "aes.h"
const unsigned char aes::sbox[16*16]=
;const unsigned char aes::isbox[16*16]=
;const unsigned char aes::rcon[11*4]=
;aes::aes(int keysize, unsigned char *key)
void aes::runkeyexpansion(int keysize, unsigned char* keybytes)
void aes::setnbnknr(int keys)
else if (keys == bits192)
else if (keys == bits256)
}/* 密碼加密 */
void aes::keyexpansion()
unsigned char temp[4];
for (row = nk; row < nb * (nr+1); row++)else if ( nk > 6 && (row % nk == 4) )
// w[row] = w[row-nk] xor temp
w[4*row+0] = (byte) ( (int) w[4*(row-nk)+0] ^ (int)temp[0] );
w[4*row+1] = (byte) ( (int) w[4*(row-nk)+1] ^ (int)temp[1] );
w[4*row+2] = (byte) ( (int) w[4*(row-nk)+2] ^ (int)temp[2] );
w[4*row+3] = (byte) ( (int) w[4*(row-nk)+3] ^ (int)temp[3] );
}}/* 1.1: 金鑰擴充套件 位置變換,把乙個4位元組的基金鑰序列[a0,a1,a2,a3],左移乙個位元組變為[a1,a2,a3,a0]。 */
void aes::rotword(unsigned char * word,unsigned char *result)
/* 1.2: 金鑰擴充套件位置變換, 對乙個4位元組的輸入字[a0,a1,a2,a3],每乙個位元組進行s盒變換 */
void aes::subword(unsigned char * word,unsigned char* result)
/* 2: 輪金鑰加(add round key) */
void aes::addroundkey(int round)
}}/* 3.1: 位元組替代,從s-box中找出s[a]=s[x,y]的值,其中a為輸入矩陣任一元素。 */
void aes::subbytes()
}}/* 3.2: 逆變換位元組替代,將s-box中的s[x,y]=s[a],其中a為輸入矩陣任一元素。 */
void aes::invsubbytes()
}}/* 4.1: 行移位,第一行維持不變,第
二、第三、第四行的每個位元組向左迴圈移位的偏移量分別為1格,2格,3格 */
void aes::shiftrows()
}for (r = 1; r < 4; r++)
}}/* 4.2: 逆變換行移位 */
void aes::invshiftrows()
}for (r = 1; r < 4; r++)
}}/* 5.1: 列混淆 */
void aes::mixcolumns()
}for (c = 0; c < 4; c++)
}/* 5.2: 逆變換列混淆 */
void aes::invmixcolumns()
}for (c = 0; c < 4; c++)
}/* 5.3: 列混淆互換操作 */
unsigned char aes::gfmultby01(unsigned char b)
unsigned char aes::gfmultby02(unsigned char b)
unsigned char aes::gfmultby03(unsigned char b)
unsigned char aes::gfmultby09(unsigned char b)
unsigned char aes::gfmultby0b(unsigned char b)
unsigned char aes::gfmultby0d(unsigned char b)
unsigned char aes::gfmultby0e(unsigned char b)
void aes::cipher(unsigned char* input, unsigned char* output)
/* 直接與第0組金鑰w[0,3](即基金鑰key)進行異或,作為輪加密的輸入 */
addroundkey(0);
/* 迴圈進行位元組替代、行位移、列混淆、輪金鑰加運算 */
for (int round = 1; round <= (nr - 1); round++)
/* 最後一輪運算不進行列混淆變換 */
subbytes();
shiftrows();
addroundkey(nr);
/* 最後,輸出128位的密文位元流 */
for (i = 0; i < (4 * nb); i++)
}void aes::invcipher(unsigned char * input, unsigned char * output)
/* 最後一輪運直接進行異或 */
addroundkey(nr);
/* 迴圈進行行位移、位元組替代、輪金鑰加運算、 列混淆*/
for (int round = nr-1; round >= 1; round--)
invshiftrows();
invsubbytes();
addroundkey(0);
/* 最後,輸出解密的128位的密文位元流 */
for (i = 0; i < (4 * nb); i++)
}
#include "aes.h"
static unsigned char key =
;int main(int argc, char **ar**)
; unsigned char inv_input[16] = ;
aes aes(bits128,key);
qstring input_str = "hello world!!!";
qstring output_str;
qdebug() << "input = " << input_str;
aes.cipher((unsigned char*)input_str.tolatin1().data(),output);
qdebug() << "after ciphe = " << qstring((const char*)&output);
aes.invcipher(output,inv_input);
qdebug() << "after invcipher = " << qstring((const char*)&inv_input);}
參考:
aes 128位加密 解密
coding utf 8 import sys from crypto.cipher import aes from crypto import random from binascii import b2a hex,a2b hex class prpcrypt def init self,key ...
Android加密之Md5,AES 加密
1 md5加密 public final static string md5encry string s try return new string str catch exception e 2 aes加 解密 aes對稱加密解密類 public class aeshelper stringbuf...
對稱加密之DES與AES
金鑰加密也稱為對稱加密,速度快,但加密和解密的鑰匙必須相同,只有通訊雙方才能知道鑰匙。高階資料加密標準 aes author 尹勇 public class aesdome public static void main string args throws exception author 尹勇 ...