36 lines
1.4 KiB
C++
36 lines
1.4 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include "../login.cpp" // 直接复用加密解密相关函数
|
|
|
|
int main()
|
|
{
|
|
std::string username = "test3";
|
|
std::string fixed_key = "XNSim_Access_Key";
|
|
std::vector<unsigned char> key = derive_aes_key(username, fixed_key);
|
|
|
|
for (int access_level = 0; access_level <= 4; ++access_level) {
|
|
// 加密
|
|
std::vector<unsigned char> iv;
|
|
std::vector<unsigned char> ciphertext = aes_encrypt(std::to_string(access_level), key, iv);
|
|
std::vector<unsigned char> iv_and_ciphertext = iv;
|
|
iv_and_ciphertext.insert(iv_and_ciphertext.end(), ciphertext.begin(), ciphertext.end());
|
|
std::string hexstr = bin2hex(iv_and_ciphertext);
|
|
|
|
// 解密
|
|
std::vector<unsigned char> iv_and_ciphertext2 = hex2bin(hexstr);
|
|
std::vector<unsigned char> iv2(iv_and_ciphertext2.begin(), iv_and_ciphertext2.begin() + 16);
|
|
std::vector<unsigned char> ciphertext2(iv_and_ciphertext2.begin() + 16,
|
|
iv_and_ciphertext2.end());
|
|
std::string decrypted = aes_decrypt(ciphertext2, key, iv2);
|
|
|
|
std::cout << "username: " << username << std::endl;
|
|
std::cout << "access_level: " << access_level << std::endl;
|
|
std::cout << "加密后16进制: " << hexstr << std::endl;
|
|
std::cout << "解密还原: " << decrypted << std::endl;
|
|
std::cout << "-----------------------------" << std::endl;
|
|
}
|
|
return 0;
|
|
} |