Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How is base64 Encoding and Bas64 decoding done with crypto++?

What I have tried:

I did a google search which gave an example for base64Encoding but gave none for decoding?
Posted

You actually used Google! Well done!

Pity you didn't actually look at any of the results ... Base64 - Wikipedia[^] covers both encoding and decoding - but you should realize that Base64 isn't a form of encryption, it's a translation which allows binary data to be transferred via a test-only medium and is "crackable" by four year olds given a big enough crayon ...
 
Share this answer
 
Comments
Gbenbam 27-Jul-24 15:17pm    
I know what base64Encoding is used for.
If you're looking to decode base64, something like this will do it.
std::string decodeBase64EncodedString(std::string encoded) {
    std::string decoded;

    
    CryptoPP::Base64Decoder decoder;
      
 
    decoder.Put(
     (CryptoPP::byte*)encoded.data(), encoded.size());
    decoder.MessageEnd();

    CryptoPP::word64 size = decoder.MaxRetrievable();
    if(size && size <= SIZE_MAX) {
       decoded.resize(size);
            decoder.Get((CryptoPP::byte*)&decoded[0], decoded.size());
    }
    return decoded;
}
 
Share this answer
 
Comments
OriginalGriff 27-Jul-24 8:47am    
This is a serious Help Vampire - check his history ... :sigh:
Pete O'Hanlon 27-Jul-24 11:12am    
Oh crap. I hadn't realised that. Thanks for the heads up.
Gbenbam 27-Jul-24 15:43pm    
You should realise that @OriginalGriff has an axe to grind with me. He has been trying hard to get at me, but I have simply ignored him. His latest antic is turning people against me. Don't be fooled. I ask questions and give answers.How does that make me a kmowledge vampire. Here are same codes I got from the net but my compiler did not recognise the base64Encoder and base64Decoder words , I am guessing that it is because of of header file unprovided. I am still trying to figure out the header file.

bool Base64Encode(std::string ciphertext, std::string &encoded)
{
CryptoPP::StringSource(ciphertext, true,
new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded)));
}


bool Base64Decode(std::string encoded, std::string& ciphertext)
{
CryptoPP::StringSource(encoded, true,
new CryptoPP::Base64Decoder(new CryptoPP::StringSink(ciphertext)));
}


As for @OriginalGriff,this where he started having issues with me.
https://www.codeproject.com/Questions/5383852/What-will-happen-if-a-user-attempt-to-use-an-accel

He broke the following codeproject rule,rather than feeling any remorse,he is busy planning revenge:

"Let's work to help developers, not make them feel stupid."


He promised in a now deleted post, that I will soon not have an account. Well, he failed because I simply ignored him. Well, my suggestion to you is that you should feel free to ignore any of my questions, but don't do it because you were fooled into doing so. Its unfortunate that his unsavoury comments have been deleted.I would have shown them to you.

See him at work here.
https://www.codeproject.com/Questions/5384537/How-to-programatically-get-the-number-of-menus-in

Like I said,feel free to ignore my questions,but do so for the right reasons.
Dave Kreskowiak 29-Jul-24 17:34pm    
No, he's not grinding an axe with you. You are literally using us to do your research work for you and try to explain whatever the answers are in the simplest terms possible. After 360 questions, you'd think you would have gotten the hang of doing research by now.
Gbenbam 3-Aug-24 17:59pm    
O.K. You should be able to prove this allegation with recent questions. Give me proof. Perhaps, more importantly,tell me how exactly you believe that codeproject should be used.
You can use the methods in this way without using a try-catch block:
C++
#include "../cryptopp/base64.h"

// text to be encoded
std::string encodetxt, decodetxt, uncodedtxt("This is a sample text for base64 coding!");

// Base64 encoding (Base64Encoder)
CryptoPP::StringSource ss1(uncodedtxt, true,
         new CryptoPP::Base64Encoder(  new CryptoPP::StringSink(encodetxt) )); 

// Base64 decoding (Base64Decoder)
CryptoPP::StringSource ss2(encodetxt, true,
          new CryptoPP::Base64Decoder( new CryptoPP::StringSink(decodetxt) ));

The coding is based on RFC4648, additional code is required for a web safe alphabet according to the documentation.
 
Share this answer
 
Encrypt with CrtptoPP -> Encode to Base 64 -> Decode from Base 64 -> Decrypt with CryptoPP
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900