コード例 #1
0
ファイル: OFB.cs プロジェクト: sonnythehottest/supercipher
        public byte[] decrypt()
        {
            //form shift register
            byte[] register = Encoding.ASCII.GetBytes(this.iv);
            byte[] ikey = new byte[register.Length];

            //start decryption
            Enkripsi enkripsi = new Enkripsi();

            Int32 blok = Encoding.ASCII.GetBytes(this.key).Length;
            byte[] cbyte = this.cipher;
            byte[] pbyte = new byte[cbyte.Length];

            for (int i = 0; i <= (cbyte.Length / blok); i++)
            {
                //get internal key
                ikey = enkripsi.encrypt(Encoding.ASCII.GetBytes(key), register);

                //get block-sized plaintext
                byte[] ci;
                byte[] pi;

                //ci = cbyte.Skip(i * blok).Take(cbyte.Length - (i * blok)).ToArray();

                //pi = xor(ci, ikey);

                if (cbyte.Length - (i * blok) >= blok)
                {
                    ci = new byte[blok];
                    pi = new byte[blok];
                    ci = cbyte.Skip(i * blok).Take(blok).ToArray();
                }
                else
                {
                    pi = new byte[cbyte.Length - (i * blok)];
                    ci = new byte[cbyte.Length - (i * blok)];
                    ci = cbyte.Skip(i * blok).Take(cbyte.Length - (i * blok)).ToArray();
                }

                ikey = ikey.Skip(0).Take(ci.Length).ToArray();

                pi = xor(ci, ikey);

                for (int m = 0; m < pi.Length; m++)
                {
                    pbyte[i * blok + m] = pi[m];
                }

                //wrap register
                register = ikey;
            }

            //this.plain = Encoding.ASCII.GetString(pbyte);
            this.plain = new byte[pbyte.Length];
            this.plain = pbyte;
            return this.plain;
        }
コード例 #2
0
ファイル: ECB.cs プロジェクト: sonnythehottest/supercipher
 public byte[] encrypt()
 {
     Enkripsi enkripsi = new Enkripsi();
     int leftover = plain.Length % key.Length;
     int blockTotal = plain.Length / key.Length;
     cipher = new byte[plain.Length + ((key.Length - leftover) % key.Length)];
     for (int i = 0; i < blockTotal; i++)
     {
         byte[] blockPlain = new byte [key.Length];
         for (int j = 0; j < key.Length; j++)
         {
             blockPlain[j] = plain[i * key.Length + j];
         }
         blockPlain = enkripsi.encrypt(blockPlain, Encoding.ASCII.GetBytes(key));
         for (int j = 0; j < key.Length; j++)
         {
             this.cipher[i * key.Length + j] = blockPlain[j];
         }
     }
     if (leftover > 0)
     {
         byte[] blockPlain = new byte[key.Length];
         byte paddingByte = 0;
         for (int i = 0; i < key.Length; i++)
         {
             if (i < leftover)
                 blockPlain[i] = plain[blockTotal * key.Length + i];
             else
                 blockPlain[i] = paddingByte;
         }
         blockPlain = enkripsi.encrypt(blockPlain, Encoding.ASCII.GetBytes(key));
         for (int i = 0; i < key.Length; i++)
         {
             this.cipher[blockTotal * key.Length + i] = blockPlain[i];
         }
     }
     return this.cipher;
 }