Exemplo n.º 1
0
        public byte[] Decrypt(byte[] chiphertext)
        {
            AESBlock chipherBlock = new AESBlock(chiphertext);
            AESBlock plainBlock   = Decrypt(chipherBlock);

            return(plainBlock.data);
        }
Exemplo n.º 2
0
 public void AddRoundKey(AESBlock rkey)
 {
     for (int i = 0; i < BLOCK_SIZE; ++i)
     {
         data[i] ^= rkey.data[i];
     }
 }
Exemplo n.º 3
0
        public byte[] Encrypt(byte[] plaintext)
        {
            AESBlock plainBlock   = new AESBlock(plaintext);
            AESBlock chipherBlock = Encrypt(plainBlock);

            return(chipherBlock.data);
        }
Exemplo n.º 4
0
 public AESBlock(AESBlock block)
 {
     data = new byte[BLOCK_SIZE];
     for (int i = 0; i < BLOCK_SIZE; ++i)
     {
         data[i] = block.data[i];
     }
 }
Exemplo n.º 5
0
 private void GenerateSubKeys()
 {
     roundKeys[0] = new AESBlock(key);
     for (int i = 0; i < ROUND_NO; ++i)
     {
         roundKeys[i + 1] = new AESBlock(roundKeys[i]);
         roundKeys[i + 1].KeyTransform(roundCoefficient[i]);
     }
 }
Exemplo n.º 6
0
        public AES(byte[] key)
        {
            this.key = new AESBlock(key);

            for (int i = 0; i < ROUND_NO + 1; ++i)
            {
                roundKeys[i] = new AESBlock();
            }

            GenerateSubKeys();
        }
Exemplo n.º 7
0
        public AESBlock Decrypt(AESBlock chipherBlock)
        {
            AESBlock outputBlock = new AESBlock(chipherBlock);

            for (int r = 0; r < ROUND_NO; ++r)
            {
                outputBlock.AddRoundKey(roundKeys[ROUND_NO - r]);

                if (r > 0)
                {
                    outputBlock.inverseMixColumns();
                }

                outputBlock.InverseShiftRows();

                outputBlock.SubBytesInverse();
            }

            outputBlock.AddRoundKey(roundKeys[0]);

            return(outputBlock);
        }
Exemplo n.º 8
0
        public AESBlock Encrypt(AESBlock inputBlock)
        {
            AESBlock chipherBlock = new AESBlock(inputBlock);

            chipherBlock.AddRoundKey(roundKeys[0]);

            for (int r = 0; r < ROUND_NO; ++r)
            {
                chipherBlock.SubBytesForward();

                chipherBlock.ShiftRows();

                if (r < ROUND_NO - 1)
                {
                    chipherBlock.MixColumns();
                }

                chipherBlock.AddRoundKey(roundKeys[r + 1]);
            }

            return(chipherBlock);
        }
Exemplo n.º 9
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();
        }