Click here to Skip to main content
Click here to Skip to main content

Easy String Compression and Encryption

By , 1 Jul 2008
 

Introduction

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.

Background

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 (0.85.4.369) 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.

History

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

License

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

About the Author

BTDex
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerAbout the decompressing bug in C#membershenqing5 Feb '13 - 17:12 
GeneralMy vote of 2memberMahdi Nejadsahebi4 Nov '12 - 0:57 
QuestionCan't decompress at client app after receive via .NET socket TCPmemberUmesh Sharma Kota8 Oct '12 - 4:22 
GeneralMy vote of 5memberUmesh Sharma Kota7 Oct '12 - 13:38 
Questionjava text compressionmemberMember 807347310 Jul '11 - 19:23 
GeneralNice and useful articlememberbuitrunghieu20 Nov '09 - 12:04 
Questionis it possible in VB6?memberbuffysabelle16 Nov '09 - 9:43 
QuestionMini-Bug?memberFridolin22 Sep '09 - 3:53 
AnswerRe: Mini-Bug?memberjessehogan8 Oct '09 - 6:52 
GeneralRe: Mini-Bug?memberMember 443577521 Mar '10 - 8:49 
I don't think this is really a "mini-bug." And the fix described above doesn't seem to work in my VS2008 environment.
 
The problem is that if you're rebuilding a string (from a compressed archive for example) and you try to append to the end of a string padded with a million or so nulls...well let's just say you're pretty well screwed.
 
In Decompress(), changing
 
objMemStream.Position = objMemStream.Length - 5
 
to
 
objMemStream.Position = objMemStream.Length - 4
 

works in that it returns a decompressed size equal to the original with no null padding, data errors or data loss.
 
I haven't waded into the why of this yet but I'd guess the author was trying to adjust for 0 based vs. 1 based arrays/collections/streams and just hiccuped on this part. (I really wish I'd done my current project in c++ for this reason alone.)
 
Never the less, this is a very helpful class as it front ends a lot of the stream to byte to string and back again translation pain that Bill G. did not see fit to add himself.
 
If you're not comfortable messing with the original code and you've got WAY too much memory, you can hack a solution by using the String.Trim function as in:
 
Result = Result.Trim("")trim the return
 

Just don't let anyone see this or put your initials on it!
GeneralThanks!memberMember 19516102 Jun '09 - 20:37 
GeneralParadoxmemberMr.PoorEnglish17 Jul '08 - 23:29 
GeneralRe: ParadoxmemberBTDex20 Jul '08 - 3:46 
GeneralRe: Paradox [modified]memberMr.PoorEnglish20 Jul '08 - 10:44 
GeneralRe: ParadoxmemberBTDex22 Jul '08 - 13:09 
Generalok, I seememberMr.PoorEnglish22 Jul '08 - 21:20 
GeneralGZip...memberFrankidoze5 Jul '08 - 4:32 

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

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