Пример #1
0
        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
        public void FeistelTest()
        {
            Enkripsi en = new Enkripsi();
            Dekripsi de = new Dekripsi();

            byte[] b = System.Text.Encoding.ASCII.GetBytes("Mata Kuliah Kriptografi.");
            String k = "12345678";
            en.generateAllInternalKey(k);
            de.generateAllInternalKey(k);

            CollectionAssert.AreEqual(b, de.feistelDecipher(en.feistel(b,en.internalKey),de.internalKey));
        }
Пример #3
0
 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;
 }
Пример #4
0
 public void TransposeSentenceTest()
 {
     Enkripsi en = new Enkripsi();
     Dekripsi de = new Dekripsi();
     byte[] b = System.Text.Encoding.ASCII.GetBytes("Mata Kuliah Kriptografi.");
     CollectionAssert.AreEqual(b, de.transpose(en.transpose(b)));
 }
Пример #5
0
 public void TransposeOneCharTest()
 {
     //
     // TODO: Add test logic here
     //
     Dekripsi de = new Dekripsi();
     Enkripsi en = new Enkripsi();
     byte[] b = System.Text.Encoding.ASCII.GetBytes("a");
     //Assert.AreEqual(b[0],c[0]);
     CollectionAssert.AreEqual(b,de.transpose(en.transpose(b)));
 }
Пример #6
0
 public void OverallTest()
 {
     Enkripsi en = new Enkripsi();
     Dekripsi de = new Dekripsi();
 }
Пример #7
0
 public void TransposeTwoCharTest()
 {
     Enkripsi en = new Enkripsi();
     Dekripsi de = new Dekripsi();
     byte[] b = System.Text.Encoding.ASCII.GetBytes("ab");
     CollectionAssert.AreEqual(b,de.transpose(en.transpose(b)));
 }