BlockEncrypt() public method

public BlockEncrypt ( byte input, int inOffset, byte output, int outOffset ) : void
input byte
inOffset int
output byte
outOffset int
return void
コード例 #1
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");
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: Blowfish.cs プロジェクト: poderosaproject/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");
                        }
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// bcrypt_hash
        /// </summary>
        /// <param name="blowfish">blowfish object to use</param>
        /// <param name="sha2pass">SHA512 of password</param>
        /// <param name="sha2salt">SHA512 of salt</param>
        /// <returns></returns>
        private byte[] BcryptHash(Blowfish blowfish, byte[] sha2pass, byte[] sha2salt)
        {
            // this code is based on OpenBSD's bcrypt_pbkdf.c
            const int BLOCKSIZE = 8;

            // key expansion
            blowfish.InitializeState();
            blowfish.ExpandState(sha2pass, sha2salt);
            for (int i = 0; i < 64; ++i) {
                blowfish.ExpandState(sha2salt);
                blowfish.ExpandState(sha2pass);
            }

            // encryption
            byte[] cdata = (byte[])_bcryptCipherText.Clone();
            for (int i = 0; i < 64; ++i) {
                for (int j = 0; j < 32; j += BLOCKSIZE) {
                    blowfish.BlockEncrypt(cdata, j, cdata, j);
                }
            }

            // copy out
            for (int i = 0; i < 32; i += 4) {
                byte b0 = cdata[i + 0];
                byte b1 = cdata[i + 1];
                byte b2 = cdata[i + 2];
                byte b3 = cdata[i + 3];
                cdata[i + 3] = b0;
                cdata[i + 2] = b1;
                cdata[i + 1] = b2;
                cdata[i + 0] = b3;
            }

            return cdata;
        }