By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,411 Members | 981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,411 IT Pros & Developers. It's quick & easy.

Crypto++5.4

100+
P: 180
Hi guys,
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
  1. int CRYPTOPP_API main(int argc, char *argv[])
  2. {
  3.  std::string command, executableName, macFilename;
  4.  
  5.  if (argc < 2)
  6.   command = 'h';
  7.  else
  8.   command = argv[1];
  9.  
  10.  if (command == "ae")
  11.  AES_CTR_Encrypt(argv[2], argv[3], argv[4], argv[5]);
  12. }
  13.  
  14. void AES_CTR_Encrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile)
  15. {
  16.   SecByteBlock key = HexDecodeString(hexKey);
  17.   SecByteBlock iv = HexDecodeString(hexIV);
  18.   CTR_Mode<AES>::Encryption aes(key, key.size(), iv);
  19.   FileSource(infile, true, new StreamTransformationFilter(aes, new FileSink(outfile)));
  20. }
This is the solution as per given in crypto++ lib

In the command line if I pass

Expand|Select|Wrap|Line Numbers
  1. crypttest "2b7e151628aed2a6abf7158809cf4f3c" "000102030405060708090a0b0c0d0e0f" samp.txt encoded.txt
where
Expand|Select|Wrap|Line Numbers
  1. "2b7e151628aed2a6abf7158809cf4f3c" is my key;
  2. "000102030405060708090a0b0c0d0e0f" is my initialization vector;
The file is encoded fine.

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
  1. "CryptoPP::Exception caught: AES/CTR: 1 is not a valid key length"
Also I have problem using argv[3] in AES_CTR_Encrypt function which is initialization
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
  1. void AES_CTR_Decrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile)
  2. {
  3.   SecByteBlock key = HexDecodeString(hexKey);
  4.   SecByteBlock iv = HexDecodeString(hexIV);
  5.   CTR_Mode<AES>::Decryption aes(key, key.size(), iv);
  6.   FileSource(infile, true, new StreamTransformationFilter(aes, new FileSink(outfile)));
but I dont get back the original file.

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.
Feb 18 '07 #1
Share this Question
Share on Google+
5 Replies


DeMan
100+
P: 1,806
Hiya,

What is in your samp.text file? It appears to me that you may inadvertently be passing in the wrong parameters -> specifically that

AES_CTR_Encrypt(argv[2], argv[3], argv[4], argv[5]);

should be

AES_CTR_Encrypt(argv[1], argv[2], argv[3], argv[4]);

.Alternatively, I think the call may be correct, but you may have missed an input parameter -> it appears you expect the encryption type to be specified (as "ae"), but you never give that on the command line (which is a little odd since you definitely seem to be calling something in the Crypto Library (judging by the error).
Feb 18 '07 #2

100+
P: 180
Hiya,

What is in your samp.text file? It appears to me that you may inadvertently be passing in the wrong parameters -> specifically that

AES_CTR_Encrypt(argv[2], argv[3], argv[4], argv[5]);

should be

AES_CTR_Encrypt(argv[1], argv[2], argv[3], argv[4]);

.Alternatively, I think the call may be correct, but you may have missed an input parameter -> it appears you expect the encryption type to be specified (as "ae"), but you never give that on the command line (which is a little odd since you definitely seem to be calling something in the Crypto Library (judging by the error).
Thanks for your reply.
My input file "samp.text" contains
Expand|Select|Wrap|Line Numbers
  1. # connection table version 2
  2. 35   0  0    3  0  0  0  0  0       1.2063            0.4  0  1
  3. 17   0  0    4  0  0  0  0  0            0        0.41875  0  1
  4. 6    2  1    1  4  4  0  0  0       0.9625         0.0125  0  1
  5. 6    2  1    2  3  3  0  0  0          0.4              0  0  1
  6. CIS 1 2
I have passed
crypttest ae key vi samp.text Encoded.text in the command line
where
crypttest-->program name -->argv[0]
ae-->to ensure that aes algorithm is executed -->argv[1]
key-->key passed as shown in above thread -->argv[2]
vi--> Initialization Vector -->argv[3]
samp.text-->Input fileName -->argv[4]
Encoded.text-->Output FileName -->argv[5]

I have no problems passing input parameters. I need to know when I have a password how do I calculate key and IV based on password?????

I have a password say "somepassword"
Now using this "somepassword" how can I calculate key and IV ????

Please see the code provided by crypto++ library..You will get a clear idea about what I want.. See usage.txt file to see " Encryption on AES in CTR mode "

I also want to know how I can decrypt the file, as crypto++ lib provides function only for encryption.
Feb 19 '07 #3

100+
P: 180
please help guys...

This is really important to me.....
Feb 19 '07 #4

DeMan
100+
P: 1,806
Have you tried padding the password (with some sort of data) to be the same length as the key you passed earlier. It's interesting that the key length there is 32 bytes (which being a power of 2 makes me think it has to be that size)

Try padding it out to the right length. I'm not familiar enough with AES to be able to say whether padding it out with 0x0 or 0xff is enough, or whether this makes for a WEAK cipher.

You could try to create a string that is 32 bytes long and use the first (or last)
32 - n (where n is your password length) characters to pad it out.
Feb 20 '07 #5

RedSon
Expert 5K+
P: 5,000
You probably need to hash your password first to make it the correct length. Then use this hash as the key, then when you again enter your password to decrypt has the password again and then decrypt with that hash.
Feb 20 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.