65.9K
CodeProject is changing. Read more.
Home

Data Compression and Data Encryption

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.09/5 (11 votes)

Aug 10, 2006

CPOL

1 min read

viewsIcon

66033

downloadIcon

1297

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