C++使用AES+Base64算法对文本进行加密


使用AES算法和Base64实现具体的文本加密。

采用AES算法的CBC模式,16位秘钥,PKCS7Padding填充方案对文本进行加解密。此方案可防止一些比较关键的文本暴露,也可以用于服务器通信间的加密,防止通信协议和信息的泄露。

Main.cpp

#include <iostream>
#include "AES.h"
#include "Base64.h"
using namespace std;

const char g_key[17] = "asdfwetyhjuytrfd";
const char g_iv[17] = "gfdertfghjkuyrtg";
string EncryptionAES(const string& strSrc) //AES加密
{
	size_t length = strSrc.length();
	int block_num = length / BLOCK_SIZE + 1;
	//明文
	char* szDataIn = new char[block_num * BLOCK_SIZE + 1];
	memset(szDataIn, 0x00, block_num * BLOCK_SIZE + 1);
	strcpy(szDataIn, strSrc.c_str());

	//进行PKCS7Padding填充。
	int k = length % BLOCK_SIZE;
	int j = length / BLOCK_SIZE;
	int padding = BLOCK_SIZE - k;
	for (int i = 0; i < padding; i++)
	{
		szDataIn[j * BLOCK_SIZE + k + i] = padding;
	}
	szDataIn[block_num * BLOCK_SIZE] = '\0';

	//加密后的密文
	char *szDataOut = new char[block_num * BLOCK_SIZE + 1];
	memset(szDataOut, 0, block_num * BLOCK_SIZE + 1);

	//进行进行AES的CBC模式加密
	AES aes;
	aes.MakeKey(g_key, g_iv, 16, 16);
	aes.Encrypt(szDataIn, szDataOut, block_num * BLOCK_SIZE, AES::CBC);
	string str = base64_encode((unsigned char*) szDataOut,
			block_num * BLOCK_SIZE);
	delete[] szDataIn;
	delete[] szDataOut;
	return str;
}
string DecryptionAES(const string& strSrc) //AES解密
{
	string strData = base64_decode(strSrc);
	size_t length = strData.length();
	//密文
	char *szDataIn = new char[length + 1];
	memcpy(szDataIn, strData.c_str(), length+1);
	//明文
	char *szDataOut = new char[length + 1];
	memcpy(szDataOut, strData.c_str(), length+1);

	//进行AES的CBC模式解密
	AES aes;
	aes.MakeKey(g_key, g_iv, 16, 16);
	aes.Decrypt(szDataIn, szDataOut, length, AES::CBC);

	//去PKCS7Padding填充
	if (0x00 < szDataOut[length - 1] <= 0x16)
	{
		int tmp = szDataOut[length - 1];
		for (int i = length - 1; i >= length - tmp; i--)
		{
			if (szDataOut[i] != tmp)
			{
				memset(szDataOut, 0, length);
				cout << "去填充失败!解密出错!!" << endl;
				break;
			}
			else
				szDataOut[i] = 0;
		}
	}
	string strDest(szDataOut);
	delete[] szDataIn;
	delete[] szDataOut;
	return strDest;
}
int main(int argc, char **argv)
{
	string str1 = "qwertyuisfdlsajdxcvnkhsakfh1332487";
	cout << "加密前:" << str1 << endl;
	string str2 = EncryptionAES(str1);
	cout << "加密后:" << str2 << endl;
	string str3 = DecryptionAES(str2);
	cout << "解密后:" << str3 << endl;
}



AES算法的C++实现

AES.h

//AES.h

#ifndef _AES_H
#define _AES_H
#include <exception>
#include <cstring>
#include <string>
#define BLOCK_SIZE 16
using namespace std;

class AES
{
public:
	enum
	{
		ECB = 0, CBC = 1, CFB = 2
	};

private:
	enum
	{
		DEFAULT_BLOCK_SIZE = 16
	};
	enum
	{
		MAX_BLOCK_SIZE = 32, MAX_ROUNDS = 14, MAX_KC = 8, MAX_BC = 8
	};
public:
	AES();
	virtual ~AES();
private:
	//Key Initialization Flag
	bool m_bKeyInit;
	//Encryption (m_Ke) round key
	int m_Ke[MAX_ROUNDS + 1][MAX_BC];
	//Decryption (m_Kd) round key
	int m_Kd[MAX_ROUNDS + 1][MAX_BC];
	//Key Length
	int m_keylength;
	//Block Size
	int m_blockSize;
	//Number of Rounds
	int m_iROUNDS;
	//Chain Block
	char m_chain0[MAX_BLOCK_SIZE];
	char m_chain[MAX_BLOCK_SIZE];
	//Auxiliary private use buffers
	int tk[MAX_KC];
	int a[MAX_BC];
	int t[MAX_BC];
private:
	void Xor(char* buff, char const* chain);
	void DefEncryptBlock(char const* in, char* result);
	void DefDecryptBlock(char const* in, char* result);
	void EncryptBlock(char const* in, char* result);
	void DecryptBlock(char const* in, char* result);
public:
	void MakeKey(char const* key, char const* chain, int keylength =
			DEFAULT_BLOCK_SIZE, int blockSize = DEFAULT_BLOCK_SIZE);
	void Encrypt(char const* in, char* result, size_t n, int iMode = ECB);
	void Decrypt(char const* in, char* result, size_t n, int iMode = ECB);
};

#endif // __RIJNDAEL_H__

AES.cpp

#include <cstring>
#include <exception>
#include "AES.h"
#include <iostream>
using namespace std;
const int sm_alog[256] =
{ 1, 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53, 95, 225, 56,
		72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170, 229, 52, 92,
		228, 55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49, 83, 245, 4,
		12, 20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178, 205, 76, 212,
		103, 169, 224, 59, 77, 215, 98, 166, 241, 8, 24, 40, 120, 136, 131, 158,
		185, 208, 107, 189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154,
		181, 196, 87, 249, 16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163,
		254, 25, 43, 125, 135, 146, 173, 236, 47, 113, 147, 174, 233, 32, 96,
		160, 251, 22, 58, 78, 210, 109, 183, 194, 93, 231, 50, 86, 250, 21, 63,
		65, 195, 94, 226, 61, 71, 201, 64, 192, 91, 237, 44, 116, 156, 191, 218,
		117, 159, 186, 213, 100, 172, 239, 42, 126, 130, 157, 188, 223, 122,
		142, 137, 128, 155, 182, 193, 88, 232, 35, 101, 175, 234, 37, 111, 177,
		200, 67, 197, 84, 252, 31, 33, 99, 165, 244, 7, 9, 27, 45, 119, 153,
		176, 203, 70, 202, 69, 207, 74, 222, 121, 139, 134, 145, 168, 227, 62,
		66, 198, 81, 243, 14, 18, 54, 90, 238, 41, 123, 141, 140, 143, 138, 133,
		148, 167, 242, 13, 23, 57, 75, 221, 124, 132, 151, 162, 253, 28, 36,
		108, 180, 199, 82, 246, 1 };

const int sm_log[256] =
{ 0, 0, 25, 1, 50, 2, 26, 198, 75, 199, 27, 104, 51, 238, 223, 3, 100, 4, 224,
		14, 52, 141, 129, 239, 76, 113, 8, 200, 248, 105, 28, 193, 125, 194, 29,
		181, 249, 185, 39, 106, 77, 228, 166, 114, 154, 201, 9, 120, 101, 47,
		138, 5, 33, 15, 225, 36, 18, 240, 130, 69, 53, 147, 218, 142, 150, 143,
		219, 189, 54, 208, 206, 148, 19, 92, 210, 241, 64, 70, 131, 56, 102,
		221, 253, 48, 191, 6, 139, 98, 179, 37, 226, 152, 34, 136, 145, 16, 126,
		110, 72, 195, 163, 182, 30, 66, 58, 107, 40, 84, 250, 133, 61, 186, 43,
		121, 10, 21, 155, 159, 94, 202, 78, 212, 172, 229, 243, 115, 167, 87,
		175, 88, 168, 80, 244, 234, 214, 116, 79, 174, 233, 213, 231, 230, 173,
		232, 44, 215, 117, 122, 235, 22, 11, 245, 89, 203, 95, 176, 156, 169,
		81, 160, 127, 12, 246, 111, 23, 196, 73, 236, 216, 67, 31, 45, 164, 118,
		123, 183, 204, 187, 62, 90, 251, 96, 177, 134, 59, 82, 161, 108, 170,
		85, 41, 157, 151, 178, 135, 144, 97, 190, 220, 252, 188, 149, 207, 205,
		55, 63, 91, 209, 83, 57, 132, 60, 65, 162, 109, 71, 20, 42, 158, 93, 86,
		242, 211, 171, 68, 17, 146, 217, 35, 32, 46, 137, 180, 124, 184, 38,
		119, 153, 227, 165, 103, 74, 237, 222, 197, 49, 254, 24, 13, 99, 140,
		128, 192, 247, 112, 7 };

const char sm_S[256] =
{ 99, 124, 119, 123, -14, 107, 111, -59, 48, 1, 103, 43, -2, -41, -85, 118, -54,
		-126, -55, 125, -6, 89, 71, -16, -83, -44, -94, -81, -100, -92, 114,
		-64, -73, -3, -109, 38, 54, 63, -9, -52, 52, -91, -27, -15, 113, -40,
		49, 21, 4, -57, 35, -61, 24, -106, 5, -102, 7, 18, -128, -30, -21, 39,
		-78, 117, 9, -125, 44, 26, 27, 110, 90, -96, 82, 59, -42, -77, 41, -29,
		47, -124, 83, -47, 0, -19, 32, -4, -79, 91, 106, -53, -66, 57, 74, 76,
		88, -49, -48, -17, -86, -5, 67, 77, 51, -123, 69, -7, 2, 127, 80, 60,
		-97, -88, 81, -93, 64, -113, -110, -99, 56, -11, -68, -74, -38, 33, 16,
		-1, -13, -46, -51, 12, 19, -20, 95, -105, 68, 23, -60, -89, 126, 61,
		100, 93, 25, 115, 96, -127, 79, -36, 34, 42, -112, -120, 70, -18, -72,
		20, -34, 94, 11, -37, -32, 50, 58, 10, 73, 6, 36, 92, -62, -45, -84, 98,
		-111, -107, -28, 121, -25, -56, 55, 109, -115, -43, 78, -87, 108, 86,
		-12, -22, 101, 122, -82, 8, -70, 120, 37, 46, 28, -90, -76, -58, -24,
		-35, 116, 31, 75, -67, -117, -118, 112, 62, -75, 102, 72, 3, -10, 14,
		97, 53, 87, -71, -122, -63, 29, -98, -31, -8, -104, 17, 105, -39, -114,
		-108, -101, 30, -121, -23, -50, 85, 40, -33, -116, -95, -119, 13, -65,
		-26, 66, 104, 65, -103, 45, 15, -80, 84, -69, 22 };

const char sm_Si[256] =
{ 82, 9, 106, -43, 48, 54, -91, 56, -65, 64, -93, -98, -127, -13, -41, -5, 124,
		-29, 57, -126, -101, 47, -1, -121, 52, -114, 67, 68, -60, -34, -23, -53,
		84, 123, -108, 50, -90, -62, 35, 61, -18, 76, -107, 11, 66, -6, -61, 78,
		8, 46, -95, 102, 40, -39, 36, -78, 118, 91, -94, 73, 109, -117, -47, 37,
		114, -8, -10, 100, -122, 104, -104, 22, -44, -92, 92, -52, 93, 101, -74,
		-110, 108, 112, 72, 80, -3, -19, -71, -38, 94, 21, 70, 87, -89, -115,
		-99, -124, -112, -40, -85, 0, -116, -68, -45, 10, -9, -28, 88, 5, -72,
		-77, 69, 6, -48, 44, 30, -113, -54, 63, 15, 2, -63, -81, -67, 3, 1, 19,
		-118, 107, 58, -111, 17, 65, 79, 103, -36, -22, -105, -14, -49, -50,
		-16, -76, -26, 115, -106, -84, 116, 34, -25, -83, 53, -123, -30, -7, 55,
		-24, 28, 117, -33, 110, 71, -15, 26, 113, 29, 41, -59, -119, 111, -73,
		98, 14, -86, 24, -66, 27, -4, 86, 62, 75, -58, -46, 121, 32, -102, -37,
		-64, -2, 120, -51, 90, -12, 31, -35, -88, 51, -120, 7, -57, 49, -79, 18,
		16, 89, 39, -128, -20, 95, 96, 81, 127, -87, 25, -75, 74, 13, 45, -27,
		122, -97, -109, -55, -100, -17, -96, -32, 59, 77, -82, 42, -11, -80,
		-56, -21, -69, 60, -125, 83, -103, 97, 23, 43, 4, 126, -70, 119, -42,
		38, -31, 105, 20, 99, 85, 33, 12, 125 };

const int sm_T1[256] =
{ -966564955, -126059388, -294160487, -159679603, -855539, -697603139,
		-563122255, -1849309868, 1613770832, 33620227, -832084055, 1445669757,
		-402719207, -1244145822, 1303096294, -327780710, -1882535355, 528646813,
		-1983264448, -92439161, -268764651, -1302767125, -1907931191, -68095989,
		1101901292, -1277897625, 1604494077, 1169141738, 597466303, 1403299063,
		-462261610, -1681866661, 1974974402, -503448292, 1033081774, 1277568618,
		1815492186, 2118074177, -168298750, -2083730353, 1748251740, 1369810420,
		-773462732, -101584632, -495881837, -1411852173, 1647391059, 706024767,
		134480908, -1782069422, 1176707941, -1648114850, 806885416, 932615841,
		168101135, 798661301, 235341577, 605164086, 461406363, -538779075,
		-840176858, 1311188841, 2142417613, -361400929, 302582043, 495158174,
		1479289972, 874125870, 907746093, -596742478, -1269146898, 1537253627,
		-1538108682, 1983593293, -1210657183, 2108928974, 1378429307,
		-572267714, 1580150641, 327451799, -1504488459, -1177431704, 0,
		-1041371860, 1075847264, -469959649, 2041688520, -1235526675,
		-731223362, -1916023994, 1740553945, 1916352843, -1807070498,
		-1739830060, -1336387352, -2049978550, -1143943061, -974131414,
		1336584933, -302253290, -2042412091, -1706209833, 1714631509, 293963156,
		-1975171633, -369493744, 67240454, -25198719, -1605349136, 2017213508,
		631218106, 1269344483, -1571728909, 1571005438, -2143272768, 93294474,
		1066570413, 563977660, 1882732616, -235539196, 1673313503, 2008463041,
		-1344611723, 1109467491, 537923632, -436207846, -34344178, -1076702611,
		-2117218996, 403442708, 638784309, -1007883217, -1101045791, 899127202,
		-2008791860, 773265209, -1815821225, 1437050866, -58818942, 2050833735,
		-932944724, -1168286233, 840505643, -428641387, -1067425632, 427917720,
		-1638969391, -1545806721, 1143087718, 1412049534, 999329963, 193497219,
		-1941551414, -940642775, 1807268051, 672404540, -1478566279,
		-1134666014, 369822493, -1378100362, -606019525, 1681011286, 1949973070,
		336202270, -1840690725, 201721354, 1210328172, -1201906460, -1614626211,
		-1110191250, 1135389935, -1000185178, 965841320, 831886756, -739974089,
		-226920053, -706222286, -1949775805, 1849112409, -630362697, 26054028,
		-1311386268, -1672589614, 1235855840, -663982924, -1403627782,
		-202050553, -806688219, -899324497, -193299826, 1202630377, 268961816,
		1874508501, -260540280, 1243948399, 1546530418, 941366308, 1470539505,
		1941222599, -1748580783, -873928669, -1579295364, -395021156,
		1042226977, -1773450275, 1639824860, 227249030, 260737669, -529502064,
		2084453954, 1907733956, -865704278, -1874310952, 100860677, -134810111,
		470683154, -1033805405, 1781871967, -1370007559, 1773779408, 394692241,
		-1715355304, 974986535, 664706745, -639508168, -336005101, 731420851,
		571543859, -764843589, -1445340816, 126783113, 865375399, 765172662,
		1008606754, 361203602, -907417312, -2016489911, -1437248001, 1344809080,
		-1512054918, 59542671, 1503764984, 160008576, 437062935, 1707065306,
		-672733647, -2076032314, -798463816, -2109652541, 697932208, 1512910199,
		504303377, 2075177163, -1470868228, 1841019862, 739644986 };

const int sm_T2[256] =
{ -1513725085, -2064089988, -1712425097, -1913226373, 234877682, -1110021269,
		-1310822545, 1418839493, 1348481072, 50462977, -1446090905, 2102799147,
		434634494, 1656084439, -431117397, -1695779210, 1167051466, -1658879358,
		1082771913, -2013627011, 368048890, -340633255, -913422521, 201060592,
		-331240019, 1739838676, -44064094, -364531793, -1088185188, -145513308,
		-1763413390, 1536934080, -1032472649, 484572669, -1371696237,
		1783375398, 1517041206, 1098792767, 49674231, 1334037708, 1550332980,
		-195975771, 886171109, 150598129, -1813876367, 1940642008, 1398944049,
		1059722517, 201851908, 1385547719, 1699095331, 1587397571, 674240536,
		-1590192490, 252314885, -1255171430, 151914247, 908333586, -1692696448,
		1038082786, 651029483, 1766729511, -847269198, -1612024459, 454166793,
		-1642232957, 1951935532, 775166490, 758520603, -1294176658, -290170278,
		-77881184, -157003182, 1299594043, 1639438038, -830622797, 2068982057,
		1054729187, 1901997871, -1760328572, -173649069, 1757008337, 0,
		750906861, 1
  • 10
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
Elgamal算法是一种非对称加密算法,其基于离散对数问题。该算法的安全性取决于大素数的难度和离散对数问题的难度。下面是使用C++实现Elgamal算法文本进行加密的示例代码,附有详细的注释: ```c++ #include <iostream> #include <cstdlib> #include <ctime> #include <cmath> #include <cstring> using namespace std; // 欧拉函数,计算 φ(p) int Euler(int p){ int ans = p; for (int i = 2; i <= sqrt(p); i++) { if (p % i == 0) { ans = ans / i * (i - 1); // 根据欧拉函数的定义,计算每个质因子的贡献 while (p % i == 0) p /= i; } } if (p > 1) ans = ans / p * (p - 1); // 处理最后一个质因子 return ans; } // 快速幂算法 int QuickPow(int a, int b, int p){ int ans = 1 % p; while (b) { if (b & 1) ans = (long long)ans * a % p; a = (long long)a * a % p; b >>= 1; } return ans; } // 扩展欧几里得算法,用于求解 ax + by = 1 的整数解 // 返回值为 gcd(a, b),x 和 y 的值通过指针返回 int ExGCD(int a, int b, int &x, int &y){ if (b == 0) { x = 1, y = 0; return a; } int gcd = ExGCD(b, a % b, y, x); y -= a / b * x; return gcd; } // Elgamal算法加密函数 void Elgamal_Encrypt(int p, int a, int k, int y, const char *plaintext, int *ciphertext){ // plaintext:明文,ciphertext:密文,p、a、k、y:公钥,k 为私钥 // 计算 φ(p) int phi = Euler(p); int len = strlen(plaintext); for (int i = 0; i < len; i++) { // 对每个字符进行加密 int m = plaintext[i]; // 随机选择一个整数 r int r = rand() % (p - 2) + 2; // r 属于 [2, p-1),即 r 取值范围为 2~p-2 // 计算 c1 和 c2 int c1 = QuickPow(a, r, p); int c2 = (long long)m * QuickPow(y, r, p) % p; ciphertext[i * 2] = c1; ciphertext[i * 2 + 1] = c2; } } // Elgamal算法解密函数 void Elgamal_Decrypt(int p, int a, int x, const int *ciphertext, int len, char *plaintext){ // ciphertext:密文,plaintext:明文,p、a、x:私钥 for (int i = 0; i < len; i++) { // 对每个密文块进行解密 int c1 = ciphertext[i * 2]; int c2 = ciphertext[i * 2 + 1]; // 计算 m = c2 * c1^(-x) mod p int invc1 = ExGCD(c1, p, invc1, invc1); invc1 = (invc1 % p + p) % p; int m = (long long)c2 * QuickPow(invc1, x, p) % p; plaintext[i] = m; } } int main(){ // 选择一个大素数 p 和原根 a int p = 9973, a = 5; // 计算 φ(p) 和随机选择一个整数 k 作为私钥 int phi = Euler(p), k = 233; // 计算公钥 y int y = QuickPow(a, k, p); // 显示公钥和私钥 cout << "公钥为 (" << p << ", " << a << ", " << y << ")" << endl; cout << "私钥为 " << k << endl; // 加密测试 const char *plaintext = "Hello, world!"; int len = strlen(plaintext); int *ciphertext = new int[len * 2]; Elgamal_Encrypt(p, a, k, y, plaintext, ciphertext); // 加密 cout << "加密后的密文为:"; for (int i = 0; i < len * 2; i++) { cout << ciphertext[i] << " "; } cout << endl; // 解密测试 char *decryptedtext = new char[len]; Elgamal_Decrypt(p, a, k, ciphertext, len, decryptedtext); // 解密 decryptedtext[len] = '\0'; cout << "解密后的明文为:" << decryptedtext << endl; return 0; } ``` 以上是使用C++实现Elgamal算法文本进行加密的示例代码。需要注意的是,该示例代码仅用于学习和研究目的,实际使用中还需要考虑到一些安全性问题,例如如何选择合适的参数、如何保护私钥等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值