⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blowfishtests.cs

📁 source for advanced algorithm
💻 CS
📖 第 1 页 / 共 2 页
字号:
        {
            BlowfishCFB bfc = new BlowfishCFB();
            bfc.Initialize(OPENSSL_BFCFB_REFKEY, 0, OPENSSL_BFCFB_REFKEY.Length);
            bfc.IV = OPENSSL_BFCFB_REFIV;
            int len = OPENSSL_BFCFB_REFDATA.Length;
            byte[] buf = new byte[len];
            bfc.Encrypt(OPENSSL_BFCFB_REFDATA, 0, buf, 0, 13);    // (just for fun, the same way like in the C code)
            bfc.Encrypt(OPENSSL_BFCFB_REFDATA, 13, buf, 13, len - 13);
            Assert.IsTrue(len == OPENSSL_BFCFB_REFCTXT.Length);
            for (int i = 0; i < len; i++)
            {
                Assert.IsTrue(OPENSSL_BFCFB_REFCTXT[i] == buf[i]);
            }
        }
    }

    /// <summary>Test covering the BlowfishSimple class.</summary>
    [TestFixture]
    public class TestBlowfishSimple
    {
        /// <summary>Runs some standard tests for BlowfishSimple.</summary>
        [Test] 
        public void Test() 
        {
            int keylen, plainLen, i;
            BlowfishSimple bfs0, bfs1;
            StringBuilder sb;
            String key, plain, enc, dec;


            sb = new StringBuilder();

            for (keylen = 0; 
                 keylen < 10000; 
                 keylen += keylen + ((keylen & 1) ^ 1))
            {
                sb.Length = 0;
                for (i = 0; i < keylen; i++)
                {
                    sb.Append((char)i);
                }
                
                key = sb.ToString();

                for (plainLen = 0; plainLen < 10000; 
                    plainLen += plainLen + (plainLen & 1) ^ 1)
                {
                    sb.Length = 0;
                    for (i = 0; i < plainLen; i++)
                    {
                        sb.Append((char)i);
                    }

                    plain = sb.ToString();

                    bfs0 = new BlowfishSimple(key);
                    bfs1 = new BlowfishSimple();
                    bfs1.Initialize(key);

                    enc = bfs0.Encrypt(plain);
                    dec = bfs1.Decrypt(enc);

                    Assert.IsTrue(BlowfishSimple.VerifyKey(key, bfs0.KeyChecksum));
                    Assert.IsTrue(BlowfishSimple.VerifyKey(key, bfs1.KeyChecksum));

                    Assert.IsTrue(0 == dec.CompareTo(plain));

                    bfs0 = new BlowfishSimple(key + ' ');
            
                    Assert.IsTrue(BlowfishSimple.VerifyKey(key, bfs1.KeyChecksum));
                }
            }
        }
    }

    /// <summary>Tests the Java Interoperability classes of Blowfish.NET.</summary>
    [TestFixture] 
    public class TestJavaInterop
    {
        /// <summary>Tests string encryption with BlowfishEasy.</summary>
        [Test] 
        public void TestBlowfishEasy() 
        {
            int keyLen, plainLen, i;
            BlowfishEasy bfes0, bfes1;
            StringBuilder sb;
            String key, plain, enc, dec;

            sb = new StringBuilder();

            for (keyLen = 0; keyLen < 10000;
                keyLen += keyLen + ((keyLen & 1) ^ 1))
            {
                sb.Length = 0;
                for (i = 0; i < keyLen; i++)
                {
                    sb.Append((char)i);
                }

                key = sb.ToString();

                for (plainLen = 1; 
                     plainLen < 10000;
                     plainLen += plainLen + (plainLen & 1) ^ 1)
                {
                    sb.Length = 0;
                    for (i = 0; i < plainLen; i++)
                    {
                        sb.Append((char)((i % 500) + 32));
                    }

                    plain = sb.ToString();

                    bfes0 = new BlowfishEasy(key);
                    bfes1 = new BlowfishEasy(key);

                    enc = bfes0.EncryptString(plain);
                    dec = bfes1.DecryptString(enc);

                    Assert.IsTrue(0 == dec.CompareTo(plain));

                    bfes1 = new BlowfishEasy(key + "wrong");
                    dec = bfes1.DecryptString(enc);

                    Assert.IsTrue(
                        (null == dec) ||
                        (0 != dec.CompareTo(plain)));
                }
            }
        }

        /// <summary>Tests stream I/O with BlowfishEasy.</summary>
        [Test] 
        public void TestBlowfishStream() 
        {
            int i, tmp, ofs;
            byte[] key, plain, enc, dec;
            int[] KEYLENS = { 0, 1, 2, 8, 16, 32, 33, 49, 55, BlowfishECB.MAX_KEY_LENGTH };
            int[] DATALENS = { 0, 1, 2, 3, 7, 8, 9, 15, 16, 17, 1024, 103117 };
            BlowfishStream bfs;
            MemoryStream ms;

            foreach (int keyLen in KEYLENS)
            {   
                key = new byte[keyLen];
        
                for (i = 0; i < keyLen; i++)
                {
                    key[i] = (byte)i;
                }

                foreach (int dataLen in DATALENS)
                {
                    plain = new byte[dataLen];
        
                    for (i = 0; i < dataLen; i++)
                    {
                        plain[i] = (byte)i;
                    }
                    
                    ms = new MemoryStream();
                    bfs = BlowfishStream.Create(
                        ms, 
                        BlowfishStreamMode.Write, 
                        key, 
                        0, 
                        key.Length);
                    
                    ofs = 0;
                    while (ofs < dataLen)
                    {
                        bfs.Write(plain, ofs, 1);
                        ofs++;
                    }
                    bfs.Close();
    
                    enc = ms.ToArray();
                    ms = new MemoryStream(enc);
                    bfs = BlowfishStream.Create(
                        ms, 
                        BlowfishStreamMode.Read, 
                        key, 
                        0, 
                        key.Length);

                    dec = new byte[dataLen];
                    Array.Clear(dec, 0, dec.Length);
                    
                    ofs = 0;
                    while (ofs < dataLen)
                    {
                        Assert.IsTrue(-1 != (tmp = bfs.ReadByte()));
                        dec[ofs++] = (byte)tmp;
                    }
                    Assert.IsTrue(-1 == bfs.ReadByte());
                    bfs.Close();

                    for (i = 0; i < dataLen; i++)
                    {
                        Assert.IsTrue(plain[i] == dec[i]);
                    }
                }
            }
        }

        /// <summary>Data produced with the Java class BlowfishJ.BlowfishOutputStream.</summary>
        static readonly byte[] XCHG_DATA =
        {
            0x32, 0xe3, 0x5d, 0x37, 0xc6, 0xbe, 0xe1, 0xf8, 0x5a, 0x3f, 0x4c,
            0xf6, 0x2b, 0xe3, 0x69, 0x7e, 0xb8, 0x35, 0x65, 0xf8, 0xcf, 0x6d,
            0x11, 0xd0, 0xa0, 0xfc, 0x51, 0x0a, 0x2b, 0x95, 0x1c, 0x1f, 0x44,
            0xb5, 0xc5, 0xd4, 0x7a, 0x76, 0xb5, 0x91, 0x03, 0x29, 0x24, 0x60,
            0xe1, 0x67, 0xbe, 0x1a, 0xfe, 0xfb, 0x8c, 0xc5, 0x82, 0x60, 0xf6,
            0xe9, 0x17, 0xc3, 0xc6, 0x34, 0x3c, 0xd9, 0x3b, 0x45, 0x47, 0x54,
            0xf6, 0x4d, 0xbc, 0x09, 0x56, 0x8a, 0x69, 0x22, 0xe6, 0x69, 0x44,
            0x18, 0x94, 0x28, 0xae, 0xa5, 0x72, 0x30, 0x24, 0xaf, 0x14, 0x14,
            0xa6, 0x3b, 0x21, 0x85, 0xca, 0xab, 0x58, 0x26, 0xa3, 0x89, 0x9d,
            0x3f, 0x1e, 0x2a, 0x40, 0xe6, 0xe6, 0xad, 0x4d, 0x4e, 0x8d, 0xe3,
            0x6d, 0x0b, 0xfa, 0x81, 0x51, 0x31, 0xa8, 0x1a, 0xe2, 0x2a 
        };

        const int XCHG_DATA_SIZE = 111;
        static readonly byte[] XCHG_KEY = { 0xaa, 0xbb, 0xcc, 0x00, 0x42, 0x33 };

        /// <summary>Tests compatibility between BlowfishJ and Blowfish.NET streams.</summary>
        [Test]
        public void TestBlowfishJXchg()
        {
            int i;
            BlowfishStream bfs;

            bfs = BlowfishStream.Create(
                new MemoryStream(XCHG_DATA),
                BlowfishStreamMode.Read,
                XCHG_KEY,
                0,
                XCHG_KEY.Length);

            for (i = 0; i < XCHG_DATA_SIZE; i++)
            {
                Assert.IsTrue((i & 0x0ff) == bfs.ReadByte());
            }

            Assert.IsTrue(-1 == bfs.ReadByte());

            bfs.Close();    
        }
    }

    /// <summary>Tests for the BlowfishAlgorithm implementation.</summary>
    [TestFixture]
    public class TestBlowfishAlgorithm
    {
        static int[] SIZES = { 0, 1, 2, 3, 7, 8, 9, 15, 16, 17, 63, 129, 1023, 65536 };
        static int[] BUFSZS = { 1, 2, 3, 7, 8, 9, 15, 16, 17, 255, 256, 257 };

        /// <summary>Test using cryptostreams.</summary>
        [Test]
        public void TestCryptoStreams()
        {
            foreach (int size in SIZES)
            {
                foreach (int bufsz in BUFSZS)
                {
                    byte[] buf = new byte[bufsz];

                    for (int pad = 0; pad < 4; pad++)
                    {
                        PaddingMode padMode;
                        bool padPrecise = false;
                        switch (pad)
                        {
                            case 0 : padMode = PaddingMode.ANSIX923; padPrecise = true;  break;
                            case 1 : padMode = PaddingMode.ISO10126; break;
                            case 2 : padMode = PaddingMode.PKCS7; padPrecise = true; break;
                            default: padMode = PaddingMode.Zeros; break;
                        }

                        for (int mode = 0; mode < 2; mode++)
                        {
                            BlowfishAlgorithm bfa = new BlowfishAlgorithm();
                            bfa.Mode = 0 == mode ? CipherMode.ECB : CipherMode.CBC;
                            bfa.Padding = padMode;

                            bfa.GenerateKey();
                            byte[] key = bfa.Key;
                            Assert.IsTrue(56 == key.Length);
                            bfa.Key = key;
                            key = (byte[])key.Clone();

                            MemoryStream ms = new MemoryStream();
                            CryptoStream cs = new CryptoStream(ms, bfa.CreateEncryptor(), CryptoStreamMode.Write);

                            int i = 0;
                            while (i < size)
                            {
                                int towrite = size - i > bufsz ? bufsz : size - i;
                                for (int j = 0; j < bufsz; j++)
                                {
                                    buf[j] = (byte)i++;
                                }
                                cs.Write(buf, 0, towrite);
                            }
                            cs.Close();

                            byte[] enc = ms.ToArray();
                            Assert.IsTrue(size <= enc.Length);

                            bfa.Key = key;

                            ms = new MemoryStream(enc);
                            cs = new CryptoStream(ms, bfa.CreateDecryptor(), CryptoStreamMode.Read);
                            for (i = 0; i < size; i++)
                            {
                                int dval = cs.ReadByte();
                                Assert.IsFalse(-1 == dval);
                                Assert.IsTrue((i & 0xff) == dval);
                            }
                            if (padPrecise)
                            {
                                Assert.IsTrue(-1 == cs.ReadByte());
                            }
                            cs.Close();

                            ms = new MemoryStream(enc);
                            cs = new CryptoStream(ms, bfa.CreateDecryptor(), CryptoStreamMode.Read);

                            i = 0;
                            while (i < size)
                            {
                                int toread = size - i > bufsz ? bufsz : size - i;
                                Assert.IsTrue(toread == cs.Read(buf, 0, toread));
                                for (int j = 0; j < toread; j++)
                                {
                                    Assert.IsTrue(buf[j] == (byte)i++);
                                }
                            }
                            if (padPrecise)
                            {
                                Assert.IsTrue(-1 == cs.ReadByte());
                            }
                            cs.Close();
                        }
                    }
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -