예제 #1
0
        public KalynaBlock Decrypt(KalynaBlock cipherText)
        {
            var plainText = new KalynaBlock(cipherText);

            plainText.SubRoundKey(RoundsKeys[10]);

            plainText.MixColumns(StaticTables.MdsRev);

            plainText.ShiftRowsRev();

            plainText.SubBytes(StaticTables.kalynaInverseSBoxes);

            for (var i = 9; 1 <= i; --i)
            {
                plainText.Xor(RoundsKeys[i]);

                plainText.MixColumns(StaticTables.MdsRev);

                plainText.ShiftRowsRev();

                plainText.SubBytes(StaticTables.kalynaInverseSBoxes);
            }

            plainText.SubRoundKey(RoundsKeys[0]);
            return(plainText);
        }
예제 #2
0
 public void Xor(KalynaBlock key)
 {
     for (var i = 0; i < Data.Count; i++)
     {
         Data[i] ^= key.Data[i];
     }
 }
예제 #3
0
        public KalynaBlock Encrypt(KalynaBlock plainText)
        {
            var cipherText = new KalynaBlock(plainText);

            cipherText.AddRoundKey(RoundsKeys[0]);

            for (var i = 1; i <= 9; i++)
            {
                cipherText.SubBytes(StaticTables.kalynaForwardSBoxes);

                cipherText.ShiftRows();

                cipherText.MixColumns(StaticTables.Mds);

                cipherText.Xor(RoundsKeys[i]);
            }

            cipherText.SubBytes(StaticTables.kalynaForwardSBoxes);

            cipherText.ShiftRows();

            cipherText.MixColumns(StaticTables.Mds);

            cipherText.AddRoundKey(RoundsKeys[10]);

            return(cipherText);
        }
예제 #4
0
        public byte[] Decrypt(byte[] chiphertext)
        {
            KalynaBlock chipherBlock = new KalynaBlock(chiphertext);
            KalynaBlock plainBlock   = Decrypt(chipherBlock);

            {
                plainBlock.Data = new List <byte>(chiphertext);
                var t = plainBlock.Data[0];
                plainBlock.Data[0]  = plainBlock.Data[15];
                plainBlock.Data[15] = t;
            }

            return(plainBlock.Data.ToArray());
        }
예제 #5
0
        public byte[] Encrypt(byte[] plaintext)
        {
            KalynaBlock plainBlock   = new KalynaBlock(plaintext);
            KalynaBlock chipherBlock = Encrypt(plainBlock);

            {
                chipherBlock.Data = new List <byte>(plaintext);
                var t = chipherBlock.Data[0];
                chipherBlock.Data[0]  = chipherBlock.Data[15];
                chipherBlock.Data[15] = t;
            }

            return(chipherBlock.Data.ToArray());
        }
예제 #6
0
        public void SubRoundKey(KalynaBlock key)
        {
            const int n = 8;

            for (var i = 0; i < 16; i += n)
            {
                var dataBi = new BigInteger(Data.Where((d, idx) => i <= idx && idx < i + n).ToArray());
                dataBi -= new BigInteger(key.Data.Where((d, idx) => i <= idx && idx < i + n).ToArray());
                var newData = dataBi.ToByteArray();
                for (var j = 0; j < n; j++)
                {
                    Data[i + j] = j < newData.Length ? newData[j] : (byte)0;
                }
            }
        }
예제 #7
0
        public void GenerateRoundsKeys()
        {
            for (var i = 0; i <= 10; i++)
            {
                RoundsKeys.Add(new KalynaBlock());
            }

            var kt = GenerateKt();

            for (var i = 0; i <= 10; i += 2)
            {
                var roundKey = RoundsKeys[i];
                roundKey.Data = new List <byte>(StaticTables.V);
                roundKey.ShiftLeft(i / 2);

                var keyCopy = new KalynaBlock(Key);
                keyCopy.RotateRight(32 * i);

                roundKey.AddRoundKey(kt);
                var copy = new KalynaBlock(roundKey);

                roundKey.AddRoundKey(keyCopy);

                roundKey.SubBytes(StaticTables.kalynaForwardSBoxes);

                roundKey.ShiftRows();

                roundKey.MixColumns(StaticTables.Mds);

                roundKey.Xor(copy);

                roundKey.SubBytes(StaticTables.kalynaForwardSBoxes);

                roundKey.ShiftRows();

                roundKey.MixColumns(StaticTables.Mds);

                roundKey.AddRoundKey(copy);

                RoundsKeys[i] = roundKey;
            }

            for (var i = 1; i <= 9; i += 2)
            {
                RoundsKeys[i].Data = RoundsKeys[i - 1].Data;
                RoundsKeys[i].RotateLeft(56);
            }
        }
예제 #8
0
        private KalynaBlock GenerateKt()
        {
            var kt = new KalynaBlock(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5 });

            kt.AddRoundKey(Key);

            kt.SubBytes(StaticTables.kalynaForwardSBoxes);

            kt.ShiftRows();

            kt.MixColumns(StaticTables.Mds);

            kt.Xor(Key);

            kt.SubBytes(StaticTables.kalynaForwardSBoxes);

            kt.ShiftRows();

            kt.MixColumns(StaticTables.Mds);

            kt.AddRoundKey(Key);

            kt.SubBytes(StaticTables.kalynaForwardSBoxes);

            kt.ShiftRows();

            kt.MixColumns(StaticTables.Mds);

            kt = new KalynaBlock
            {
                Data = new List <byte>
                {
                    0x7D, 0xD8, 0xE2, 0x38, 0x2F, 0xBC, 0x5C, 0xD0,
                    0xA1, 0x5B, 0x77, 0x3B, 0x65, 0x1F, 0x2F, 0x86
                }
            };

            return(kt);
        }
예제 #9
0
 public KalynaBlock(KalynaBlock block)
 {
     Data = new List <byte>(block.Data);
 }
예제 #10
0
 public Kalyna(byte[] key)
 {
     Key = new KalynaBlock(key);
     GenerateRoundsKeys();
 }
예제 #11
0
        static void Main(string[] args)
        {
            //AES
            AESBlock input1 = new AESBlock(new byte[] { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });

            Console.WriteLine("AES:");
            Console.WriteLine("\tInput block: " + input1.ToString());

            AES      aes             = new AES(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
            AESBlock chipheredBlock1 = aes.Encrypt(input1);

            Console.WriteLine("\tEncripted block: " + chipheredBlock1.ToString());

            AESBlock output1 = aes.Decrypt(chipheredBlock1);

            Console.WriteLine("\tDecripted vlock: " + output1.ToString());


            //Kalyna
            KalynaBlock input2 = new KalynaBlock(new byte[] { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 });

            Console.WriteLine("Kalyna:");
            Console.WriteLine("\tInput block: " + input2.ToString());

            Kalyna      kalyna          = new Kalyna(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
            KalynaBlock chipheredBlock2 = kalyna.Encrypt(input2);

            Console.WriteLine("\tEncripted block: " + chipheredBlock2.ToString());

            KalynaBlock output2 = kalyna.Decrypt(chipheredBlock2);

            Console.WriteLine("\tDecripted vlock: " + output2.ToString());


            var rootpath    = Directory.GetCurrentDirectory();
            var inpFileName = "Lorem_ipsum.pdf";

            //AES
            {
                Console.WriteLine("AES:");
                FileChipherer chipherer = new FileChipherer(ChiphererAlgo.AES, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });

                Stopwatch stopwatch = new Stopwatch();

                stopwatch.Start();
                chipherer.Encrypt(Path.Combine(rootpath, inpFileName), Path.Combine(rootpath, "Encr_" + inpFileName));
                stopwatch.Stop();

                Console.WriteLine($"\tSpent {stopwatch.ElapsedMilliseconds} ms to encrypt");
                stopwatch.Reset();

                stopwatch.Start();
                chipherer.Decrypt(Path.Combine(rootpath, "Encr_" + inpFileName), Path.Combine(rootpath, "Decr_" + inpFileName));
                stopwatch.Stop();

                Console.WriteLine($"\tSpent {stopwatch.ElapsedMilliseconds} ms to decrypt");
            }

            //Kalyna
            {
                Console.WriteLine("Kalyna:");
                FileChipherer chipherer = new FileChipherer(ChiphererAlgo.Kalyna, new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });

                Stopwatch stopwatch = new Stopwatch();

                stopwatch.Start();
                chipherer.Encrypt(Path.Combine(rootpath, inpFileName), Path.Combine(rootpath, "Encr_" + inpFileName));
                stopwatch.Stop();

                Console.WriteLine($"\tSpent {stopwatch.ElapsedMilliseconds} ms to encrypt");
                stopwatch.Reset();

                stopwatch.Start();
                chipherer.Decrypt(Path.Combine(rootpath, "Encr_" + inpFileName), Path.Combine(rootpath, "Decr_" + inpFileName));
                stopwatch.Stop();

                Console.WriteLine($"\tSpent {stopwatch.ElapsedMilliseconds} ms to decrypt");
            }

            Console.ReadKey();
        }