Exemplo n.º 1
0
 private static byte[] HexToByteArray(string text)
 {
     if (text.Length % 2 != 0)
     {
         return(null);
     }
     return(BigIntegerConverter.ParseHex(text));
 }
Exemplo n.º 2
0
        // Test of signing and verifying using test vectors
        // http://ed25519.cr.yp.to/python/sign.input
        internal static void Test()
        {
            using (var reader = new System.IO.StreamReader(@"sign.input")) {
                int skip  = 0;
                int count = 0;
                while (true)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    count++;
                    if (count <= skip)
                    {
                        continue;
                    }
                    System.Diagnostics.Debug.WriteLine("Line {0}", count);

                    string[] w          = line.Split(':');
                    byte[][] b          = w.Select(s => BigIntegerConverter.ParseHex(s)).ToArray();
                    byte[]   privateKey = new byte[32];
                    Buffer.BlockCopy(b[0], 0, privateKey, 0, 32);
                    byte[] publicKey = b[1];
                    byte[] message   = b[2];
                    byte[] signature = new byte[64];
                    Buffer.BlockCopy(b[3], 0, signature, 0, 64);

                    CurveEd25519 curve = new CurveEd25519();

                    byte[] sig;
                    if (!curve.Sign(privateKey, message, out sig))
                    {
                        throw new Exception("signing failed");
                    }
                    if (sig.Length != signature.Length)
                    {
                        throw new Exception("invalid sign length");
                    }
                    for (int i = 0; i < signature.Length; ++i)
                    {
                        if (sig[i] != signature[i])
                        {
                            throw new Exception("signs doesn't match");
                        }
                    }
                    if (!curve.Verify(publicKey, signature, message))
                    {
                        throw new Exception("verification failed");
                    }
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 public CurveEd25519()
 {
     _curveName          = "edwards25519";
     _publicKeyAlgorithm = PublicKeyAlgorithm.ED25519;
     _p = new BigInteger(BigIntegerConverter.ParseHex(
                             // 2^255 - 19
                             "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed"
                             ));
     _d = BigInteger.Parse(
         "37095705934669439343138083508754565189542113879843219016388785533085940283555"
         );
     _bx = BigInteger.Parse(
         "15112221349535400772501151409588531511454012693041857206046113283949847762202"
         );
     _by = BigInteger.Parse(
         "46316835694926478169428394003475163141307993866256225615783033603165251855960"
         );
     _l = new BigInteger(BigIntegerConverter.ParseHex(
                             // 2^252 + 0x14def9dea2f79cd65812631a5cf5d3ed
                             "1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed"
                             ));
 }
Exemplo n.º 4
0
        // 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");
                        }
                    }
                }
            }
        }