BTW, you only posted the decrypt code, so I'm attaching both encrypt
and decrypt here just to make sure we're on the same page.
I've narrowed it down to not being able to decrypt the 512 bytes key
from the file.

    Avi.

--
void stubClass::AES_CTR_Encrypt(const char *infile, const char
                                                                *outfile)
{
        try
        {
                //Open outfile in binary and output
                ofstream file( outfile, ios::binary );

                BYTE keyAES[ CryptoPP::AES::MAX_KEYLENGTH ];
                BYTE  ivAES[ CryptoPP::AES::BLOCKSIZE ];

                //Generate AES key and iv
                CryptoPP::OS_GenerateRandomBlock(false, keyAES,
                        CryptoPP::AES::MAX_KEYLENGTH);
                CryptoPP::OS_GenerateRandomBlock(false, ivAES,
                        CryptoPP::AES::BLOCKSIZE);

                //Pass key to string
                string AesKey((char*)keyAES, CryptoPP::AES::MAX_KEYLENGTH);
                AesKey.append((char*)ivAES, CryptoPP::AES::BLOCKSIZE);

                //Crypt string AesKey with RSA and save to file stream in 
HexFormat
                CryptoPP::StringSource pubStr(pubString, true, new
                        CryptoPP::HexDecoder);
                CryptoPP::RSAES_OAEP_SHA_Encryptor pub(pubStr);
                CryptoPP::AutoSeededX917RNG <CryptoPP::DES_EDE3> rng;

                CryptoPP::StringSource(AesKey, true,
                        new CryptoPP::PK_EncryptorFilter(rng, pub,
                        new CryptoPP::HexEncoder(new 
CryptoPP::FileSink(file))));

                //Crypt file with AES-CBC and save to file stream
                CryptoPP::AES::Encryption aesEncryption( keyAES,
                        CryptoPP::AES::MAX_KEYLENGTH);
                CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption
                        ( aesEncryption,ivAES );

                CryptoPP::FileSource(infile, true,
                        new CryptoPP::StreamTransformationFilter(cbcEncryption,
                        new CryptoPP::FileSink(file)));

                //Close the stream
                file.close();
        }
        catch(CryptoPP::Exception &e)
        {
                cout << "CryptoPP::Exception caught: " << e.what() << endl;
        }
}

void stubClass::AES_CTR_Decrypt(const char *infile, const char
*outfile)
{
        try
        {
                ifstream file( infile, ios::binary );

                byte keyAES[ CryptoPP::AES::MAX_KEYLENGTH ];
                byte ivAES[  CryptoPP::AES::BLOCKSIZE ];

                char Key[512];
                file.read(Key,512);

                CryptoPP::StringSource privStr(priString, true,
                        new CryptoPP::HexDecoder);
                CryptoPP::RSAES_OAEP_SHA_Decryptor priv(privStr);
                CryptoPP::AutoSeededX917RNG<CryptoPP::DES_EDE3> rng;
                string AesKeyUnCrypt;

                CryptoPP::StringSource(Key, true,
                        new CryptoPP::HexDecoder(
                        new CryptoPP::PK_DecryptorFilter(rng, priv,
                        new CryptoPP::StringSink(AesKeyUnCrypt))));

                
memcpy(keyAES,AesKeyUnCrypt.data(),CryptoPP::AES::MAX_KEYLENGTH);
        memcpy(ivAES,AesKeyUnCrypt.data()+
CryptoPP::AES::MAX_KEYLENGTH, CryptoPP::AES::BLOCKSIZE);

                CryptoPP::AES::Decryption aesDecryption( keyAES,
CryptoPP::AES::MAX_KEYLENGTH);
                CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption
( aesDecryption, ivAES );
                CryptoPP::FileSource(file, true,
            new CryptoPP::StreamTransformationFilter(cbcDecryption,
            new CryptoPP::FileSink(outfile)));
        }
        catch(CryptoPP::Exception &e)
        {
                std::cout << "CryptoPP::Exception caught: " << e.what() << endl;
        }
}
--

On Feb 1, 12:10 pm, Avi <[email protected]> wrote:
> Hi Dillon,
>
> I'm new around here and new to the cryptography world.
>
> I'm using VC++ 6.0 SP5 on a WinXP SP2 machine (I have a legacy code
> base that I'd like to secure).
>
> I tried to follow your (and originally Hugo's) example, but I'm having
> the same runtime error in both cases.
> I'm posting my problem here instead of in Hugo's thread in order to
> narrow the differences between our executables (since you embedded
> your keys into the source code).
>
> The code crashes in oaep.cpp, line 91: memcpy(output, M, maskedDB
> +dbLen-M);
> The 'output' value is 0 (NULL).
>
> I attached the call stack below.
> Any ideas?
>
>     Avi.
>
> --
> CryptoPP::OAEP_Base::Unpad(const unsigned char * 0x0032b7f1, unsigned
> int 255, unsigned char * 0x00000000, const CryptoPP::NameValuePairs &
> {...}) line 91 + 20 bytes
> CryptoPP::TF_DecryptorBase::Decrypt(CryptoPP::RandomNumberGenerator &
> {...}, const unsigned char * 0x0032bbd8, unsigned int 257, unsigned
> char * 0x00000000, const CryptoPP::NameValuePairs & {...}) line 142 +
> 59 bytes
> CryptoPP::PK_DefaultDecryptionFilter::Put2(const unsigned char *
> 0x00000000, unsigned int 0, int -1, unsigned char 1) line 626 + 75
> bytes
> CryptoPP::BufferedTransformation::MessageEnd(int -1, unsigned char 1)
> line 763 + 78 bytes
> CryptoPP::SimpleProxyFilter::LastPut(const unsigned char * 0x00000000,
> const unsigned char * 0x00000000) line 513 + 51 bytes
> CryptoPP::FilterWithBufferedInput::PutMaybeModifiable(unsigned char *
> 0x00326d68, unsigned int 0, int -2, unsigned char 1, unsigned char 0)
> line 410
> CryptoPP::FilterWithBufferedInput::Put2(const unsigned char *
> 0x00326d68, unsigned int 0, int -2, unsigned char 1) line 156
> CryptoPP::Filter::Output(int 2, const unsigned char * 0x00326d68,
> unsigned int 0, int -2, unsigned char 1, const
> std::basic_string<char,std::char_traits<char>,std::allocator<char> > &
> {0x00000000 ""}) line 115 + 44 bytes
> CryptoPP::BaseN_Decoder::Put2(const unsigned char * 0x00000000,
> unsigned int 0, int -1, unsigned char 1) line 163 + 42 bytes
> CryptoPP::BufferedTransformation::ChannelPut2(const
> std::basic_string<char,std::char_traits<char>,std::allocator<char> > &
> {0x00000000 ""}, const unsigned char * 0x00000000, unsigned int 0, int
> -1, unsigned char 1) line 250 + 27 bytes
> CryptoPP::BufferedTransformation::ChannelMessageEnd(const
> std::basic_string<char,std::char_traits<char>,std::allocator<char> > &
> {0x00000000 ""}, int -1, unsigned char 1) line 938 + 63 bytes
> CryptoPP::BufferedTransformation::TransferMessagesTo2
> (CryptoPP::BufferedTransformation & {...}, unsigned int & 0, const
> std::basic_string<char,std::char_traits<char>,std::allocator<char> > &
> {0x00000000 ""}, unsigned char 1) line 407 + 28 bytes
> CryptoPP::BufferedTransformation::TransferAllTo2
> (CryptoPP::BufferedTransformation & {...}, const
> std::basic_string<char,std::char_traits<char>,std::allocator<char> > &
> {0x00000000 ""}, unsigned char 1) line 448 + 24 bytes
> CryptoPP::SourceTemplate<CryptoPP::StringStore>::PumpAll2(unsigned
> char 1) line 718 + 73 bytes
> CryptoPP::Source::PumpAll() line 689 + 47 bytes
> CryptoPP::Source::SourceInitialize(unsigned char 1, const
> CryptoPP::NameValuePairs & {...}) line 701
> CryptoPP::StringSource::StringSource(const char * 0x0012fbd8, unsigned
> char 1, CryptoPP::BufferedTransformation * 0x00326b10) line 737 + 173
> bytes
> stubClass::AES_CTR_Decrypt(const char * 0x0012ff48, const char *
> 0x0012ff30) line 128 + 364 bytes
> main() line 92
> --
>
> On Jan 23, 4:34 am, Dillon Beresford <[email protected]>
> wrote:
>
> > Hugo shared a class he coded up earlier this month which he gave to the 
> > group and while searching for information regarding an idea I came up with 
> > about self decrypting stubs using Crypto++, which I needed for my own 
> > project I found his thread. I was actually able to code up a little 
> > function which I added to the class to load keys using
> > CryptoPP::ArraySource as opposed to LoadFile. For my project I needed to be 
> > able to generate stubs on the fly which would handle all of the decryption. 
> > I'm also currently writing up some code that will actually add two extra 
> > layers of protection to both keys inside the executable to avoid reverse 
> > code engineering from dumping keys in memory We simply store our keys in 
> > the stub.
>
> > If we add stub + *.enc file to SFX we can have our stub do all the work for 
> > us. :)
>
> > I would suggest to anyone storing keys inside a binary to compress the 
> > executable using something other than UPX or ASPack. ;-) keys get gen all 
> > the time so it's best to use something with tons of instructions for 
> > anti-debugging, strip TLS, exports, and CRC protection and aPlib for 
> > compression.
>
> > Dillon's code reading from CryptoPP::ArraySource as opposed to 
> > CryptoPP::FileSource
>
> > void stubClass::LoadKey(const char *bufferPub, const char *bufferPri) {
>
> >         CryptoPP::ArraySource(bufferPub, true,
>
> >                 new CryptoPP::StringSink(pubString));
>
> >         CryptoPP::ArraySource(bufferPri, true,
>
> >                 new CryptoPP::StringSink(priString));
>
> > }
>
> > Hugo's class using CryptoPP::FileSource
> > /*
>
> > void stubClass::LoadKey(const char *pubFilename, const char *privFilename) {
>
> >         CryptoPP::FileSource (pubFilename, true,
>
> >                 new CryptoPP::StringSink(pubString));
>
> >         CryptoPP::FileSource (privFilename, true,
>
> >                 new CryptoPP::StringSink(priString));
>
> > }
>
> > */
>
> > We can store our keys in bufferPub and bufferPri and call this from the 
> > main() in our stub.
>
> > const char bufferPri[]="308..."
> > const char bufferPub[]="308..."
>
> > int main() {
> >         char EFile[]="dob.enc";
> >         char DFile[]="dec.exe";
>
> >         stubClass stubDecrypt;
> >         // grab our keys from memory.
> >         stubDecrypt.LoadKey(bufferPub,bufferPri);
> >         // decrypt our file  
> >         stubDecrypt.AES_CTR_Decrypt(EFile, DFile);
>
> >         return 0;
>
> > }
>
> > thanks Hugo!
>
> > linked back to the original thread where Hugo posted his class.
>
> >http://groups.google.com/group/cryptopp-users/browse_thread/thread/48...
>
> > regards,
>
> > Dillon Beresford
>
> >  stub.cpp
> > 4KViewDownload
>
> >  stubClass.cpp
> > 3KViewDownload
>
> >  stubClass.h
> > < 1KViewDownload
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
-~----------~----~----~----~------~----~------~--~---

Reply via email to