![]() |
#2
rjsp2021-04-20 16:36
C++ 比较简单
用到了 windows APIs,否则你自己去网上找个开源的MD5 ![]() #include <string> #include <optional> #include <stdexcept> #include <windows.h> class MD5 { public: MD5() { if( !CryptAcquireContext(&hProv_,nullptr,nullptr,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT) ) throw std::runtime_error("failed to invoke CryptAcquireContext."); } ~MD5() { if( hProv_ ) CryptReleaseContext(hProv_,0); } MD5( const MD5& ) = delete; MD5( MD5&& ) = default; MD5& operator=( const MD5& ) = delete; MD5& operator=( MD5&& ) = default; std::optional<std::string> operator()( const std::string& s ) const { HCRYPTPROV hHash = 0; if( !CryptCreateHash(hProv_,CALG_MD5,0,0,&hHash) ) return {}; BYTE buf[16]; if( !CryptHashData(hHash,(const BYTE*)s.c_str(),s.size(),0) ) return {}; DWORD dwHashLen = sizeof(buf); if( !CryptGetHashParam(hHash,HP_HASHVAL,buf,&dwHashLen,0) ) return {}; CryptDestroyHash(hHash); std::string result( 2*sizeof(buf), ' ' ); for( size_t i=0; i!=std::size(buf); ++i ) { result[2*i+0] = "0123456789abcdef"[buf[i]/0x10]; result[2*i+1] = "0123456789abcdef"[buf[i]%0x10]; } return result; } private: HCRYPTPROV hProv_ = 0; }; #include <iostream> #include <fstream> using namespace std; int main( void ) { const char* src = R"(D:\source\vc2017\cpp001\src.txt)"; const char* dst = R"(D:\source\vc2017\cpp001\dst.txt)"; ifstream is(src); if( !is ) return false; ofstream os(dst); if( !is ) return false; MD5 md5; for( string line; getline(is,line); ) { if( line.empty() ) os << '\n'; else { auto m = md5(line).value(); os << line << '|' << m.substr(8,16) << '|' << m << "|md5\n"; } } } [此贴子已经被作者于2021-4-20 16:38编辑过] |
我有好多文本需要加密成md5,每个文本的都是数字和字符串一行一个,现在需要一个md5加密工具加密成这样的格式,
51251251|ee2a5c3c8996415f|a324cc1eee2a5c3c8996415f572867df|md5
前面是明文后面是16为还有32位最后是加密方式,谁可以帮我写个小工具批量加密生成成这样的格式 大神可以做的联系我有红包qq2429282227