Problem when trying to decryp encryped file with CryptoPP (C++)

  • Context: C/C++ 
  • Thread starter Thread starter CMV123
  • Start date Start date
  • Tags Tags
    File
Click For Summary
SUMMARY

The discussion centers on a decryption issue encountered while using CryptoPP to decrypt a file encrypted with AES128. The user successfully decodes the Base64-encoded encrypted file but receives a 'KeyBadErr' when attempting to decrypt it with the provided binary key file. The problem is likely due to discrepancies in encryption algorithms, padding schemes, or the mode of operation (e.g., ECB, CBC, CTR) used during encryption. The user confirms that their own encryption and decryption processes work correctly, indicating that the issue lies with the external file or its key.

PREREQUISITES
  • Familiarity with AES128 encryption and decryption processes.
  • Understanding of Base64 encoding and decoding.
  • Knowledge of CryptoPP library functions, specifically DefaultDecryptor and DefaultDecryptorWithMAC.
  • Experience with file handling in C++.
NEXT STEPS
  • Investigate the differences between Java AES128 and CryptoPP AES128 implementations.
  • Research padding schemes used in AES encryption and how they affect decryption.
  • Learn about the various modes of AES operation (ECB, CBC, CTR) and their implications on encryption/decryption.
  • Examine the structure and integrity of the binary key file used for decryption.
USEFUL FOR

C++ developers, cryptography enthusiasts, and anyone working with file encryption and decryption using the CryptoPP library.

CMV123
Messages
1
Reaction score
0
Hi all,

I have a problem.

Someone I know encrypted a text file using AES128 by using binary key file, (He generate the binary key).
After that he uses "base64Encode" over the encryped file

I have the private binary key file.
Now, when I'm trying to decrypt the file, I do as follow:
1. Use "Base64Decoder" to get the encrypted file --> Pass successfully
2. Use the following:

Try using both functions:
FileSource f(in, true, new DefaultDecryptorWithMAC(passPhrase, new FileSink(out)));

FileSource f(in, true, new DefaultDecryptor(passPhrase, new FileSink(out)));

where:
in - is the input file (encrypted file)
passPharse - char* - location of the key file


but I always getting the following error:
terminate called after throwing an instance of 'CryptoPP::DefaultDecryptor::KeyBadErr'
what(): DefaultDecryptor: cannot decrypt message with this passphrase


Now when I'm trying to encrypt and decrypt by myself it works fine,
(I'm using the binary key he gave me)

1. FileSource f(in, true, new DefaultEncryptorWithMAC(passPhrase, new FileSink(out)));
2. FileSource(in, true, new Base64Encoder(new FileSink(out)));
3. FileSource(in, true, new Base64Decoder(new FileSink(out)));
4. FileSource f(in, true, new DefaultDecryptorWithMAC(passPhrase, new FileSink(out)));


Could anyone help me to understand what is my problem?
I'm guessing that the problem could be from 2 reasons:
1. Because this is a different algorithm form java AES128
2. There is a problem in the key, It can't read it right
 
Technology news on Phys.org
I don't know anything about the implementation you are using so I don't know whether your code is correct, but from my experience such problems are usually the result of using different padding schemes.

Also, since it's AES, make sure you are using the same mode (ECB, CBC, CTR...) and initialization vector, if appropriate.

I'm pretty sure one of these two is the problem if your program correctly decrypts the stuff it encrypts.
 
Last edited:

Similar threads

Replies
17
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K