public void TestSignAndVerify()
        {
            var handler   = new AsymmetricCryptoHandler();
            var hash      = Hash.Parse("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
            var signature = handler.SignHash(hash, _privateKey);

            Assert.NotNull(signature);

            var result = handler.VerifyHash(hash, signature, _publicKey);

            Assert.True(result);

            var maliciousHash = Hash.Parse("535ceaca7d4c98e083cd3392037ec740af7cb6ce19af76e0ac04698203ac1292");

            result = handler.VerifyHash(maliciousHash, signature, _publicKey);
            Assert.False(result);
        }
Ejemplo n.º 2
0
        private void btnGenerateKeys_Click(object sender, EventArgs e)
        {
            string path = Environment.CurrentDirectory + @"\" + _KEYS_FILENAME;

            if (!File.Exists(path))
            {
                File.Create(path).Close();
                StreamWriter file = new StreamWriter(path, false);
                file.WriteLine("; the asymmetric encryption will be used to store the key and iv of the");
                file.WriteLine("; symmetric protocol.");
                file.WriteLine("");
                file.WriteLine("; used for encryption");
                file.WriteLine("[Public]");
                file.WriteLine("Modulus=");
                file.WriteLine("Exponent=");
                file.WriteLine("");
                file.WriteLine("; used for decryption");
                file.WriteLine("[Private]");
                file.WriteLine("Modulus=");
                file.WriteLine("Exponent=");
                file.WriteLine("P=");
                file.WriteLine("Q=");
                file.WriteLine("DP=");
                file.WriteLine("DQ=");
                file.WriteLine("Inverse=");
                file.WriteLine("D=");
                file.WriteLine("");
                file.WriteLine("[Symmetric]");
                file.WriteLine("Key=");
                file.WriteLine("IV=");
                file.Close();
                file.Dispose();
                file = null;
            }

            IniFileName iniFile = new IniFileName(path);

            //Generate a public/private key pair.
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            //Save the public key information to an RSAParameters structure.
            RSAParameters publicInfo = rsa.ExportParameters(false);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

            tdes.GenerateIV();
            tdes.GenerateKey();

            AsymmetricCryptoHandler crypto = new AsymmetricCryptoHandler(publicInfo, true);

            byte[] key = crypto.Encrypt(tdes.Key);
            byte[] iv  = crypto.Encrypt(tdes.IV);

            iniFile.SetValue("Public", "Modulus", Convert.ToBase64String(publicInfo.Modulus));
            iniFile.SetValue("Public", "Exponent", Convert.ToBase64String(publicInfo.Exponent));

            RSAParameters keyInfo = rsa.ExportParameters(true);

            iniFile.SetValue("Private", "Modulus", Convert.ToBase64String(keyInfo.Modulus));
            iniFile.SetValue("Private", "Exponent", Convert.ToBase64String(keyInfo.Exponent));
            iniFile.SetValue("Private", "P", Convert.ToBase64String(keyInfo.P));
            iniFile.SetValue("Private", "Q", Convert.ToBase64String(keyInfo.Q));
            iniFile.SetValue("Private", "DP", Convert.ToBase64String(keyInfo.DP));
            iniFile.SetValue("Private", "DQ", Convert.ToBase64String(keyInfo.DQ));
            iniFile.SetValue("Private", "Inverse", Convert.ToBase64String(keyInfo.InverseQ));
            iniFile.SetValue("Private", "D", Convert.ToBase64String(keyInfo.D));
            iniFile.SetValue("Symmetric", "Key", Convert.ToBase64String(key));
            iniFile.SetValue("Symmetric", "IV", Convert.ToBase64String(iv));

            StreamWriter csFile = new StreamWriter(Environment.CurrentDirectory + @"\csharp.cs", false);

            csFile.WriteLine($"private byte[] _MODULUS = {GenerateArrayString(keyInfo.Modulus)};");
            csFile.WriteLine($"private byte[] _EXPONENT = {GenerateArrayString(keyInfo.Exponent)};");
            csFile.WriteLine($"private byte[] _P = {GenerateArrayString(keyInfo.P)};");
            csFile.WriteLine($"private byte[] _Q = {GenerateArrayString(keyInfo.Q)};");
            csFile.WriteLine($"private byte[] _DP = {GenerateArrayString(keyInfo.DP)};");
            csFile.WriteLine($"private byte[] _DQ = {GenerateArrayString(keyInfo.DQ)};");
            csFile.WriteLine($"private byte[] _INVERSE = {GenerateArrayString(keyInfo.InverseQ)};");
            csFile.WriteLine($"private byte[] _D = {GenerateArrayString(keyInfo.D)};");

            csFile.WriteLine($"");
            csFile.WriteLine($"private byte[] _SYM_KEY = {GenerateArrayString(key)};");
            csFile.WriteLine($"private byte[] _SYM_IV = {GenerateArrayString(iv)};");
            csFile.Close();
            csFile.Dispose();
        }