Beispiel #1
0
        public static byte[] ElectronicCodebookModeEncypt(byte[] plaintext, byte[] key)
        {
            List <byte> listByteCiphertext = new List <byte>();

            for (int i = 0; i < plaintext.Length / 16; i++)
            {
                byte[] bytesBlock = HelpfulFunctions.SubArrayDeepClone(plaintext, i * 16, 16);
                listByteCiphertext.AddRange(AESFunctions.AES_encrypt_block(bytesBlock, key));
            }

            return(listByteCiphertext.ToArray());
        }
Beispiel #2
0
        public static byte[] CipherBlockChainingModeEncypt(byte[] plaintext, byte[] key, byte[] IV)
        {
            List <byte> listByteCiphertext = new List <byte>();

            byte[] tmp = IV;
            for (int i = 0; i < plaintext.Length / 16; i++)
            {
                byte[] bytesBlock = HelpfulFunctions.SubArrayDeepClone(plaintext, i * 16, 16);
                bytesBlock = HelpfulFunctions.ExclusiveOR(bytesBlock, tmp);
                tmp        = AESFunctions.AES_encrypt_block(bytesBlock, key);
                listByteCiphertext.AddRange(tmp);
            }

            return(listByteCiphertext.ToArray());
        }
Beispiel #3
0
        public static byte[] CipherFeedbackModeDecrypt(byte[] ciphertext, byte[] key, byte[] IV)
        {
            List <byte> listBytePlaintext = new List <byte>();

            //TODO
            byte[] tmp = IV;
            for (int i = 0; i < ciphertext.Length / 16; i++)
            {
                byte[] bytesBlock       = HelpfulFunctions.SubArrayDeepClone(ciphertext, i * 16, 16);
                byte[] bytesFromEncrypt = AESFunctions.AES_encrypt_block(tmp, key);
                listBytePlaintext.AddRange(HelpfulFunctions.ExclusiveOR(bytesFromEncrypt, bytesBlock));
                tmp = bytesBlock;
            }

            return(listBytePlaintext.ToArray());
        }
Beispiel #4
0
        public static byte[] CounterModeEncypt(byte[] plaintext, byte[] key, byte[] nonce)
        {
            List <byte> listByteCiphertext = new List <byte>();

            byte[] counter = { 0, 0, 0, 0, 0, 0, 0, 0 };

            for (int i = 0; i < plaintext.Length / 16; i++)
            {
                List <byte> tmp = new List <byte>();
                tmp.AddRange(nonce);
                tmp.AddRange(counter);

                byte[] bytesFromAES = AESFunctions.AES_encrypt_block(tmp.ToArray(), key);
                byte[] bytesBlock   = HelpfulFunctions.SubArrayDeepClone(plaintext, i * 16, 16);
                listByteCiphertext.AddRange(HelpfulFunctions.ExclusiveOR(bytesBlock, bytesFromAES));

                counter = HelpfulFunctions.AddOneToByteArray(counter);
            }

            return(listByteCiphertext.ToArray());
        }