newbie question on base64/ RSA decoding

202 views
Skip to first unread message

Stephan Stapel

unread,
May 31, 2004, 4:34:19 PM5/31/04
to crypto...@eskimo.com
Dear people on this list!

I've been 'playing' around with CryptoPP for quite some time now and
have some problems decrypting a base64-encoded string I've encrypted before.
My code looks like this:


CryptoPP::RSAES_OAEP_SHA_Decryptor privkey(rng, 1024);
CryptoPP::RSAES_OAEP_SHA_Encryptor pubkey(privkey);


// .....

// encrypt a string
CryptoPP::SecByteBlock
sbbCipherText(pubkey.CiphertextLength(szClearText.size()));

pubkey.Encrypt(rng,
(byte const*)szClearText.data(),
szClearText.size(),
sbbCipherText.begin());

std::string resultEncoded;

CryptoPP::Base64Encoder be3(new CryptoPP::StringSink(resultEncoded));
be3.Put(sbbCipherText.begin(), sbbCipherText.size());
std::cout << "Encrypted string: " << resultEncoded << std::endl;


// decrypt the string
char szDecoded[1024];

CryptoPP::DecodingResult dresult = privkey.Decrypt(
rng,
(const byte*)sbbCipherText.data(),
sbbCipherText.size(),
(byte*)szDecoded);
szDecoded[dresult.messageLength] = '\0';
std::string szCleartextEncoded = std::string(szDecoded);

std::cout << "Decrypted string: " << szCleartextEncoded << std::endl;


This works really fine. But what I would like to achieve is to use the
base64 encoded string 'resultEncoded' as the input for decryption which
I don't get going though trying hard.

Can someone please help me out on this?

Thanks in advance

and

kind regards,

Stephan

Stephan Stapel

unread,
Jun 1, 2004, 2:18:57 PM6/1/04
to Walton, Jeffrey, crypto...@eskimo.com
Hi again!

Thanks for your very quick response. Somehow I missed that entry in the
FAQ. Sorry for that. Unfortunately, I still have problems running the
program.
I modified the mentioned code to:


std::string resultEncoded;

// 1. encrypt string into a SecByteBlock
// 2. Encode string into a Base64 std::string (resultEncoded)

// decrypt the string 'resultEncoded'

char szDecodedEncryption[1024];
CryptoPP::Base64Decoder bd;
bd.Put((const byte*)resultEncoded.data(), resultEncoded.size());
bd.MessageEnd();
bd.Get((byte*)szDecodedEncryption, resultEncoded.size() * 3 / 2);
szDecodedEncryption[resultEncoded.size() * 3 / 2] = '\0';
std::string decodedEncryption(szDecodedEncryption);

char szDecoded[1024];

CryptoPP::DecodingResult dresult = privkey.Decrypt(rng,

(const byte*)decodedEncryption.data(),
(decodedEncryption.size(),


(byte*)szDecoded);
szDecoded[dresult.messageLength] = '\0';
std::string szCleartextEncoded = std::string(szDecoded);
std::cout << "Decrypted string: " << szCleartextEncoded << std::endl;

The strange thing is
1) if I use the SecByteBlock which I used on Encrypting, everything
works fine.
2) The SecByteBlock that is being returned is identical to the decoded
'decodedEcryption'.


I'm just wondering what I'm still doing wrong. Can you point me to my
mistake?

Kind regards,

Stephan

Reply all
Reply to author
Forward
0 new messages