473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Crypto++5.4

180 100+
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
5 3730
DeMan
1,806 1GB
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
vermarajeev
180 100+
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
vermarajeev
180 100+
please help guys...

This is really important to me.....
Feb 19 '07 #4
DeMan
1,806 1GB
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
5,000 Expert 4TB
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

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

Similar topics

1
by: Stu | last post by:
Hi, Im reading a file in from disk as a byte array then passing it to a memory stream for decryption using crypto api functions. What I have found is that you need to reduce the array length by 2...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
13
by: Andy Chau | last post by:
I try to use RSA to implement the following scheme but wasn't sucessful. Sever encrypt a message using a public key, the client decrpyt the message using a private key. I don't want the client...
0
by: Slug | last post by:
Hello all, I've been trying to get a public key solution working but have been having a few problems. For starters there is a lot of contradictory information out there, MSDN is not much help,...
1
by: mirandacascade | last post by:
I am attempting to implement a process, and I'm pretty sure that a major roadblock is that I do not understand the nomenclature. The specs indicate that the goal is to calculate a message digest...
1
by: GiBo | last post by:
Hi I need some encryption done in my Python 2.5 application. I wonder what's the most recommended library? I've found M2crypto and some OpenSSL wrappers and Python Cryptography Toolkit and some...
2
by: vermarajeev | last post by:
Hi guys, I have written code to encrypt and decrypt files using perl script. Please help me to port below code to crypto++ library. //ENCRYPTION my $cipher = Crypt::CBC->new( -cipher =>...
12
by: Fett | last post by:
I need a crypto package that works on windows with python 2.5. Can anyone suggest one for me? I have been searching for a couple days for a good cryptography package to use for public/private...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.