Click here to Skip to main content
Email Password   helpLost your password?

Introduction

The Crypto++ library is a freeware library of cryptographic schemes, written by Wei Dai. However the library also contains other useful classes which are not immediately apparent when you use the library. Two of these are the Gzip and Gunzip classes which can be used to compress and decompress (zip and unzip) data.

Compression

Compressing your data could not be simpler. Say you had data pData that was of size dwLen that you wished to compress.

#include <gzip.h>


Gzip zipper(1);    // 1 is fast, 9 is slow


  zipper.Put(pData,dwLen);
  zipper.Close();

  byte* pCompressed = new byte[zipper.MaxRetrieveable()];
  zipper.Get(pCompressed,zipper.MaxRetrieveable());

pCompressed now contains the compressed data.

Decompression

You may not be surprised to know that decompression is just as easy. (dwLen is now the length of our compressed data)

Gunzip unzipper;

  unzipper.Put(pCompressedData,dwLen);
  unzipper.Close();

  byte* pData = new byte[unzipper.MaxRetrieveable()];
  unzipper.Get(pData,unzipper.MaxRetrieveable());

pData now contains the uncompressed data.

Remarks

Thanks to Wei Dai for his permission to write this article in what is hoped to be a series of articles on the use of his Crypto++ library.

Latest versions

The latest versions of the Crypto++ library can be found here.
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Questionpassword protected archive?
Matthias Blum
14:23 15 Jan '09  
Hey,
does the gzip.h also support encrypted archives?
Or how can I encrypt a big (~700MB) archive as fast as possible?

Thanks,
Matthias
AnswerRe: password protected archive?
Shaun Wilde
1:06 16 Jan '09  
It's been 8 years since I last had to use this 3rd party library, you will have to look up for yourself.

I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
BizSquawk

GeneralCompressing and Decompressing Data using C++
jassshan
22:39 24 Nov '08  
Halo,do someone have Compressing and Decompressing Data using C++ source code.IF u guys n girls have,can u all sent me the source code.my email at whocares1717@hotmail.com.thx
GeneralGzip a set of files
sheyliger
4:21 7 Jul '08  
Can this be used to gzip/gunzip a set of files? If so, does anyone have an example?
GeneralRe: Gzip a set of files
Shaun Wilde
9:35 7 Jul '08  
Try downloading the latest source code there may be sample source code there.

The API is quite rich and you could probably do what you want by chaining various classes together.

I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
www.many-monkeys.com

GeneralWell I've added header but it doesn't work
Noagene
22:36 15 Aug '07  
If i add only header file, it compiles well.
But if i add 'Gzip zipper(1);' code, compiler puts error message
why does this happen?
I'm using Crypto++ 5.2.1 library..
GeneralRe: Well I've added header but it doesn't work
Shaun Wilde
0:28 19 Aug '07  
What error message would that be?

I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
www.many-monkeys.com

GeneralRe: Well I've added header but it doesn't work
Noagene
14:38 19 Aug '07  
It says Gzip is a undeclared identifier..

Even though I included the header.
GeneralError code has been changed..
Noagene
19:55 19 Aug '07  
'CryptoPP::Gzip::Gzip(CryptoPP::BufferedTransformation *,unsigned int,unsigned int,bool)' : cannot convert argument 1 'int' to 'CryptoPP::BufferedTransformation *'


So it says Gzip zipper(1) is wrong.. how should i change it??
thx for your help.
AnswerRe: Error code has been changed..
butuzov_Ilya
23:27 6 Sep '07  
Try Gzip zipper(NULL,1);
GeneralClose() is No Longer Available...
Jeffrey Walton
16:21 27 May '07  
Which breaks Get() code for some unknown reason (to me at this point).

The work around seems to be to use Gunzip as Wei demonstrates in test.cpp:

void GunzipFile(const char *in, const char *out)
{
FileSource(in, true, new Gunzip(new FileSink(out)));
}

However, this means at times a double buffer space is required - one for the string, and the one for a BYTE[] if used.


GeneralRe: Close() is No Longer Available...
Shaun Wilde
11:04 28 May '07  
I assume Close from the Crypto++ library is not longer available?

Well libraries/frameworks change I suppose.

I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
www.many-monkeys.com

GeneralRe: Close() is No Longer Available...
Jeffrey Walton
12:03 28 May '07  
Hi Shaun,

It's nice to see you monitor your articles. BTW, I cited you in one of my articles: Tamper Aware and Self Healing Code[^]

Jeff

GeneralRe: Close() is No Longer Available...
Shaun Wilde
20:55 29 May '07  

Jeffrey Walton wrote:
It's nice to see you monitor your articles
I do try, but one is getting old and forgetful.



I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
www.many-monkeys.com

GeneralRe: Close() is No Longer Available...
Jeffrey Walton
12:08 28 May '07  
Hi Shaun,

I also forgot to mention that I saw you solution (after the post) in Close': is not a member of 'CryptoPP::Gzip[^].

I opted _not_ to maintain the past...

Jeff

GeneralRe: Close() is No Longer Available...
Shaun Wilde
20:56 29 May '07  
I forgot I even posted a solution - even though if I actually looked at the rest of the messages it would have been apparent. Must check my glasses as well.

I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. - pTerry
www.many-monkeys.com

GeneralCrypto++ Integration Article
Jeffrey Walton
19:28 8 Dec '06  
Hi Shaun,

I'm not trying to steal your thunder...

http://www.codeproject.com/useritems/CryptoPPIntegration.asp[^]

If you would be so kind as to add some of your hints, it would be very appreciated.

Jeff
GeneralGzip work verry greatly!
dungbkhn
18:50 28 Jun '05  
Laugh Rose
GeneralGot a wrong result!
JerryCheng
23:44 11 Nov '03  
In my thread, I have a Gzip instance. So, each time after I capture the desktop image, I use folowing code to compress the buffer just like the example:
while(1)
{
...
//some code used to capture the desktop image
zipper.Put((BYTE*)Buffer, Buflen)
zipper.Close();

size = zipper.MaxRetrieveable();
//some other manipulate
...
}
BUT, when run the codes second time, I got a ZERO size ,that's to say ,the varible szie equal 0!!!

Who can help me ???
GeneralRe: Got a wrong result!
Shaun Wilde
9:13 12 Nov '03  
withou a full sample that exhibits the problem its hard to work out the problem.

You might want to create a new zipper object in each loop so that it starts afresh each time.

I don't use the crypto++ library at the mo as I am in a pure .NET environment.

I am using VB on the current projectFrown

www.many-monkeys.com
GeneralRe: Got a wrong result!
JerryCheng
17:59 13 Nov '03  
Yes, after I moved the codes in while loop to a function, it worked!
But, there has a new problem. If I just only use the UNZIP instance to unzip a data buffer zipped by ZIP in another program, each time when I run unzipper.Close(); I got an exception said: Unhandle exception in **.zip(KERNAL32.DLL):0xE06D7363: Mirosoft C++ Exception.
Why? Must I use ZIP and UNZIP together? Or I can't use UNZIP separetly?
Thank you for your kind to answer my questions!
GeneralRe: Got a wrong result!
Shaun Wilde
5:43 14 Nov '03  
you should be able to - this is one for the crypto++ people.

You could try stepping into the code and seeing where in the crypto++ code it fails exactly.

I am using VB on the current projectFrown

www.many-monkeys.com
Generalencryption of file..help??
xxhimanshu
21:40 11 Nov '03  
Hi,
I have read many places and found that Crypto++ functions are very much compatible to php mcrypt library. I never knew how to use this in VC++ application. You have dobe such a wonderful work and its so easy to understand that i can compile and run your code easily. But i am having a small problem and hope that you can solve it out. I have to use the mcrypt library of php to encrypt a file at server and i have to decrypt the same file on client side using VC++. I am using Rijndael algorithm for encryption at server side through PHP. as the documentation says...
mcrypt's MCRYPT_RIJNDAEL_128 is compatible with cryptopp-php's CRYPTOPP_CIPHER_AES/ CRYPTOPP_CIPHER_RIJNDAEL, which are just aliases for each other. and
MCRYPT_MODE_CBC in mcrypt and CRYPTOPP_MODE_CBC in cryptopp-php.

MCRYPT_MODE_ECB in mcrypt and CRYPTOPP_MODE_ECB in cryptopp-php.
Can you tell me how to use crypto++ to do the same. or show me some piece of code to make it easy to understand..I need it urgently..Please help in doing the same. Any help or pointers are highly appreciated.
Thanks a lot in advance..
Confused Confused
Regards,

Himanshu
GeneralRe: encryption of file..help??
Shaun Wilde
9:17 12 Nov '03  
Your best bet is to post this to the crypto++ mailing list however as I remember there few samples in the library but the test harness they produce should give you pointers.

I currently don't use crypto++, and I don't have the latest version onmy machine.

I am using VB on the current projectFrown

www.many-monkeys.com
Generalzipping
Anonymous
9:24 1 Jun '03  
I want to know the algorithm of zipping the data and information.
I want to bild up the application like winzip or winrar.
Please tell me any logic for algorithm........


Last Updated 17 Apr 2001 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010