コード例 #1
0
 public BlowfishCipher2(byte[] key)
 {
     Debug.Assert(key.Length==32);
     _bf = new Blowfish();
     _bf.initializeKey(key);
 }
コード例 #2
0
 public BlowfishCipher2(byte[] key, byte[] iv)
 {
     _bf = new Blowfish();
     _bf.SetIV(iv);
     _bf.initializeKey(key);
 }
コード例 #3
0
ファイル: Blowfish.cs プロジェクト: hanamiche/poderosa
        // Test using test vector
        // https://www.schneier.com/code/vectors.txt
        internal static void Test()
        {
            // Test ECB
            using (var reader = new System.IO.StreamReader(@"vectors.txt")) {
                string line;
                do
                {
                    line = reader.ReadLine();
                } while (line != null && !line.StartsWith("key bytes"));

                var blowfish = new Blowfish();
                int count    = 0;
                while (true)
                {
                    line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    string[] w = System.Text.RegularExpressions.Regex.Split(line, @"\s\s+");
                    if (w.Length < 3 || w[0].Length != 16 || w[1].Length != 16 || w[2].Length != 16)
                    {
                        break;
                    }
                    byte[] key    = BigIntegerConverter.ParseHex(w[0]);
                    byte[] clear  = BigIntegerConverter.ParseHex(w[1]);
                    byte[] cipher = BigIntegerConverter.ParseHex(w[2]);

                    ++count;
                    System.Diagnostics.Debug.WriteLine("Test ECB #{0}", count);

                    blowfish.InitializeKey(key);
                    for (int tries = 1; tries <= 3; ++tries)
                    {
                        byte[] encrypted = new byte[cipher.Length];
                        blowfish.BlockEncrypt(clear, 0, encrypted, 0);
                        for (int i = 0; i < cipher.Length; ++i)
                        {
                            if (encrypted[i] != cipher[i])
                            {
                                throw new Exception("encryption failed");
                            }
                        }
                    }
                    for (int tries = 1; tries <= 3; ++tries)
                    {
                        byte[] decrypted = new byte[clear.Length];
                        blowfish.BlockDecrypt(cipher, 0, decrypted, 0);
                        for (int i = 0; i < cipher.Length; ++i)
                        {
                            if (decrypted[i] != clear[i])
                            {
                                throw new Exception("decryption failed");
                            }
                        }
                    }
                }
            }

            // Test CBC
            {
                byte[] key = BigIntegerConverter.ParseHex("0123456789ABCDEFF0E1D2C3B4A59687");
                byte[] iv  = BigIntegerConverter.ParseHex("FEDCBA9876543210");
                // data: 37363534333231204E6F77206973207468652074696D6520666F722000 (29 bytes) + padding bytes (3 bytes)
                byte[] data   = BigIntegerConverter.ParseHex("37363534333231204E6F77206973207468652074696D6520666F722000000000");
                byte[] cipher = BigIntegerConverter.ParseHex("6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC");
                System.Diagnostics.Debug.WriteLine("Test CBC");

                {
                    Blowfish blowfish = new Blowfish();
                    blowfish.InitializeKey(key);
                    blowfish.SetIV(iv);

                    byte[] encrypted = new byte[cipher.Length];
                    blowfish.EncryptCBC(data, 0, data.Length, encrypted, 0);
                    for (int i = 0; i < cipher.Length; ++i)
                    {
                        if (encrypted[i] != cipher[i])
                        {
                            throw new Exception("encryption failed");
                        }
                    }
                }

                {
                    Blowfish blowfish = new Blowfish();
                    blowfish.InitializeKey(key);
                    blowfish.SetIV(iv);

                    byte[] decrypted = new byte[data.Length];
                    for (int i = 0; i < decrypted.Length; ++i)
                    {
                        decrypted[i] = 0xff;
                    }
                    blowfish.DecryptCBC(cipher, 0, cipher.Length, decrypted, 0);
                    for (int i = 0; i < data.Length; ++i)
                    {
                        if (decrypted[i] != data[i])
                        {
                            throw new Exception("decryption failed");
                        }
                    }
                }
            }
        }