Ejemplo n.º 1
0
        public static byte[] Encode(StreamCtx ctx, byte[] data, bool encrypt)
        {
            MemoryStream instr  = null;
            MemoryStream outstr = null;

            byte[] buffer = null;
            try
            {
                instr  = new MemoryStream(data, false);
                outstr = new MemoryStream((data.Length - (data.Length % ctx.BlockSize())) + ctx.BlockSize());
                if (encrypt)
                {
                    Encrypt(ctx, instr, outstr);
                }
                else
                {
                    Decrypt(ctx, instr, outstr);
                }
                outstr.Seek(0L, 0);
                buffer = new byte[outstr.Length];
                outstr.Read(buffer, 0, buffer.Length);
            }
            finally
            {
                if (instr != null)
                {
                    instr.Close();
                }
                if (outstr != null)
                {
                    outstr.Close();
                }
            }
            return(buffer);
        }
Ejemplo n.º 2
0
        public static StreamCtx MakeStreamCtx(IBlockCipher ibc, byte[] key, byte[] iv, Mode mode)
        {
            StreamCtx ctx = new StreamCtx
            {
                Ibc = ibc
            };

            ctx.Ibc.InitCipher(key);
            ctx.IV   = iv;
            ctx.Mode = mode;
            ctx.MakeReadOnly();
            return(ctx);
        }
Ejemplo n.º 3
0
        // Methods
        public Encription()
        {
            this.plainText  = "";
            this.CipherText = "";
            int          keySize = 0x20;
            IBlockCipher aes     = AesFactory.GetAes();

            byte[] iv = new byte[aes.BlockSizeInBytes()];
            for (int i = 0; i < iv.Length; i++)
            {
                iv[i] = 0;
            }
            byte[] salt           = new byte[8];
            int    iterationCount = 0x400;
            long   num4           = DateTime.Now.Ticks;

            byte[] key = KeyGen.DeriveKey("ANIEncryptionLib", keySize, salt, iterationCount);
            num4        = DateTime.Now.Ticks - num4;
            this.aesNow = StreamCipher.MakeStreamCtx(aes, key, iv);
        }
Ejemplo n.º 4
0
 public static void Encrypt(StreamCtx ctx, Stream instr, Stream outstr)
 {
     if (instr.Length > 0L)
     {
         byte[] inb  = new byte[ctx.BlockSize()];
         byte[] iv   = new byte[ctx.BlockSize()];
         bool   flag = false;
         int    i    = 0;
         while (true)
         {
             i = instr.Read(inb, 0, ctx.BlockSize());
             if (i < 0)
             {
                 i = 0;
             }
             if ((i > 0) && !flag)
             {
                 flag = true;
                 if (ctx.Mode == Mode.CBC)
                 {
                     Array.Copy(ctx.IV, 0, iv, 0, iv.Length);
                 }
             }
             if ((i <= 0) || (i < inb.Length))
             {
                 PrepareLastBuffer(inb, i);
                 PrepareBuffer(ctx.Mode, inb, iv);
                 ctx.Ibc.Cipher(inb, iv);
                 outstr.Write(iv, 0, iv.Length);
                 break;
             }
             PrepareBuffer(ctx.Mode, inb, iv);
             ctx.Ibc.Cipher(inb, iv);
             outstr.Write(iv, 0, iv.Length);
         }
         outstr.Flush();
     }
 }
Ejemplo n.º 5
0
 public static void Decrypt(StreamCtx ctx, Stream instr, Stream outstr)
 {
     if (instr.Length > 0L)
     {
         byte[] inb     = new byte[ctx.BlockSize()];
         byte[] outb    = new byte[ctx.BlockSize()];
         int    num     = 0;
         bool   flag    = false;
         byte[] iv      = new byte[ctx.BlockSize()];
         bool   flag2   = false;
         byte[] buffer4 = new byte[ctx.BlockSize()];
         while (true)
         {
             num = instr.Read(inb, 0, inb.Length);
             if (num < 0)
             {
                 num = 0;
             }
             if ((num > 0) && (num != inb.Length))
             {
                 return;
             }
             if ((num > 0) && !flag)
             {
                 flag = true;
                 if (ctx.Mode == Mode.CBC)
                 {
                     Array.Copy(ctx.IV, 0, iv, 0, ctx.IV.Length);
                 }
             }
             if (num <= 0)
             {
                 if (!flag2)
                 {
                     return;
                 }
                 Array.Copy(buffer4, outb, outb.Length);
                 int num2 = outb[outb.Length - 1];
                 if (num2 > outb.Length)
                 {
                     return;
                 }
                 outstr.Write(outb, 0, outb.Length - num2);
                 break;
             }
             ctx.Ibc.InvCipher(inb, outb);
             if (ctx.Mode == Mode.CBC)
             {
                 PrepareBuffer(ctx.Mode, outb, iv);
                 Array.Copy(inb, iv, inb.Length);
             }
             if (!flag2)
             {
                 Array.Copy(outb, buffer4, outb.Length);
                 flag2 = true;
             }
             else
             {
                 outstr.Write(buffer4, 0, buffer4.Length);
                 Array.Copy(outb, buffer4, outb.Length);
             }
         }
         outstr.Flush();
     }
 }