4 Star 1 Fork 0

cloudin/国密算法课设

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sm3.c 1.86 KB
一键复制 编辑 原始数据 按行查看 历史
#include "pch.h"
#include <string>
#include <cstring>
#include <vector>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include "cryptopp/aes.h"
#include "cryptopp/filters.h"
#include "cryptopp/modes.h"
using namespace std;
using namespace CryptoPP;
byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE];
void initKV() {
memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);
memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);
}
string encrypt(string plainText) {
string cipherText;
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(cipherText));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plainText.c_str()), plainText.length() + 1);
stfEncryptor.MessageEnd();
string cipherTextHex;
for (int i = 0; i < cipherText.size(); i++)
{
char ch[3] = { 0 };
sprintf_s(ch, "%02x", static_cast<byte>(cipherText[i]));
cipherTextHex += ch;
}
return cipherTextHex;
}
int main() {
string text = "20201310";
cout << "text : " << text << endl;
initKV();
string cipherHex = encrypt(text);
cout << "cipher : " << cipherHex << endl;
}
#include "cryptlib.h"
#include "sm3.h"
#include <iostream>
#include <cryptopp/hex.h>
#include <cryptopp/files.h>
int main(int argc, char* argv[])
{
using namespace CryptoPP;
HexEncoder encoder(new FileSink(std::cout));
std::string msg = "20201310";
std::string digest;
SM3 hash;
hash.Update((const byte*)&msg[0], msg.size());
digest.resize(hash.DigestSize());
hash.Final((byte*)&digest[0]);
std::cout << "Message: " << msg << std::endl;
std::cout << "Digest: ";
StringSource(digest, true, new Redirector(encoder));
std::cout << std::endl;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/cloud-in/national-secret-store.git
git@gitee.com:cloud-in/national-secret-store.git
cloud-in
national-secret-store
国密算法课设
master

搜索帮助

Dd8185d8 1850385 E526c682 1850385