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

Data Compression and Data Encryption

, 10 Aug 2006
Rate this:
Please Sign up or sign in to vote.
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.

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.

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.

 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.

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.

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.

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)

About the Author

mohamed.elbatah
Web Developer
Egypt Egypt
No Biography provided

Comments and Discussions

 
Generalit don't works with 1.1 framework Pinmemberdreamer923-Jan-07 0:00 
AnswerRe: it don't works with 1.1 framework Pinmembermedo_elbatah30-Jan-07 2:48 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 10 Aug 2006
Article Copyright 2006 by mohamed.elbatah
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid