Example #1
0
        public void GenerateKey(byte akRule, bool[] bits, int timeSteps)
        {
            var rule = new AKRule(akRule);

            var key        = new List <bool>();
            var fixedIndex = new Random().Next() % bits.Length;

            for (int step = 0; step < timeSteps; step++)
            {
                key.Add(bits[fixedIndex]);
                var newBits = new bool[bits.Length];

                for (int i = 0; i < bits.Length; i++)
                {
                    var leftIndex  = i - 1 < 0 ? bits.Length - 1 : i - 1;
                    var rightIndex = i + 1 > bits.Length - 1 ? 0 : i + 1;

                    var neighbour = new[] { bits[leftIndex], bits[i], bits[rightIndex] };

                    newBits[i] = rule.GetTransformedValue(neighbour);
                }

                bits = newBits;
            }

            var keyArray = key.ToArray();

            var keyByte = new List <byte>();

            for (int i = 0; i < keyArray.Length; i += 8)
            {
                var end   = i + 8 > keyArray.Length - 1 ? keyArray.Length - 1 : i + 8;
                var sign  = new ArraySegment <bool>(keyArray, i, end - i).ToArray();
                var ascii = ByteHelper.ConvertBoolArrayToByte(sign);
                keyByte.Add(ascii);
            }
            KeyPrintableBytes = ByteHelper.ByteArrayToHex(keyByte.ToArray());
        }
Example #2
0
        public void Decrypt(string key, string crypt)
        {
            var keyBytes       = ByteHelper.HexToByteArray(key);
            var cryptBytes     = ByteHelper.HexToByteArray(crypt);
            var plaintextBytes = new byte[cryptBytes.Length];

            if (keyBytes.Length < cryptBytes.Length)
            {
                return;
            }

            for (int i = 0; i < cryptBytes.Length; i++)
            {
                var bitKey    = ByteHelper.ConvertByteToBoolArray(keyBytes[i]);
                var bitCrypt  = ByteHelper.ConvertByteToBoolArray(cryptBytes[i]);
                var plainText = new bool[8];
                for (int j = 0; j < 8; j++)
                {
                    plainText[j] = XOR(bitKey[j], bitCrypt[j]);
                }
                plaintextBytes[i] = ByteHelper.ConvertBoolArrayToByte(plainText);
            }
            Message = Encoding.ASCII.GetString(plaintextBytes);
        }
Example #3
0
 public AKRule(byte value)
 {
     _value = ByteHelper.ConvertByteToBoolArray(value);
 }