std::string Converter::SecByteBlockToString(SecByteBlock data){ Integer a; a.Decode(data.BytePtr(), data.SizeInBytes()); //cout<< "ze secbyteblock do integera: "<<a<<endl; std::ostrstream oss; oss << std::hex << a; std::string s(oss.str()); s = s.substr(0, 2*data.SizeInBytes()+1); //Do zapamiêtania. d³ugoœæ stringa z s wynosi 2*d³ugoœæ w bytach plus 1. return s; }
//******************************************************************************************************** Integer Converter::decodeSecByteBlock(SecByteBlock key) { Integer x; x.Decode(key.BytePtr(), key.SizeInBytes()); return x; }
int main() { using namespace CryptoPP; AutoSeededRandomPool rng; DH dhA; //< Diffie-Hellman structure DH2 dh2A(dhA); //< Diffie-Hellman structure SecByteBlock sprivA; //< static private key SecByteBlock spubA; //< static public key SecByteBlock eprivA; //< ephemeral private key SecByteBlock epubA; //< ephemeral public key SecByteBlock sharedA; //< shared key DH dhB; //< Diffie-Hellman structure DH2 dh2B(dhA); //< Diffie-Hellman structure SecByteBlock sprivB; //< static private key SecByteBlock spubB; //< static public key SecByteBlock eprivB; //< ephemeral private key SecByteBlock epubB; //< ephemeral public key SecByteBlock sharedB; //< shared key std::cout << "Initializing DH parameters" << std::endl;; dhA.AccessCryptoParameters().GenerateRandomWithKeySize(rng,1024); dhB.AccessCryptoParameters().GenerateRandomWithKeySize(rng,1024); std::cout << "Generating Keys" << std::endl;; sprivA = SecByteBlock( dh2A.StaticPrivateKeyLength() ); spubA = SecByteBlock( dh2A.StaticPublicKeyLength() ); eprivA = SecByteBlock( dh2A.EphemeralPrivateKeyLength() ); epubA = SecByteBlock( dh2A.EphemeralPublicKeyLength() ); dh2A.GenerateStaticKeyPair(rng,sprivA,spubA); dh2A.GenerateEphemeralKeyPair(rng,eprivA, epubA); sharedA= SecByteBlock( dh2A.AgreedValueLength() ); sprivB = SecByteBlock( dh2B.StaticPrivateKeyLength() ); spubB = SecByteBlock( dh2B.StaticPublicKeyLength() ); eprivB = SecByteBlock( dh2B.EphemeralPrivateKeyLength() ); epubB = SecByteBlock( dh2B.EphemeralPublicKeyLength() ); dh2B.GenerateStaticKeyPair(rng,sprivB,spubB); dh2B.GenerateEphemeralKeyPair(rng,eprivB, epubB); sharedB= SecByteBlock( dh2B.AgreedValueLength() ); if(!dh2A.Agree(sharedA, sprivA, eprivA, spubB, epubB) ) std::cerr << "A failed to agree " << std::endl; if(!dh2B.Agree(sharedB, sprivB, eprivB, spubA, epubA) ) std::cerr << "B failed to agree " << std::endl; Integer sharedAOut, sharedBOut; sharedAOut.Decode(sharedA.BytePtr(), sharedA.SizeInBytes()); sharedBOut.Decode(sharedB.BytePtr(), sharedB.SizeInBytes()); std::cout << "shared secret:" << "\n A: " << std::hex << sharedAOut << "\n B: " << std::hex << sharedBOut << std::endl; }