I want to encrypt/decrypt a file
with AES in CTR mode using crypto++ library.
To encrypt a file using AES in CTR mode
the solution is something like this
Expand|Select|Wrap|Line Numbers
- int CRYPTOPP_API main(int argc, char *argv[])
- {
- std::string command, executableName, macFilename;
- if (argc < 2)
- command = 'h';
- else
- command = argv[1];
- if (command == "ae")
- AES_CTR_Encrypt(argv[2], argv[3], argv[4], argv[5]);
- }
- void AES_CTR_Encrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile)
- {
- SecByteBlock key = HexDecodeString(hexKey);
- SecByteBlock iv = HexDecodeString(hexIV);
- CTR_Mode<AES>::Encryption aes(key, key.size(), iv);
- FileSource(infile, true, new StreamTransformationFilter(aes, new FileSink(outfile)));
- }
In the command line if I pass
Expand|Select|Wrap|Line Numbers
- crypttest "2b7e151628aed2a6abf7158809cf4f3c" "000102030405060708090a0b0c0d0e0f" samp.txt encoded.txt
Expand|Select|Wrap|Line Numbers
- "2b7e151628aed2a6abf7158809cf4f3c" is my key;
- "000102030405060708090a0b0c0d0e0f" is my initialization vector;
Now suppose I have a password
const char* password = "somePassword";
Now I want the file to be encoded using this password. How do I do this?
I tried passing "password" as arg[2] in AES_CTR_Encrypt function, I get error telling
Expand|Select|Wrap|Line Numbers
- "CryptoPP::Exception caught: AES/CTR: 1 is not a valid key length"
vector. How do I calculate different IV values for different files???
Problem decrypting the same file
To decrypt the file with the same password, I tried defining
Expand|Select|Wrap|Line Numbers
- void AES_CTR_Decrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile)
- {
- SecByteBlock key = HexDecodeString(hexKey);
- SecByteBlock iv = HexDecodeString(hexIV);
- CTR_Mode<AES>::Decryption aes(key, key.size(), iv);
- FileSource(infile, true, new StreamTransformationFilter(aes, new FileSink(outfile)));
- }
What is that I need to decrypt a file????
How can I encrypt/decrypt a file with AES in CTR mode when I have a password????
You can refer to this link to get access to crypto++ lib
http://www.cryptopp.com/
Eagerly waiting for a reply.