Using a key from a string

1,626 views
Skip to first unread message

Some Guy

unread,
Aug 30, 2011, 9:45:55 PM8/30/11
to Crypto++ Users
Hi, I'm trying to load keys from a string then perform operations but
I'm obviously doing it wrong. This is just using sample code from the
Crypto++ wiki except for the key loading which I've changed and is
wrong. Can someone tell me how I should be doing it please?

Any help is much appreciated, next thing I'm trying is generating the
keys with crypto++ rather than the online generator I was using.

std::string pub_key =
"AAAAB3NzaC1yc2EAAAABJQAAAIEAofcO32Qko4srylZRXQywJqWt4msjzrilwIntr21mjZM8vQ3pTsM/
GWW+nQzAAGp0JPanYbJN7f3HGwF7z8V51cq41PJKnAwJDayoj5eOEM8iIpgZaoowT
+jfn25Q2+55oEpQz41LdvZoLZ4s58JI30r9byWWlTwen9+JwGFcJ3c==";
std::string priv_key = "AAAAgEYJ+Jfz9CsLv
+jLYXRYg4ZVe6AuVKxrhfJlKIM2Oi9iQ8df9i/lumvY+HtD"
"yKY73zKULLSgIbMF9TwcUTdAo2KqKpX
+phAZNXEXjCB14JSXObhkJvm8ak5kdDSj"
"SZIGIRTPueonhvvya
+tzxmvVpQLpxezlMx6LppzPJxJ8tF6tAAAAQQD96+ATzVro"
"D01/N8ULQNOhCzdpudjtAxFjJWSlt7yhNLMFfXdh7q/
ihRYIsbEBgI7lmq6ISmFc"
"HBgfahbXldH/AAAAQQCjSnoBnTvpriqm/L97teWQYXDGxXAZd2k9bYEO/
v2fGG0k"
"dWqv9KBzSYcZwF1iurmoyQzLRf09ET0hFU7JZTqJAAAAQBa0//
vmDitumKwJSXXH"
"kS84LKn8IM9VJ78VosY49A8f1p8FgEb15xwiuKsI2A3w9LGcqi3KDwUAwXl9y9/
o"
"Yhs";

try
{

RSA::PrivateKey privateKey;
privateKey.Load(StringSource(priv_key,true,NULL).Ref());
RSA::PublicKey publicKey;
publicKey.Load(StringSource(pub_key,true,NULL).Ref());

// Message
string message = "Yoda said, Do or Do Not. There is not try.";
string signature;

////////////////////////////////////////////////
// Sign and Encode
RSASS<PSS, SHA1>::Signer signer( privateKey );

StringSource( message, true,
new SignerFilter( rng, signer,
new StringSink( signature )
) // SignerFilter
); // StringSource

////////////////////////////////////////////////
// Verify and Recover
RSASS<PSS, SHA1>::Verifier verifier( publicKey );

StringSource( message+signature, true,
new SignatureVerificationFilter(
verifier, NULL,
SignatureVerificationFilter::THROW_EXCEPTION
) // SignatureVerificationFilter
); // StringSource

cout << "Verified signature on message" << endl;

} // try

catch( CryptoPP::Exception& e ) {
std::cerr << "Error: " << e.what() << std::endl;
}

return 0;
}

Some Guy

unread,
Aug 30, 2011, 9:48:12 PM8/30/11
to Crypto++ Users
Oh should have mentioned the error is BER decode error

Geoff Beier

unread,
Aug 30, 2011, 10:03:06 PM8/30/11
to Some Guy, Crypto++ Users

Base-64 decode your keys. (Sorry, posting from a phone so I can't grab and paste code, but there's plenty of it out there. )

On Aug 30, 2011 9:48 PM, "Some Guy" <some....@gmail.com> wrote:

Niij

unread,
Sep 18, 2011, 6:19:53 PM9/18/11
to Crypto++ Users
Hi,

I'm in the same situation as you but with EC scheme, I used Harry's
code (http://groups.google.com/group/cryptopp-users/browse_thread/
thread/e58e46f0e8558f2b/022e858d15529859?lnk=gst&q=How+to+save+public
+key+and+private+key+for+#022e858d15529859)
to get the private and public key written on files and now I 'd like
to Load the public key from the public.key file using the Load()
method from PublicKey class

Here is the portion of my code :
CryptoPP::ECIES < ECC_ALGORITHM >::Encryptor Encryptor
(publicKey);

// Public Key Loading

std::ifstream file2("Public.key", std::ios::in);
string public_k;
if(file2)
{
getline(file2, public_k); // put the first line on
"public_k"
cout << "the public key " << public_k << endl ; //
display the line
file2.close();
}
else
cerr << "impossible to open the file!" << endl;

publicKey.Load(public_k);

-------------

After compiling I got the following error :
error: no matching function for call to
‘CryptoPP::DL_PublicKey_EC<CryptoPP::ECP>::Load(std::string&)’
/usr/include/cryptopp/asn.h:240: note: candidates are: void
CryptoPP::ASN1CryptoMaterial<BASE>::Load(CryptoPP::BufferedTransformation&)
[with BASE = CryptoPP::PublicKey]

The problem seems to be the choice of the parameter type for Load() ,
which should be BufferedTransformation&. How can I convert from
string to this type then ?

thanks for help.






Jeffrey Walton

unread,
Sep 18, 2011, 9:38:27 PM9/18/11
to Crypto++ Users
You might want to use a FileSource rather than an istream directly. A
FileSource is a BufferedTransformation.

Base64 encoding and decoding is covered near the bottom of
http://www.cryptopp.com/wiki/Keys_and_formats.

Jeff
Message has been deleted

Amin

unread,
Sep 19, 2011, 8:40:30 AM9/19/11
to cryptop...@googlegroups.com
I save and load the compressed version of EC keys in BaseEncoded form:

void Security::SavePublicKeyinFile (string pub){ //pub--> name of the file to be saved

Base64Encoder pubkeysink(new FileSink(pub.c_str()));
publicKey.GetGroupParameters().GetCurve().EncodePoint(pubkeysink,publicKey.GetPublicElement(),true);
pubkeysink.MessageEnd(); // Need to flush Base64Encoders buffer
}


void Security::LoadPublicKeyfromFile (ECDSA<ECP, SHA1>::PublicKey &SubjectPublicKey, char* pub){ //pub--> name of the file to be loaded

FileSource pubSrc(pub, true, new Base64Decoder);
ECP::Point pp;
SubjectPublicKey.AccessGroupParameters().Initialize(CryptoPP::ASN1::secp224r1());
SubjectPublicKey.GetGroupParameters().GetCurve().DecodePoint(pp,pubSrc,SubjectPublicKey.GetGroupParameters().GetCurve().EncodedPointSize(true));
SubjectPublicKey.SetPublicElement(pp);
}

/Amin

Niij

unread,
Sep 21, 2011, 4:13:16 PM9/21/11
to Crypto++ Users
thanks for your help.
Reply all
Reply to author
Forward
0 new messages