dsa_ver.cpp
来自「含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种Chec」· C++ 代码 · 共 121 行
CPP
121 行
/*Grab an DSA public key from the file given as an argument, grab a signaturefrom another file, and verify the message (which, suprise, is also in a file).Note that the key format is just the domain parameters and public key, one perline, in decimal. No provisions for anything complicated... that's why this isonly an example program.The signature format is actually reasonably portable. It's simply the IEEE 1363signature format, encoded into base64 with a trailing newlineWritten by Jack Lloyd (lloyd@randombit.net), August 5, 2002This file is in the public domain*/#include <iostream>#include <iomanip>#include <fstream>#include <cstdlib>#include <string>#include <botan/filters.h>#include <botan/lookup.h>#include <botan/look_pk.h>#include <botan/dsa.h>using namespace Botan;int main(int argc, char* argv[]) { if(argc != 4) { std::cout << "Usage: dsa_ver keyfile messagefile sigfile" << std::endl; return 1; } std::ifstream keyfile(argv[1]); if(!keyfile) { std::cout << "Couldn't read the key file." << std::endl; return 1; } std::ifstream message(argv[2]); if(!message) { std::cout << "Couldn't read the message file." << std::endl; return 1; } std::ifstream sigfile(argv[3]); if(!sigfile) { std::cout << "Couldn't read the signature file." << std::endl; return 1; } try { LibraryInitializer init; std::string g, p, q, y; getline(keyfile, g); getline(keyfile, p); getline(keyfile, q); getline(keyfile, y); DL_Group group(p, q, g); DSA_PublicKey key(group, y); // should be checking the key with check_params, but it takes a while to do // the domain verification, so don't bother std::string sigstr; getline(sigfile, sigstr); Pipe pipe(new Base64_Decoder); pipe.start_msg(); pipe.write(sigstr); pipe.end_msg(); SecureVector<byte> sig = pipe.read_all(); pipe.reset(); pipe.append(new PK_Verifier_Filter(get_pk_verifier(key, "EMSA1(SHA-1)"), sig, sig.size())); pipe.start_msg(); message >> pipe; pipe.end_msg(); pipe.set_default_msg(1); if(pipe.remaining() != 1) { std::cout << "Internal error in dsa_ver: report this" << std::endl; return 1; } byte result = 0; pipe.read(result); if(result) std::cout << "Signature verified\n"; else std::cout << "Signature did NOT verify\n"; } catch(Exception& e) { std::cout << "Exception caught: " << e.what() << std::endl; } catch(std::exception& e) { std::cout << "Standard library exception caught: " << e.what() << std::endl; } catch(...) { std::cout << "Unknown exception caught." << std::endl; } return 0; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?