58 static constexpr uint32_t
K[64] = {
130 inline uint32_t
rotr(uint32_t x, uint32_t n) {
131 return (x >> n) | (x << (32 - n));
141 inline uint32_t
ch(uint32_t x, uint32_t y, uint32_t z) {
142 return (x & y) ^ (~x & z);
152 inline uint32_t
maj(uint32_t x, uint32_t y, uint32_t z) {
153 return (x & y) ^ (x & z) ^ (y & z);
180 return rotr(x, 7) ^
rotr(x, 18) ^ (x >> 3);
189 return rotr(x, 17) ^
rotr(x, 19) ^ (x >> 10);
197 inline void transform(uint32_t *state,
const uint8_t *data) {
199 uint32_t
A,
B,
C,
D,
E, F, G, H, T1, T2;
200 for (
int i = 0; i < 16; i++)
201 W[i] = (data[i * 4] << 24) | (data[i * 4 + 1] << 16)
202 | (data[i * 4 + 2] << 8) | data[i * 4 + 3];
203 for (
int i = 16; i < 64; i++)
214 for (
int i = 0; i < 64; i++) {
244 uint32_t state[8] = {
253 std::vector<uint8_t> data(
input.begin(),
input.end());
254 uint64_t bit_len = data.size() * 8;
255 data.push_back(0x80);
256 while ((data.size() % 64) != 56) data.push_back(0x00);
257 for (
int i = 7; i >= 0; i--)
258 data.push_back((bit_len >> (i * 8)) & 0xFF);
259 for (
size_t i = 0; i < data.size(); i += 64)
260 transform(state, &data[i]);
261 std::stringstream ss;
262 for (
int i = 0; i < 8; i++)
263 ss << std::hex << std::setfill(
'0') << std::setw(8) << state[i];
274 std::ifstream file(filename, std::ios::binary);
275 if (!file.is_open())
return "Cannot open file: " + filename;
277 uint32_t state[8] = {
286 std::vector<uint8_t> buffer(64);
287 uint64_t total_bits = 0;
288 while (file.read(
reinterpret_cast<char *
>(buffer.data()), 64)) {
289 transform(state, buffer.data());
292 std::streamsize last_size = file.gcount();
293 total_bits += last_size * 8;
294 std::vector<uint8_t> last_block(
295 buffer.begin(), buffer.begin() + last_size);
296 last_block.push_back(0x80);
297 while ((last_block.size() % 64) != 56) last_block.push_back(0x00);
298 for (
int i = 7; i >= 0; i--)
299 last_block.push_back((total_bits >> (i * 8)) & 0xFF);
300 for (
size_t i = 0; i < last_block.size(); i += 64)
301 transform(state, &last_block[i]);
302 std::stringstream ss;
303 for (
int i = 0; i < 8; i++)
304 ss << std::hex << std::setfill(
'0') << std::setw(8) << state[i];
317 static constexpr uint32_t
S[64] = {
386 static constexpr uint32_t
K[64] = {
459 inline uint32_t
F(uint32_t x, uint32_t y, uint32_t z) {
460 return (x & y) | (~x & z);
470 inline uint32_t
G(uint32_t x, uint32_t y, uint32_t z) {
471 return (x & z) | (y & ~z);
481 inline uint32_t
H(uint32_t x, uint32_t y, uint32_t z) {
492 inline uint32_t
I(uint32_t x, uint32_t y, uint32_t z) {
503 return (x << n) | (x >> (32 - n));
511 inline void transform(uint32_t *state,
const uint8_t *block) {
512 uint32_t a = state[0], b = state[1], c = state[2], d = state[3];
514 for (
int i = 0; i < 16; i++) {
515 M[i] = block[i * 4] | (block[i * 4 + 1] << 8)
516 | (block[i * 4 + 2] << 16)
517 | (block[i * 4 + 3] << 24);
519 for (
int i = 0; i < 64; i++) {
526 g = (5 * i + 1) % 16;
529 g = (3 * i + 5) % 16;
557 = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476};
558 std::vector<uint8_t> data(
input.begin(),
input.end());
559 uint64_t bit_len = data.size() * 8;
560 data.push_back(0x80);
561 while ((data.size() % 64) != 56) data.push_back(0x00);
562 for (
int i = 0; i < 8; i++)
563 data.push_back((bit_len >> (i * 8)) & 0xFF);
564 for (
size_t i = 0; i < data.size(); i += 64)
565 transform(state, &data[i]);
566 std::stringstream ss;
567 for (
int i = 0; i < 4; i++)
568 for (
int j = 0; j < 4; j++)
569 ss << std::hex << std::setfill(
'0') << std::setw(2)
570 << ((state[i] >> (j * 8)) & 0xFF);
581 const char *base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm"
582 "nopqrstuvwxyz0123456789+/";
584 result.reserve(((
input.size() + 2) / 3) * 4);
586 int bits_collected = 0;
587 for (
char c :
input) {
588 buffer = (buffer << 8) | static_cast<uint8_t>(c);
590 while (bits_collected >= 6) {
593 base64_chars[(buffer >> bits_collected) & 0x3F]);
596 if (bits_collected > 0) {
597 buffer <<= (6 - bits_collected);
598 result.push_back(base64_chars[buffer & 0x3F]);
600 while (result.size() % 4) result.push_back(
'=');
611 static const std::string base64_chars
612 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
613 "abcdefghijklmnopqrstuvwxyz"
616 result.reserve(
input.size() / 4 * 3);
618 int bits_collected = 0;
619 for (
char c :
input) {
621 size_t index = base64_chars.find(c);
622 if (index == std::string::npos)
continue;
623 buffer = (buffer << 6) | index;
625 if (bits_collected >= 8) {
627 result.push_back((buffer >> bits_collected) & 0xFF);
642 static constexpr uint8_t
Sbox[256] = {
1166 for (
int i = 0; i < 16; i++) state[i] =
Sbox[state[i]];
1174 for (
int i = 0; i < 16; i++) state[i] =
InvSbox[state[i]];
1184 state[1] = state[5];
1185 state[5] = state[9];
1186 state[9] = state[13];
1189 state[2] = state[10];
1192 state[6] = state[14];
1195 state[3] = state[15];
1196 state[15] = state[11];
1197 state[11] = state[7];
1208 state[13] = state[9];
1209 state[9] = state[5];
1210 state[5] = state[1];
1213 state[10] = state[2];
1216 state[14] = state[6];
1219 state[7] = state[11];
1220 state[11] = state[15];
1221 state[15] = state[3];
1231 inline uint8_t
gmul(uint8_t a, uint8_t b) {
1233 for (
int i = 0; i < 8; i++) {
1235 bool high = a & 0x80;
1237 if (high) a ^= 0x1b;
1249 for (
int i = 0; i < 4; i++) {
1251 temp[0] =
gmul(state[idx], 2) ^
gmul(state[idx + 1], 3)
1252 ^ state[idx + 2] ^ state[idx + 3];
1253 temp[1] = state[idx] ^
gmul(state[idx + 1], 2)
1254 ^
gmul(state[idx + 2], 3) ^ state[idx + 3];
1255 temp[2] = state[idx] ^ state[idx + 1]
1256 ^
gmul(state[idx + 2], 2)
1257 ^
gmul(state[idx + 3], 3);
1258 temp[3] =
gmul(state[idx], 3) ^ state[idx + 1]
1259 ^ state[idx + 2] ^
gmul(state[idx + 3], 2);
1260 std::memcpy(&state[idx], temp, 4);
1270 for (
int i = 0; i < 4; i++) {
1272 temp[0] =
gmul(state[idx], 0x0e)
1273 ^
gmul(state[idx + 1], 0x0b)
1274 ^
gmul(state[idx + 2], 0x0d)
1275 ^
gmul(state[idx + 3], 0x09);
1276 temp[1] =
gmul(state[idx], 0x09)
1277 ^
gmul(state[idx + 1], 0x0e)
1278 ^
gmul(state[idx + 2], 0x0b)
1279 ^
gmul(state[idx + 3], 0x0d);
1280 temp[2] =
gmul(state[idx], 0x0d)
1281 ^
gmul(state[idx + 1], 0x09)
1282 ^
gmul(state[idx + 2], 0x0e)
1283 ^
gmul(state[idx + 3], 0x0b);
1284 temp[3] =
gmul(state[idx], 0x0b)
1285 ^
gmul(state[idx + 1], 0x0d)
1286 ^
gmul(state[idx + 2], 0x09)
1287 ^
gmul(state[idx + 3], 0x0e);
1288 std::memcpy(&state[idx], temp, 4);
1298 for (
int i = 0; i < 16; i++) state[i] ^= key[i];
1310 for (
int i = 0; i < 16; i++) round_keys[i] = key[i];
1311 for (
int i = 4; i < 44; i++) {
1312 std::memcpy(temp, &round_keys[(i - 1) * 4], 4);
1314 uint8_t t = temp[0];
1315 temp[0] =
Sbox[temp[1]];
1316 temp[1] =
Sbox[temp[2]];
1317 temp[2] =
Sbox[temp[3]];
1319 rcon[0] = 0x01 << ((i / 4) - 1);
1320 rcon[1] = rcon[2] = rcon[3] = 0;
1321 if (rcon[0] == 0) rcon[0] = 0x1b;
1322 for (
int j = 0; j < 4; j++) temp[j] ^= rcon[j];
1324 for (
int j = 0; j < 4; j++) {
1325 round_keys[i * 4 + j]
1326 = round_keys[(i - 4) * 4 + j] ^ temp[j];
1339 const uint8_t *round_keys) {
1341 std::memcpy(state,
input, 16);
1352 std::memcpy(output, state, 16);
1363 const uint8_t *round_keys) {
1365 std::memcpy(state,
input, 16);
1376 std::memcpy(output, state, 16);
1385 inline std::string
pad_key(
const std::string &key) {
1386 std::string padded = key;
1387 padded.resize(16, 0);
1397 static std::random_device rd;
1398 static std::mt19937
gen(
1399 std::chrono::steady_clock::now().time_since_epoch().count()
1401 static std::uniform_int_distribution<> dis(0, 255);
1404 for (
int i = 0; i < 16; i++)
1405 bytes.push_back(
static_cast<char>(dis(
gen)));
1424 uint8_t round_keys[44 * 4];
1426 reinterpret_cast<const uint8_t *
>(padded_key.c_str()),
1430 result.reserve(plaintext.size() + 16);
1432 uint8_t counter[16];
1433 std::memcpy(counter, iv.c_str(), 16);
1434 for (
size_t i = 0; i < plaintext.size(); i += 16) {
1435 uint8_t keystream[16];
1437 for (
int j = 0; j < 16 && i + j < plaintext.size(); j++)
1438 result.push_back(plaintext[i + j] ^ keystream[j]);
1439 for (
int j = 15; j >= 0; j--) {
1440 if (++counter[j] != 0)
break;
1457 if (decoded.size() < 16)
return "Invalid ciphertext";
1459 uint8_t round_keys[44 * 4];
1461 reinterpret_cast<const uint8_t *
>(padded_key.c_str()),
1463 std::string iv = decoded.substr(0, 16);
1464 std::string encrypted_data = decoded.substr(16);
1466 result.reserve(encrypted_data.size());
1467 uint8_t counter[16];
1468 std::memcpy(counter, iv.c_str(), 16);
1469 for (
size_t i = 0; i < encrypted_data.size(); i += 16) {
1470 uint8_t keystream[16];
1472 for (
int j = 0; j < 16 && i + j < encrypted_data.size(); j++)
1473 result.push_back(encrypted_data[i + j] ^ keystream[j]);
1474 for (
int j = 15; j >= 0; j--)
1475 if (++counter[j] != 0)
break;
AES加密算法的内部实现细节。
Definition crypto.h:638
void add_round_key(uint8_t *state, const uint8_t *key)
轮密钥加操作,将状态矩阵与轮密钥进行异或。
Definition crypto.h:1297
void mix_columns(uint8_t *state)
列混合操作,对状态矩阵的每一列进行线性变换。
Definition crypto.h:1247
uint8_t gmul(uint8_t a, uint8_t b)
在GF(2^8)有限域中执行乘法运算。
Definition crypto.h:1231
static constexpr uint8_t InvSbox[256]
AES算法的逆S盒,用于解密时的字节替换。
Definition crypto.h:903
std::string random_bytes()
生成随机字节序列。
Definition crypto.h:1396
static constexpr uint8_t Sbox[256]
AES算法的S盒,用于字节替换。
Definition crypto.h:642
void shift_rows(uint8_t *state)
行移位操作,将状态矩阵的各行循环左移不同偏移量。
Definition crypto.h:1181
void key_expansion(const uint8_t *key, uint8_t *round_keys)
AES密钥扩展算法,将原始密钥扩展为11轮使用的轮密钥。
Definition crypto.h:1307
void inv_sub_bytes(uint8_t *state)
逆字节替换操作,使用逆S盒替换状态矩阵中的每个字节。
Definition crypto.h:1173
std::string pad_key(const std::string &key)
将密钥填充到16字节(AES-128要求的密钥长度)。
Definition crypto.h:1385
void aes_encrypt_block(const uint8_t *input, uint8_t *output, const uint8_t *round_keys)
AES加密单个块(128位)。
Definition crypto.h:1337
void aes_decrypt_block(const uint8_t *input, uint8_t *output, const uint8_t *round_keys)
AES解密单个块(128位)。
Definition crypto.h:1361
void inv_shift_rows(uint8_t *state)
逆行移位操作,将状态矩阵的各行循环右移不同偏移量。
Definition crypto.h:1205
void sub_bytes(uint8_t *state)
字节替换操作,使用S盒替换状态矩阵中的每个字节。
Definition crypto.h:1165
void inv_mix_columns(uint8_t *state)
逆列混合操作,对状态矩阵的每一列进行逆线性变换。
Definition crypto.h:1268
MD5哈希算法的内部实现细节。
Definition crypto.h:313
uint32_t rotate_left(uint32_t x, uint32_t n)
对32位无符号整数进行循环左移。
Definition crypto.h:502
uint32_t I(uint32_t x, uint32_t y, uint32_t z)
MD5第四轮逻辑函数I。
Definition crypto.h:492
static constexpr uint32_t S[64]
MD5算法中每轮循环使用的左移位数数组。
Definition crypto.h:317
uint32_t F(uint32_t x, uint32_t y, uint32_t z)
MD5第一轮逻辑函数F。
Definition crypto.h:459
void transform(uint32_t *state, const uint8_t *block)
对64字节的数据块执行MD5变换。
Definition crypto.h:511
static constexpr uint32_t K[64]
MD5算法中使用的常量K数组。
Definition crypto.h:386
uint32_t G(uint32_t x, uint32_t y, uint32_t z)
MD5第二轮逻辑函数G。
Definition crypto.h:470
uint32_t H(uint32_t x, uint32_t y, uint32_t z)
MD5第三轮逻辑函数H。
Definition crypto.h:481
SHA256哈希算法的内部实现细节。
Definition crypto.h:54
uint32_t gamma1(uint32_t x)
SHA256中的小西格玛1函数。
Definition crypto.h:188
uint32_t sigma1(uint32_t x)
SHA256中的大西格玛1函数。
Definition crypto.h:170
uint32_t rotr(uint32_t x, uint32_t n)
对32位无符号整数进行循环右移。
Definition crypto.h:130
void transform(uint32_t *state, const uint8_t *data)
对64字节的数据块执行SHA256变换。
Definition crypto.h:197
uint32_t maj(uint32_t x, uint32_t y, uint32_t z)
SHA256中的多数函数Maj。
Definition crypto.h:152
uint32_t ch(uint32_t x, uint32_t y, uint32_t z)
SHA256中的选择函数Ch。
Definition crypto.h:141
uint32_t sigma0(uint32_t x)
SHA256中的大西格玛0函数。
Definition crypto.h:161
uint32_t gamma0(uint32_t x)
SHA256中的小西格玛0函数。
Definition crypto.h:179
static constexpr uint32_t K[64]
SHA256算法使用的常量K数组。
Definition crypto.h:58
密码学相关的命名空间。
Definition crypto.h:49
std::string sha256(const std::string &input)
计算输入字符串的SHA256哈希值。
Definition crypto.h:242
std::string aes_decrypt(const std::string &ciphertext, const std::string &key)
使用AES-128-CTR模式解密密文。
Definition crypto.h:1455
std::string file_sha256(const std::string &filename)
计算文件的SHA256哈希值。
Definition crypto.h:273
std::string md5(const std::string &input)
计算输入字符串的MD5哈希值。
Definition crypto.h:554
std::string base64_encode(const std::string &input)
对输入字符串进行Base64编码。
Definition crypto.h:580
std::string aes_encrypt(const std::string &plaintext, const std::string &key)
使用AES-128-CTR模式加密明文。
Definition crypto.h:1422
std::string base64_decode(const std::string &input)
对Base64编码的字符串进行解码。
Definition crypto.h:610
T input(const CharT *prompt="", const BasicInputSettings< CharT > &is=input_settings)
从标准输入读取一个值,支持类型模板。
Definition input.h:75