Example #1
0
    static void TestBlowfish()
    {
        if (!Blowfish.SelfTest())
        {
            System.Console.WriteLine("selftest failed.");
            return;
        }

        System.Console.WriteLine("selftest passed.");

        byte[] key = new byte[16];

        for (byte bI = 0; bI < key.Length; bI++)
        {
            key[bI] = bI;
        }

        Blowfish bf = new Blowfish(key);

        System.Console.WriteLine((bf.IsWeakKey) ? "weak key detected." :
                                 "no weak key.");

        String sTest = "this is something to encrypt";

        System.Console.WriteLine(sTest);

        byte[] plainText = StringToBlocks(sTest);

        byte[] cipherText = new byte[plainText.Length];

        bf.Encrypt(plainText, cipherText, 0, 0, plainText.Length);

        System.Console.WriteLine(BlocksToString(cipherText));

        bf.Decrypt(cipherText, cipherText, 0, 0, cipherText.Length);

        System.Console.WriteLine(BlocksToString(cipherText));

        int nI, nSize = Blowfish.BLOCKSIZE * BIGBUFDIM;

        byte[] bigBuf = new byte[nSize];
        for (nI = 0; nI < nSize; nI++)
        {
            bigBuf[nI] = (byte)nI;
        }

        System.Console.WriteLine("benchmark running ...");

        long lTm = DateTime.Now.Ticks;

        for (nI = 0; nI < TESTLOOPS; nI++)
        {
            bf.Encrypt(bigBuf, bigBuf, 0, 0, nSize);

            if ((nI & 0x0f) == 0)
            {
                System.Console.Write(".");
            }
        }

        lTm = DateTime.Now.Ticks - lTm;

        lTm /= 10000;

        System.Console.WriteLine("\n{0} bytes in {1} millisecs",
                                 TESTLOOPS * nSize,
                                 lTm);

        long lSize = (long)nSize * 1000 * TESTLOOPS;

        lSize /= lTm;

        System.Console.WriteLine("(average of {0} bytes per second)", lSize);

        bf.Burn();
    }