Exemplo n.º 1
0
        public static byte[] DecryptIGE(byte[] ciphertext, byte[] key, byte[] iv)
        {
            var iv1 = new byte[iv.Length / 2];
            var iv2 = new byte[iv.Length / 2];

            Array.Copy(iv, 0, iv1, 0, iv1.Length);
            Array.Copy(iv, iv1.Length, iv2, 0, iv2.Length);

            AesEngine aes = new AesEngine();
            aes.Init(false, key);

            byte[] plaintext = new byte[ciphertext.Length];
            int blocksCount = ciphertext.Length / 16;

            byte[] ciphertextBlock = new byte[16];
            byte[] plaintextBlock = new byte[16];
            for (int blockIndex = 0; blockIndex < blocksCount; blockIndex++)
            {
                for (int i = 0; i < 16; i++)
                {
                    ciphertextBlock[i] = (byte)(ciphertext[blockIndex * 16 + i] ^ iv2[i]);
                }

                aes.ProcessBlock(ciphertextBlock, 0, plaintextBlock, 0);

                for (int i = 0; i < 16; i++)
                {
                    plaintextBlock[i] ^= iv1[i];
                }

                Array.Copy(ciphertext, blockIndex * 16, iv1, 0, 16);
                Array.Copy(plaintextBlock, 0, iv2, 0, 16);

                Array.Copy(plaintextBlock, 0, plaintext, blockIndex * 16, 16);
            }

            return plaintext;
        }
Exemplo n.º 2
0
        public static byte[] EncryptIGE(byte[] originPlaintext, byte[] key, byte[] iv) {
            
            byte[] plaintext;
            using (MemoryStream plaintextBuffer = new MemoryStream(originPlaintext.Length + 40)) { 
            //using(SHA1 hash = new SHA1Managed()) {
                //byte[] hashsum = hash.ComputeHash(originPlaintext);
                //plaintextBuffer.Write(hashsum, 0, hashsum.Length);
                plaintextBuffer.Write(originPlaintext, 0, originPlaintext.Length);
                while(plaintextBuffer.Position%16 != 0) {
                    plaintextBuffer.WriteByte(0); // TODO: random padding
                }
                plaintext = plaintextBuffer.ToArray();
            }

            var iv1 = new byte[iv.Length/2];
            var iv2 = new byte[iv.Length/2];

            Array.Copy(iv, 0, iv1, 0, iv1.Length);
            Array.Copy(iv, iv1.Length, iv2, 0, iv2.Length);

            AesEngine aes = new AesEngine();
            aes.Init(true, key);

            int blocksCount = plaintext.Length/16;
            byte[] ciphertext = new byte[plaintext.Length];

            byte[] ciphertextBlock = new byte[16];
            byte[] plaintextBlock = new byte[16];
            for(int blockIndex = 0; blockIndex < blocksCount; blockIndex++) {
                Array.Copy(plaintext, 16*blockIndex, plaintextBlock, 0, 16);

                //logger.info("plaintext block: {0} xor {1}", BitConverter.ToString(plaintextBlock).Replace("-", ""), BitConverter.ToString(iv1).Replace("-", ""));

                for(int i = 0; i < 16; i++) {
                        plaintextBlock[i] ^= iv1[i];
                }

                //logger.info("xored plaintext: {0}", BitConverter.ToString(plaintextBlock).Replace("-", ""));

                aes.ProcessBlock(plaintextBlock, 0, ciphertextBlock, 0);

                //logger.info("encrypted plaintext: {0} xor {1}", BitConverter.ToString(ciphertextBlock).Replace("-", ""), BitConverter.ToString(iv2).Replace("-", ""));

                for(int i = 0; i < 16; i++) {
                    ciphertextBlock[i] ^= iv2[i];
                }

                //logger.info("xored ciphertext: {0}", BitConverter.ToString(ciphertextBlock).Replace("-", ""));

                Array.Copy(ciphertextBlock, 0, iv1, 0, 16);
                Array.Copy(plaintext, 16*blockIndex, iv2, 0, 16);

                Array.Copy(ciphertextBlock, 0, ciphertext, blockIndex * 16, 16);
            }

            return ciphertext;
        }