⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rijndael.cpp

📁 Rijndael加密算法
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	2144161806, 1908694277, 1675577880, 1842759443, 
	-684598070, -650587711, -886847780, -987051049, 
	-283776794, -518199827, -217582864, -49348613, 
	-1485196142, -1452230247, -1150570876, -1251826801, 
	-1621262146, -1856729675, -2091935064, -1924753501
};

const int CRijndael::sm_U2[256] =
{
	0, 185469197, 370938394, 487725847, 
	741876788, 657861945, 975451694, 824852259, 
	1483753576, 1400783205, 1315723890, 1164071807, 
	1950903388, 2135319889, 1649704518, 1767536459, 
	-1327460144, -1141990947, -1493400886, -1376613433, 
	-1663519516, -1747534359, -1966823682, -2117423117, 
	-393160520, -476130891, -24327518, -175979601, 
	-995558260, -811141759, -759894378, -642062437, 
	2077965243, 1893020342, 1841768865, 1724457132, 
	1474502543, 1559041666, 1107234197, 1257309336, 
	598438867, 681933534, 901210569, 1052338372, 
	261314535, 77422314, 428819965, 310463728, 
	-885281941, -1070226842, -584599183, -701910916, 
	-419197089, -334657966, -249586363, -99511224, 
	-1823743229, -1740248562, -2057834215, -1906706412, 
	-1082931401, -1266823622, -1452288723, -1570644960, 
	-156404115, -39616672, -525245321, -339776134, 
	-627748263, -778347692, -863420349, -947435186, 
	-1361232379, -1512884472, -1195299809, -1278270190, 
	-2098914767, -1981082820, -1795618773, -1611202266, 
	1179510461, 1296297904, 1347548327, 1533017514, 
	1786102409, 1635502980, 2087309459, 2003294622, 
	507358933, 355706840, 136428751, 53458370, 
	839224033, 957055980, 605657339, 790073846, 
	-1921626666, -2038938405, -1687527476, -1872472383, 
	-1588696606, -1438621457, -1219331080, -1134791947, 
	-721025602, -569897805, -1021700188, -938205527, 
	-113368694, -231724921, -282971248, -466863459, 
	1033297158, 915985419, 730517276, 545572369, 
	296679730, 446754879, 129166120, 213705253, 
	1709610350, 1860738147, 1945798516, 2029293177, 
	1239331162, 1120974935, 1606591296, 1422699085, 
	-146674470, -61872681, -513933632, -363595827, 
	-612775698, -797457949, -848962828, -966011911, 
	-1355701070, -1539330625, -1188186456, -1306280027, 
	-2096529274, -2012771957, -1793748324, -1642357871, 
	1201765386, 1286567175, 1371368976, 1521706781, 
	1805211710, 1620529459, 2105887268, 1988838185, 
	533804130, 350174575, 164439672, 46346101, 
	870912086, 954669403, 636813900, 788204353, 
	-1936009375, -2020286868, -1702443653, -1853305738, 
	-1599933611, -1414727080, -1229004465, -1112479678, 
	-722821367, -538667516, -1024029421, -906460130, 
	-120407235, -203640272, -288446169, -440360918, 
	1014646705, 930369212, 711349675, 560487590, 
	272786309, 457992840, 106852767, 223377554, 
	1678381017, 1862534868, 1914052035, 2031621326, 
	1211247597, 1128014560, 1580087799, 1428173050, 
	32283319, 182621114, 401639597, 486441376, 
	768917123, 651868046, 1003007129, 818324884, 
	1503449823, 1385356242, 1333838021, 1150208456, 
	1973745387, 2125135846, 1673061617, 1756818940, 
	-1324610969, -1174273174, -1492117379, -1407315600, 
	-1657524653, -1774573730, -1960297399, -2144979644, 
	-377732593, -495826174, -10465259, -194094824, 
	-985373125, -833982666, -749177823, -665420500, 
	2050466060, 1899603969, 1814803222, 1730525723, 
	1443857720, 1560382517, 1075025698, 1260232239, 
	575138148, 692707433, 878443390, 1062597235, 
	243256656, 91341917, 409198410, 325965383, 
	-891866660, -1042728751, -590666810, -674944309, 
	-420538904, -304014107, -252508174, -67301633, 
	-1834518092, -1716948807, -2068091986, -1883938141, 
	-1096852096, -1248766835, -1467789414, -1551022441, 
};

const int CRijndael::sm_U3[256] =
{
	0, 218828297, 437656594, 387781147, 
	875313188, 958871085, 775562294, 590424639, 
	1750626376, 1699970625, 1917742170, 2135253587, 
	1551124588, 1367295589, 1180849278, 1265195639, 
	-793714544, -574886247, -895026046, -944901493, 
	-459482956, -375925059, -24460122, -209597777, 
	-1192718120, -1243373871, -1560376118, -1342864701, 
	-1933268740, -2117097739, -1764576018, -1680229657, 
	-1149510853, -1234119374, -1586641111, -1402549984, 
	-1890065633, -2107839210, -1790836979, -1739919100, 
	-752637069, -567761542, -919226527, -1002522264, 
	-418409641, -368796322, -48656571, -267222708, 
	1808481195, 1723872674, 1910319033, 2094410160, 
	1608975247, 1391201670, 1173430173, 1224348052, 
	59984867, 244860394, 428169201, 344873464, 
	935293895, 984907214, 766078933, 547512796, 
	1844882806, 1627235199, 2011214180, 2062270317, 
	1507497298, 1423022939, 1137477952, 1321699145, 
	95345982, 145085239, 532201772, 313773861, 
	830661914, 1015671571, 731183368, 648017665, 
	-1119466010, -1337113617, -1487908364, -1436852227, 
	-1989511742, -2073986101, -1820562992, -1636341799, 
	-719438418, -669699161, -821550660, -1039978571, 
	-516815478, -331805821, -81520232, -164685935, 
	-695372211, -611944380, -862229921, -1047501738, 
	-492745111, -274055072, -122203525, -172204942, 
	-1093335547, -1277294580, -1530717673, -1446505442, 
	-1963377119, -2014171096, -1863376333, -1645990854, 
	104699613, 188127444, 472615631, 287343814, 
	840019705, 1058709744, 671593195, 621591778, 
	1852171925, 1668212892, 1953757831, 2037970062, 
	1514790577, 1463996600, 1080017571, 1297403050, 
	-621329940, -671330331, -1058972162, -840281097, 
	-287606328, -472877119, -187865638, -104436781, 
	-1297141340, -1079754835, -1464259146, -1515052097, 
	-2038232704, -1954019447, -1667951214, -1851909221, 
	172466556, 122466165, 273792366, 492483431, 
	1047239000, 861968209, 612205898, 695634755, 
	1646252340, 1863638845, 2013908262, 1963115311, 
	1446242576, 1530455833, 1277555970, 1093597963, 
	1636604631, 1820824798, 2073724613, 1989249228, 
	1436590835, 1487645946, 1337376481, 1119727848, 
	164948639, 81781910, 331544205, 516552836, 
	1039717051, 821288114, 669961897, 719700128, 
	-1321436601, -1137216434, -1423284651, -1507760036, 
	-2062531997, -2011476886, -1626972559, -1844621192, 
	-647755249, -730921978, -1015933411, -830924780, 
	-314035669, -532464606, -144822727, -95084496, 
	-1224610662, -1173691757, -1390940024, -1608712575, 
	-2094148418, -1910056265, -1724135252, -1808742747, 
	-547775278, -766340389, -984645440, -935031095, 
	-344611594, -427906305, -245122844, -60246291, 
	1739656202, 1790575107, 2108100632, 1890328081, 
	1402811438, 1586903591, 1233856572, 1149249077, 
	266959938, 48394827, 369057872, 418672217, 
	1002783846, 919489135, 567498868, 752375421, 
	209336225, 24197544, 376187827, 459744698, 
	945164165, 895287692, 574624663, 793451934, 
	1679968233, 1764313568, 2117360635, 1933530610, 
	1343127501, 1560637892, 1243112415, 1192455638, 
	-590686415, -775825096, -958608605, -875051734, 
	-387518699, -437395172, -219090169, -262898, 
	-1265457287, -1181111952, -1367032981, -1550863006, 
	-2134991011, -1917480620, -1700232369, -1750889146
};

const int CRijndael::sm_U4[256] =
{
	0, 151849742, 303699484, 454499602, 
	607398968, 758720310, 908999204, 1059270954, 
	1214797936, 1097159550, 1517440620, 1400849762, 
	1817998408, 1699839814, 2118541908, 2001430874, 
	-1865371424, -1713521682, -2100648196, -1949848078, 
	-1260086056, -1108764714, -1493267772, -1342996022, 
	-658970480, -776608866, -895287668, -1011878526, 
	-57883480, -176042074, -292105548, -409216582, 
	1002142683, 850817237, 698445255, 548169417, 
	529487843, 377642221, 227885567, 77089521, 
	1943217067, 2061379749, 1640576439, 1757691577, 
	1474760595, 1592394909, 1174215055, 1290801793, 
	-1418998981, -1570324427, -1183720153, -1333995991, 
	-1889540349, -2041385971, -1656360673, -1807156719, 
	-486304949, -368142267, -249985705, -132870567, 
	-952647821, -835013507, -718427793, -601841055, 
	1986918061, 2137062819, 1685577905, 1836772287, 
	1381620373, 1532285339, 1078185097, 1229899655, 
	1040559837, 923313619, 740276417, 621982671, 
	439452389, 322734571, 137073913, 19308535, 
	-423803315, -273658557, -190361519, -39167137, 
	-1031181707, -880516741, -795640727, -643926169, 
	-1361764803, -1479011021, -1127282655, -1245576401, 
	-1964953083, -2081670901, -1728371687, -1846137065, 
	1305906550, 1155237496, 1607244650, 1455525988, 
	1776460110, 1626319424, 2079897426, 1928707164, 
	96392454, 213114376, 396673818, 514443284, 
	562755902, 679998000, 865136418, 983426092, 
	-586793578, -737462632, -820237430, -971956092, 
	-114159186, -264299872, -349698126, -500888388, 
	-1787927066, -1671205144, -2022411270, -1904641804, 
	-1319482914, -1202240816, -1556062270, -1437772596, 
	-321194175, -438830001, -20913827, -137500077, 
	-923870343, -1042034569, -621490843, -738605461, 
	-1531793615, -1379949505, -1230456531, -1079659997, 
	-2138668279, -1987344377, -1835231979, -1684955621, 
	2081048481, 1963412655, 1846563261, 1729977011, 
	1480485785, 1362321559, 1243905413, 1126790795, 
	878845905, 1030690015, 645401037, 796197571, 
	274084841, 425408743, 38544885, 188821243, 
	-681472870, -563312748, -981755258, -864644728, 
	-212492126, -94852180, -514869570, -398279248, 
	-1626745622, -1778065436, -1928084746, -2078357000, 
	-1153566510, -1305414692, -1457000754, -1607801408, 
	1202797690, 1320957812, 1437280870, 1554391400, 
	1669664834, 1787304780, 1906247262, 2022837584, 
	265905162, 114585348, 499347990, 349075736, 
	736970802, 585122620, 972512814, 821712160, 
	-1699282452, -1816524062, -2001922064, -2120213250, 
	-1098699308, -1215420710, -1399243832, -1517014842, 
	-757114468, -606973294, -1060810880, -909622130, 
	-152341084, -1671510, -453942344, -302225226, 
	174567692, 57326082, 410887952, 292596766, 
	777231668, 660510266, 1011452712, 893681702, 
	1108339068, 1258480242, 1343618912, 1494807662, 
	1715193156, 1865862730, 1948373848, 2100090966, 
	-1593017801, -1476300487, -1290376149, -1172609243, 
	-2059905521, -1942659839, -1759363053, -1641067747, 
	-379313593, -529979063, -75615141, -227328171, 
	-850391425, -1000536719, -548792221, -699985043, 
	836553431, 953270745, 600235211, 718002117, 
	367585007, 484830689, 133361907, 251657213, 
	2041877159, 1891211689, 1806599355, 1654886325, 
	1568718495, 1418573201, 1335535747, 1184342925
};

const char CRijndael::sm_rcon[30] =
{
	1, 2, 4, 8, 16, 32, 
	64, -128, 27, 54, 108, -40, 
	-85, 77, -102, 47, 94, -68, 
	99, -58, -105, 53, 106, -44, 
	-77, 125, -6, -17, -59, -111
};

const int CRijndael::sm_shifts[3][4][2] =
{
	{ {0, 0}, {1, 3}, {2, 2}, {3, 1} },
	{ {0, 0}, {1, 5}, {2, 4}, {3, 3} },
	{ {0, 0}, {1, 7}, {3, 5}, {4, 4} }
};

//Error Messages
char const* CRijndael::sm_szErrorMsg1 = "Object not Initialized";
char const* CRijndael::sm_szErrorMsg2 = "Data not multiple of Block Size";

//Null chain
char const* CRijndael::sm_chain0 = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

//CONSTRUCTOR
CRijndael::CRijndael() : m_bKeyInit(false)
{
}

//DESTRUCTOR
CRijndael::~CRijndael()
{
}

//Expand a user-supplied key material into a session key.
// key        - The 128/192/256-bit user-key to use.
// chain      - initial chain block for CBC and CFB modes.
// keylength  - 16, 24 or 32 bytes
// blockSize  - The block size in bytes of this Rijndael (16, 24 or 32 bytes).
void CRijndael::MakeKey(char const* key, char const* chain, int keylength, int blockSize)
{
	if(NULL == key)
		throw exception("Empty key");
	if(!(16==keylength || 24==keylength || 32==keylength))
		throw exception("Incorrect key length");
	if(!(16==blockSize || 24==blockSize || 32==blockSize))
		throw exception("Incorrect block length");
	m_keylength = keylength;
	m_blockSize = blockSize;
	//Initialize the chain
	memcpy(m_chain0, chain, m_blockSize);
	memcpy(m_chain, chain, m_blockSize);
	//Calculate Number of Rounds
	switch(m_keylength)
	{
		case 16:
			m_iROUNDS = (m_blockSize == 16) ? 10 : (m_blockSize == 24 ? 12 : 14);
			break;

		case 24:
			m_iROUNDS = (m_blockSize != 32) ? 12 : 14;
			break;

		default: // 32 bytes = 256 bits
			m_iROUNDS = 14;
	}
	int BC = m_blockSize / 4;
	int i, j;
	for(i=0; i<=m_iROUNDS; i++)
	{
		for(j=0; j<BC; j++)
			m_Ke[i][j] = 0;
	}
	for(i=0; i<=m_iROUNDS; i++)
	{
		for(j=0; j<BC; j++)
			m_Kd[i][j] = 0;
	}
	int ROUND_KEY_COUNT = (m_iROUNDS + 1) * BC;
	int KC = m_keylength/4;
	//Copy user material bytes into temporary ints
	int* pi = tk;
	char const* pc = key;
	for(i=0; i<KC; i++)
	{
		*pi = (unsigned char)*(pc++) << 24;
		*pi |= (unsigned char)*(pc++) << 16;
		*pi |= (unsigned char)*(pc++) << 8;
		*(pi++) |= (unsigned char)*(pc++);
	}
	//Copy values into round key arrays
	int t = 0;
	for(j=0; (j<KC)&&(t<ROUND_KEY_COUNT); j++,t++)
	{
		m_Ke[t/BC][t%BC] = tk[j];
		m_Kd[m_iROUNDS - (t/BC)][t%BC] = tk[j];
	}
	int tt, rconpointer = 0;
	while(t < ROUND_KEY_COUNT)
	{
		//Extrapolate using phi (the round key evolution function)
		tt = tk[KC-1];
		tk[0] ^= (sm_S[(tt >> 16) & 0xFF] & 0xFF) << 24 ^
			(sm_S[(tt >>  8) & 0xFF] & 0xFF) << 16 ^
			(sm_S[ tt & 0xFF] & 0xFF) <<  8 ^
			(sm_S[(tt >> 24) & 0xFF] & 0xFF) ^
			(sm_rcon[rconpointer++]  & 0xFF) << 24;
		if(KC != 8)
			for(i=1, j=0; i<KC;)
				tk[i++] ^= tk[j++];
		else
		{
			for(i=1, j=0; i<KC/2; )
				tk[i++] ^= tk[j++];
			tt = tk[KC/2-1];
			tk[KC/2] ^= (sm_S[ tt & 0xFF] & 0xFF) ^
				(sm_S[(tt >>  8) & 0xFF] & 0xFF) <<  8 ^
				(sm_S[(tt >> 16) & 0xFF] & 0xFF) << 16 ^
				(sm_S[(tt >> 24) & 0xFF] & 0xFF) << 24;
			for(j = KC/2, i=j+1; i<KC; )
				tk[i++] ^= tk[j++];
		}
		//Copy values into round key arrays
		for(j=0; (j<KC) && (t<ROUND_KEY_COUNT); j++, t++)
		{
			m_Ke[t/BC][t%BC] = tk[j];
			m_Kd[m_iROUNDS - (t/BC)][t%BC] = tk[j];
		}
	}
	//Inverse MixColumn where needed
	for(int r=1; r<m_iROUNDS; r++)
		for(j=0; j<BC; j++)
		{
			tt = m_Kd[r][j];
			m_Kd[r][j] = sm_U1[(tt >> 24) & 0xFF] ^
				sm_U2[(tt >> 16) & 0xFF] ^

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -