Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Data Compression and Data Encryption

, 10 Aug 2006
Data Compression and Data Encryption
CompressionEncryption_src.zip
Data Compression and Encryption
Data Compression and Encryption
Properties
bin
Release
Data Compression and Encryption.dll
Data Compression and Encryption.pdb
Data Compression and Encryption.suo
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Data_Compression_and_Encryption
{
    class DataEncryption
    {
        private static byte[] ConvertToBytes(string text)
        {
            return System.Text.Encoding.Unicode.GetBytes(text);
        }
        private static string ConvertToText(byte[] ByteAarry)
        {
            return System.Text.Encoding.Unicode.GetString(ByteAarry);
        }
        private static BitArray ToBits(byte[] Bytes)
        {
            BitArray bits = new BitArray(Bytes);
            return bits;
        }
        private static BitArray SubBits(BitArray Bits, int Start, int Length)
        {
            BitArray half = new BitArray(Length);
            for (int i = 0; i < half.Length; i++)
                half[i] = Bits[i+Start];
            return half;
        }
        private static BitArray ConcateBits(BitArray LHH, BitArray RHH)
        {
            BitArray bits = new BitArray(LHH.Length + RHH.Length);
            for (int i = 0; i < LHH.Length; i++)
                bits[i] = LHH[i];
            for (int i = 0; i < RHH.Length; i++)
                bits[i+LHH.Length] = RHH[i];
            return bits;
        }
        
        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;
        }
        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;
        }
        public static string Encrypt(string Text)
        {
            byte[] b = DataEncryption.ConvertToBytes(Text);
            b = DataEncryption.Encrypt(b);
            return DataEncryption.ConvertToText(b);
        }
        public static string Decrypt(string EncryptedText)
        {
            byte[] b = DataEncryption.ConvertToBytes(EncryptedText);
            b = DataEncryption.Decrypt(b);
            return DataEncryption.ConvertToText(b);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

mohamed.elbatah
Web Developer
Egypt Egypt
No Biography provided

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