Click here to Skip to main content
15,861,125 members
Articles / Programming Languages / C#
Article

Data Compression and Data Encryption

Rate me:
Please Sign up or sign in to vote.
3.09/5 (11 votes)
10 Aug 2006CPOL1 min read 65.1K   1.3K   32   5
Data Compression and Data Encryption

Introduction

This article is about data compression and data encryption.

Data Compression uses the Gzip algorithm in C# 2.0 and compresses, decompresses an array of bytes that can be converted to files by streams.

Data encryption uses a custom algorithm for encrypting and decrypting an array of bytes or strings which are useful when popular methods can be used by hackers.

Source Code

The source code contains two classes, one for compression and the other for encryption. The compression class is called DataCompression and contains two public methods for compress and decompress. The encryption class is called DataEncryption and contains two public methods for encrypt and decrypt.

Data Compression

The Compress method uses the gzip method to compress in memory data which can be converted to file using streams.

C#
public static byte[] Compress(byte[] bytData)
{
    try
    {
        MemoryStream ms = new MemoryStream();
        Stream s = new GZipStream(ms, CompressionMode.Compress);
        s.Write(bytData, 0, bytData.Length);
        s.Close();
        byte[] compressedData = (byte[])ms.ToArray();
        return compressedData;
    }
    catch
    {
        return null;
    }
}

The deCompress method uses the gzip method to decompress in memory data which can be converted to file using streams.

C#
public static byte[] DeCompress(byte[] bytInput)
{
    string strResult="";
    int totalLength = 0;
    byte[] writeData = new byte[4096];
    Stream s2 = new GZipStream(new MemoryStream(bytInput),CompressionMode.Decompress);
    while (true) 
    {
        int size = s2.Read(writeData, 0, writeData.Length);
        if (size > 0) 
        {
            totalLength += size;
            strResult+=System.Text.Encoding.Unicode.GetString(writeData, 0,size);
        } 
        else 
        {
            break;
        }
    }
    s2.Close();
    return Encoding.Unicode.GetBytes( strResult);
} 

Data Encryption

The encrypt method takes a string or array of bytes and returns a string or array of bytes as the input parameter.

This code takes input and divides it into two parts, does Xor between them and negates the right hand part and makes the encrypted output.

C#
 public static byte[] Encrypt(byte[] ordinary)
{
    BitArray bits = ToBits(ordinary);
    BitArray LHH=SubBits(bits,0,bits.Length/2);
    BitArray RHH = SubBits(bits, bits.Length / 2, bits.Length / 2);
    BitArray XorH = LHH.Xor(RHH);
    RHH = RHH.Not();
    XorH = XorH.Not();
    BitArray encr = ConcateBits(XorH, RHH);
    byte[] b = new byte[encr.Length/8];
    encr.CopyTo(b, 0);
    return b;
}

Or take the string and produce an encrypted string directly.

C#
public static string Encrypt(string Text)
{
    byte[] b = DataEncryption.ConvertToBytes(Text);
    b = DataEncryption.Encrypt(b);
    return DataEncryption.ConvertToText(b);
}

The Decrypt method takes the encrypted string or array of bytes and returns clear decrypted string or array of bytes as the type of input parameter.

This code takes input and reverses the process of encryption to decrypt the data.

C#
public static byte[] Decrypt(byte[] Encrypted)
{
    BitArray enc = ToBits(Encrypted);
    BitArray XorH = SubBits(enc, 0, enc.Length / 2);
    XorH = XorH.Not();
    BitArray RHH = SubBits(enc, enc.Length / 2, enc.Length/2 );
    RHH = RHH.Not();
    BitArray LHH = XorH.Xor(RHH);
    BitArray bits = ConcateBits(LHH, RHH);
    byte[] decr= new byte[bits.Length / 8];
    bits.CopyTo(decr, 0);
    return decr;
}

Or take the string and produce an encrypted string directly.

C#
public static string Decrypt(string EncryptedText)
{
    byte[] b = DataEncryption.ConvertToBytes(EncryptedText);
    b = DataEncryption.Decrypt(b);
    return DataEncryption.ConvertToText(b);
}

I hope this code is useful for you.

History

  • 10th August, 2006: Initial post

License

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


Written By
Web Developer
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralProject helps Pin
Member 1190206311-Mar-16 17:07
Member 1190206311-Mar-16 17:07 
NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 7:02
Tony Selke27-Sep-07 7:02 
Generalit don't works with 1.1 framework Pin
Dav Zen3-Jan-07 0:00
Dav Zen3-Jan-07 0:00 
AnswerRe: it don't works with 1.1 framework Pin
mohamed.elbatah30-Jan-07 2:48
mohamed.elbatah30-Jan-07 2:48 
GeneralData Compression and Data Encryption Pin
mosto10-Aug-06 15:50
mosto10-Aug-06 15:50 

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.