Click here to Skip to main content
13,502,077 members
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 1 Jul 2008

Easy String Compression and Encryption

, 1 Jul 2008
Rate this:
Please Sign up or sign in to vote.
Class module to easily de-/compress and en-/decrypt big strings


This article discusses string compression with optional decent encryption with pure VB.NET code, and no external tools required.

It can easily be integrated into existing projects. As the code is kept simple, it's suitable for beginners and a conversion to C# can be done easily.


In need of a routine to quickly and safely deflate and inflate big strings, I searched the net for a solution. A comprehensive set of functionalities didn't show up, so I decided to write this class module, which encapsulates all the functionality needed to complete the task.

Using the Code

Although strings of any length can be applied to the process, the compression of short strings (i.e. 'Hello World!') is counterproductive as it results in even bigger compressed counterparts. The CompressionRatio property of the class tells you how effective the compression was. You can decide then, if you want to use the compressed string and if so, prefix and suffix can be automatically applied to it, to distinguish between compressed and uncompressed content afterwards.

Process overview:

Plain text -> to byte array -> gzip compression -> encryption -> to base64 string = shrinked text

shrinked text -> to byte array -> decryption -> gzip decompression -> to string = plain text

The code is simple to use. Here's the quick way to compress a string:

Dim CompStr As New clsCompressedString(System.Text.Encoding.UTF8)

CompStr.UnCompressed = "some large text content..."

MsgBox "The compressed string is: " & CompStr.Compressed

... and the way back:

Dim CompStr As New clsCompressedString(System.Text.Encoding.UTF8)

CompStr.Compressed = "..." '<- the previously compressed string

MsgBox "The uncompressed string is: " & CompStr.UnCompressed

Error handling is kept at the minimum. The class returns empty strings when fed with corrupt data or supplied with wrong passphrase.

Optional encryption is performed by utilizing the .NET built-in RijndaelManaged at maximum key length and simplified usage: You just need to provide a single passphrase for encryption and decryption. Encryption key and iv are generated based on the passphrase by using SHA256 and MD5 hash value generation.

The demo project shows all features available.

Points of Interest

With string conversions involved, text encoding has to be addressed properly. Otherwise some or all characters could get messed up in the process of compression/decompression, depending on what content you try to compress/decompress.

Why Not Use ICSharpCode.SharpZipLib?

Well, you can easily alter the compression routines in the class to use ZipLib. I experimented with that and it showed, that ZipLib ( is only up to 7% more efficient than the built in GZip. To get this slight better performance, you have to set ZipLib to the highest compression level (9). But that comes with a price: ZipLib at highest level is very slow compared to GZip and therefore takes several times longer to compress a huge string. So I prefer GZip for this task as it is fast, reliable and doesn't require to link to additional DLLs and I don't run into licensing and security issues by using comprehensive third party code.

Preferences could possibly change when it comes to binary file compression. Maybe then ZipLib outruns GZip - but binary file compression was not the assigned task in this case.


  • 1st July, 2008: This is the first version. Participate and help to optimize and extend the code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

AnswerAbout the decompressing bug in C# Pin
shenqing5-Feb-13 17:12
membershenqing5-Feb-13 17:12 
GeneralMy vote of 2 Pin
Mahdi Nejadsahebi4-Nov-12 0:57
memberMahdi Nejadsahebi4-Nov-12 0:57 
QuestionCan't decompress at client app after receive via .NET socket TCP Pin
Umesh Sharma Kota8-Oct-12 4:22
memberUmesh Sharma Kota8-Oct-12 4:22 
GeneralMy vote of 5 Pin
Umesh Sharma Kota7-Oct-12 13:38
memberUmesh Sharma Kota7-Oct-12 13:38 
Questionjava text compression Pin
Member 807347310-Jul-11 19:23
memberMember 807347310-Jul-11 19:23 
GeneralNice and useful article Pin
buitrunghieu20-Nov-09 12:04
memberbuitrunghieu20-Nov-09 12:04 
Questionis it possible in VB6? Pin
buffysabelle16-Nov-09 9:43
memberbuffysabelle16-Nov-09 9:43 
QuestionMini-Bug? Pin
Fridolin22-Sep-09 3:53
memberFridolin22-Sep-09 3:53 
AnswerRe: Mini-Bug? Pin
jessehogan8-Oct-09 6:52
memberjessehogan8-Oct-09 6:52 
GeneralRe: Mini-Bug? Pin
Member 443577521-Mar-10 8:49
memberMember 443577521-Mar-10 8:49 
GeneralThanks! Pin
Member 19516102-Jun-09 20:37
memberMember 19516102-Jun-09 20:37 
GeneralParadox Pin
Mr.PoorEnglish17-Jul-08 23:29
memberMr.PoorEnglish17-Jul-08 23:29 
GeneralRe: Paradox Pin
BTDex20-Jul-08 3:46
memberBTDex20-Jul-08 3:46 
GeneralRe: Paradox [modified] Pin
Mr.PoorEnglish20-Jul-08 10:44
memberMr.PoorEnglish20-Jul-08 10:44 
GeneralRe: Paradox Pin
BTDex22-Jul-08 13:09
memberBTDex22-Jul-08 13:09 
Generalok, I see Pin
Mr.PoorEnglish22-Jul-08 21:20
memberMr.PoorEnglish22-Jul-08 21:20 
GeneralGZip... Pin
Frankidoze5-Jul-08 4:32
memberFrankidoze5-Jul-08 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180417.1 | Last Updated 1 Jul 2008
Article Copyright 2008 by BTDex
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid