Beispiel #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);
        }
Beispiel #2
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();
     }
 }
Beispiel #3
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();
     }
 }