我想做一个md5批量加密的软件,谁可以做要批量加密的
我有好多文本需要加密成md5,每个文本的都是数字和字符串一行一个,现在需要一个md5加密工具加密成这样的格式,51251251|ee2a5c3c8996415f|a324cc1eee2a5c3c8996415f572867df|md5
前面是明文后面是16为还有32位最后是加密方式,谁可以帮我写个小工具批量加密生成成这样的格式 大神可以做的联系我有红包qq2429282227
#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编辑过]
#include <windows.h> #include <stdio.h> #include <string.h> int main( void ) { const char* src = "D:\\source\\vc2017\\cpp001\\src.txt"; const char* dst = "D:\\source\\vc2017\\cpp001\\dst.txt"; HCRYPTPROV hProv = 0; CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT); FILE* fin = fopen( src, "rt" ); FILE* fout = fopen( dst, "wt" ); HCRYPTPROV hHash = 0; CryptCreateHash(hProv,CALG_MD5,0,0,&hHash); for( char buf[1024]; fgets(buf,sizeof(buf),fin); ) { char* p = strchr(buf,'\n'); if( p || feof(fin) ) { CryptHashData(hHash,(const BYTE*)buf,p-buf,0); fprintf( fout, "%.*s", p-buf, buf ); BYTE buf[16]; DWORD dwHashLen = sizeof(buf); CryptGetHashParam(hHash,HP_HASHVAL,buf,&dwHashLen,0); CryptDestroyHash(hHash); CryptCreateHash(hProv,CALG_MD5,0,0,&hHash); fprintf( fout, "|" ); for( size_t i=4; i!=12; ++i ) fprintf( fout, "%02x", buf[i] ); fprintf( fout, "|" ); for( size_t i=0; i!=sizeof(buf); ++i ) fprintf( fout, "%02x", buf[i] ); fprintf( fout, "|md5\n" ); } else { CryptHashData(hHash,(const BYTE*)buf,sizeof(buf),0); fprintf( fout, "%s", buf ); } } fclose( fout ); fclose( fin ); CryptReleaseContext(hProv,0); }