开发者

Byte array cryptography in C#

I want to create a nice cryptography using bitwise operators. However I fail to do so.

I want it to have bitwise operators using a byte array to encrypt and decrypt my b开发者_开发百科yte array.

public class Cryptographer
{
    private byte[] Keys { get; set; }

    public Cryptographer(string password)
    {
        Keys = Encoding.ASCII.GetBytes(password);
    }

    public void Encrypt(byte[] data)
    {
        for(int i = 0; i < data.Length; i++)
        {
            data[i] = (byte) (data[i] & Keys[i]);
        }
    }

    public void Decrypt(byte[] data)
    {
        for (int i = 0; i < data.Length; i++)
        {
            data[i] = (byte)(Keys[i] & data[i]);
        }
    }
}

I know this is wrong, thats why I need help. I simply want it to use 1 string to encrypt and decrypt all data.


This is what is sometimes known as 'craptography', because it provides the illusion of security while being functionally useless in protecting anything. Use the framework classes if you want to do cryptography right, because it's extremely difficult to roll your own.

Take a look at this for advice on what you are trying to do (encrypt/decrypt) - http://msdn.microsoft.com/en-us/library/e970bs09.aspx. Really your requirements should determine what classes you decide to use. This has good background: http://msdn.microsoft.com/en-us/library/92f9ye3s.aspx

For simple encrypt/decrypt (if this is what you need) DPAPI may be the simplest way.


You seem to be trying to implement the XOR cipher. XOR is ^ in C#:

public void Crypt(byte[] data)
{
    for(int i = 0; i < data.Length; i++)
    {
        data[i] = (byte) (data[i] ^ Keys[i]);
    }                             ↑
}

Since the Encrypt and Decrypt method do exactly the same, you need only one method.

Note, however, that this is just a toy and not suitable to secure data in real-world scenarios. Have a look at the System.Security.Cryptography Namespace which provides many implementations of proven algorithms. Using these correctly is still hard to get right though.


Use Xor ^ operator and not And &. Also you should not assume that data and key are the same length.

public class Cryptographer
{
    private byte[] Keys { get; set; }

    public Cryptographer(string password)
    {
        Keys = Encoding.ASCII.GetBytes(password);
    }

    public void Encrypt(byte[] data)
    {
        for(int i = 0; i < data.Length; i++)
        {
            data[i] = (byte) (data[i] ^ Keys[i % Keys.Length]);
        }
    }

    public void Decrypt(byte[] data)
    {
        for (int i = 0; i < data.Length; i++)
        {
            data[i] = (byte)(Keys[i % Keys.Length] ^ data[i]);
        }
    }
}


    static void Main(string[] args)
    {

        Int32 a = 138;
        Console.WriteLine("first int: " + a.ToString());

        byte[] bytes = BitConverter.GetBytes(a);

        var bits = new BitArray(bytes);
        String lol = ToBitString(bits);
        Console.WriteLine("bit int: " + lol);

        lol = lol.Substring(1, lol.Length - 1) + lol[0];
        Console.WriteLine("left   : " + lol);

        byte[] bytes_new = GetBytes(lol);

        byte[] key = { 12, 13, 24, 85 };
        var bits2 = new BitArray(key);
        String lol2 = ToBitString(bits2);
        Console.WriteLine("key    : " + lol2);

        byte[] cryptedBytes = Crypt(bytes_new, key);
        var bits3 = new BitArray(cryptedBytes);
        String lol3 = ToBitString(bits3);
        Console.WriteLine("    XOR: " + lol3);

        byte[] deCryptedBytes = Crypt(cryptedBytes, key);
        var bits4 = new BitArray(cryptedBytes);
        String lol4 = ToBitString(bits4);
        Console.WriteLine("  DEXOR: " + lol4);

        int a_new = BitConverter.ToInt32(bytes_new, 0);
        Console.WriteLine("and int: " + a_new.ToString());

        Console.ReadLine();
    }

    public static byte[] Crypt(byte[] data, byte[] key)
    {
        byte[] toCrypt = data;
        for (int i = 0; i < toCrypt.Length; i++)
        {
            toCrypt[i] = (byte)(toCrypt[i] ^ key[i]);
        }
        return toCrypt;
    }

    private static String ToBitString(BitArray bits)
    {
        var sb = new StringBuilder();

        for (int i = bits.Count - 1; i >= 0; i--)
        {
            char c = bits[i] ? '1' : '0';
            sb.Append(c);
        }

        return sb.ToString();
    }

    private static byte[] GetBytes(string bitString)
    {
        byte[] result = Enumerable.Range(0, bitString.Length / 8).
            Select(pos => Convert.ToByte(
                bitString.Substring(pos * 8, 8),
                2)
            ).ToArray();

        List<byte> mahByteArray = new List<byte>();
        for (int i = result.Length - 1; i >= 0; i--)
        {
            mahByteArray.Add(result[i]);
        }

        return mahByteArray.ToArray();
    }


Remember, there is no such thing as a 'secure' cipher. Any encryption method that can be written can be broken. With that being said, using simple bitwise techniques for encryption is inviting a not too bright hacker to break your encryption. There are guys/gals that sit around all day long with nothing better to do. Use one of the encryption libraries that uses a large key and do something 'unusual' to that key before using it. Even so, remember, there are people employed and not employed to do nothing but break cryptographic messages all around the world; 24 by 7. The Germans thought they had an un-breakable system in WW II. They called it Enigma. Do some reading on it and you will discover that it was broken even before the war broke out!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜