예제 #1
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());
        }
예제 #2
0
        public static byte[] PropagatingCipherBlockChainingModeDecrypt(byte[] ciphertext, byte[] key, byte[] IV)
        {
            List <byte> listBytePlaintext = new List <byte>();

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

            return(listBytePlaintext.ToArray());
        }
예제 #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());
        }
예제 #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());
        }
예제 #5
0
        public static byte[] PropagatingCipherBlockChainingMode2Decrypt(byte[] ciphertext, byte[] key, byte[] IV)
        {
            List <byte> listBytePlaintext = new List <byte>();

            byte[] tmp  = IV;
            byte[] tmp2 = { 1 };
            for (int i = 0; i < ciphertext.Length / 16; i++)
            {
                byte[] bytesBlock = HelpfulFunctions.SubArrayDeepClone(ciphertext, i * 16, 16);
                byte[] tmp2Copy   = tmp2;
                tmp2       = bytesBlock;
                bytesBlock = AESFunctions.AES_Decrypt_block(bytesBlock, key);
                if (i != 0)
                {
                    bytesBlock = HelpfulFunctions.ExclusiveOR(bytesBlock, tmp2Copy);
                }
                tmp = HelpfulFunctions.ExclusiveOR(bytesBlock, tmp);
                listBytePlaintext.AddRange(tmp);
            }

            return(listBytePlaintext.ToArray());
        }