查看“Cryptopp AES示例”的源代码
←
Cryptopp AES示例
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
<syntaxhighlight lang="cpp"> #include <iostream> #include <string> #include "cryptlib.h" #include "aes.h" #include "modes.h" #include "files.h" #include "osrng.h" #include "hex.h" using namespace CryptoPP; using namespace std; int main(int argc, char* argv[]) { // 建立隨機亂數產生器 // AutoSeededRandomPool prng; // 將金鑰與 IV 放置於安全的記憶體空間 // SecByteBlock key(AES::DEFAULT_KEYLENGTH); // 採用預設的金鑰長度(128 位元) // SecByteBlock key(AES::MAX_KEYLENGTH); // 採用最長的金鑰長度(256 位元) // SecByteBlock key(24); // 自行指定金鑰長度(192 位元) // 產生隨機金鑰與 IV // prng.GenerateBlock(key, key.size()); byte key[AES::DEFAULT_KEYLENGTH] = "abcd1234"; // 原始資料 std::string plain = "Crypto++ is a free C++ library for cryptography"; // 用來儲存密文與明文的變數 std::string cipher, encoded, recovered; std::cout << "key:" << key << std::endl; std::cout << "原始資料:" << plain << std::endl; try { // 採用 AES-ECB 加密 ECB_Mode<AES>::Encryption e; // 設定金鑰 e.SetKey(key, sizeof(key)); // 進行加密 StringSource s(plain, true, new StreamTransformationFilter(e, new StringSink(cipher) ) // StreamTransformationFilter ); // StringSource } catch(const Exception& e) { std::cerr << e.what() << std::endl; return EXIT_FAILURE; } // Pretty print cipher text StringSource ss2( cipher, true, new HexEncoder( new StringSink( encoded ) ) // HexEncoder ); // StringSource std::cout << "cipher text: " << encoded << std::endl; try { // 採用 AES-ECB 解密 ECB_Mode<AES>::Decryption d; // 設定金鑰與 IV d.SetKey(key, sizeof(key)); // 進行解密 StringSource s(cipher, true, new StreamTransformationFilter(d, new StringSink(recovered) ) // StreamTransformationFilter ); // StringSource } catch(const Exception& e) { std::cerr << e.what() << std::endl; return EXIT_FAILURE; } std::cout << "解開的明文:" << recovered << std::endl; return EXIT_SUCCESS; } </syntaxhighlight>
返回至“
Cryptopp AES示例
”。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
基础知识
正则表达式
Markdown
分布式
项目管理
系统集成项目管理基础知识
云原生
Docker
云原生安全
云原生词汇表
十二因素应用
Kubernetes
音频处理
音频合成
Edge-tts
CMS系统
Docsify
VuePress
Mediawiki
自动生成
Marp
CI/CD
GitLab
设计
颜色
平面设计
AI
数字人
操作系统
GNU/Linux
数据库
Mysql
工具
链入页面
相关更改
特殊页面
页面信息