예제 #1
0
		public static ECKeyPair Create (ECDomainNames domain, byte[] privateKey, byte[] publicKey)
		{
			ECKeyPair pair = Create (domain);
			pair.PrivateKey = privateKey;
			pair._Q = new ECPoint (pair.Domain.Group, publicKey);
			return pair;
		}
예제 #2
0
        public void Test_Random()
        {
            ECDomainNames domainName = ECDomainNames.secp160r1;

            for (int i = 0; i < 10; i++)
            {
                ECIES  ecies1    = new ECIES(domainName);
                ECIES  ecies2    = new ECIES(domainName);
                byte[] plainText = RNG.GetBytes(RNG.GetBytes(1)[0] + RNG.GetBytes(1)[0]);

                // ecies2 exports public key.
                byte[] publicKey = ecies2.Parameters.ExportPublicKey(true);

                // ecies1 imports public key.
                ecies1.Parameters.PublicKey = publicKey;

                // ecies1 encrypt plainText.
                byte[] cipherText = ecies1.Encrypt(plainText);

                // ecies2 decrypt cipherText.
                byte[] decrypted = ecies2.Decrypt(cipherText);

                // Check !
                Assert.AreEqual(plainText, decrypted);
            }
        }
예제 #3
0
 internal ECKeyPair(Number d, ECPoint Q, ECDomainParameters domain, ECDomainNames domainName)
 {
     _d          = d;
     _Q          = Q;
     _domain     = domain;
     _domainName = domainName;
 }
예제 #4
0
 public ECIES(ECDomainNames name)
 {
     _domain = ECDomains.GetDomainParameter(name);
     _kdf    = new ANSI_X963_KDF(new SHA1Managed());
     _params = new ECIESParameters(_domain);
     _mac    = new HMACSHA1();
 }
예제 #5
0
        public static ECKeyPair CreatePrivate(ECDomainNames domain, byte[] privateKey)
        {
            ECKeyPair pair = Create(domain);

            pair.PrivateKey = privateKey;
            return(pair);
        }
예제 #6
0
		public ECIES (ECDomainNames name)
		{
			_domain = ECDomains.GetDomainParameter (name);
			_kdf = new ANSI_X963_KDF (new SHA1Managed ());
			_params = new ECIESParameters (_domain);
			_mac = new HMACSHA1 ();
		}
예제 #7
0
        public static ECKeyPair CreatePublic(ECDomainNames domain, byte[] publicKey)
        {
            ECKeyPair pair = Create(domain);

            pair.PublicKey = publicKey;
            return(pair);
        }
예제 #8
0
        public void Test_Random_with_SharedInfo1()
        {
            ECDomainNames domainName = ECDomainNames.secp256r1;

            for (int i = 0; i < 5; i++)
            {
                ECIES  ecies1     = new ECIES(domainName);
                ECIES  ecies2     = new ECIES(domainName);
                byte[] sharedInfo = RNG.GetBytes(RNG.GetBytes(1)[0] + 1);
                byte[] plainText  = RNG.GetBytes(RNG.GetBytes(1)[0] + RNG.GetBytes(1)[0] + 1);

                // setup shared info 1
                ecies1.SharedInfo1 = sharedInfo;
                ecies2.SharedInfo1 = sharedInfo;

                // ecies2 exports public key.
                byte[] publicKey = ecies2.Parameters.ExportPublicKey(true);

                // ecies1 imports public key.
                ecies1.Parameters.PublicKey = publicKey;

                // ecies1 encrypt plainText.
                byte[] cipherText = ecies1.Encrypt(plainText);

                // ecies2 decrypt cipherText.
                byte[] decrypted = ecies2.Decrypt(cipherText);

                // Check !
                Assert.AreEqual(plainText, decrypted);
            }
        }
예제 #9
0
		internal ECKeyPair (Number d, ECPoint Q, ECDomainParameters domain, ECDomainNames domainName)
		{
			_d = d;
			_Q = Q;
			_domain = domain;
			_domainName = domainName;
		}
예제 #10
0
        public static ECKeyPair Create(ECDomainNames domain, byte[] privateKey, byte[] publicKey)
        {
            ECKeyPair pair = Create(domain);

            pair.PrivateKey = privateKey;
            pair._Q         = new ECPoint(pair.Domain.Group, publicKey);
            return(pair);
        }
예제 #11
0
        static ECDomains()
        {
            _cache         = new Dictionary <ECDomainNames, ECDomainParameters> ();
            _creator       = new Dictionary <ECDomainNames, CreateDomainParameterDelegate> ();
            _oidMap        = new Dictionary <Uri, ECDomainNames> ();
            _oidReverseMap = new Dictionary <ECDomainNames, Uri> ();

            ECDomainNames[] names = new ECDomainNames[] {
                ECDomainNames.secp112r1,
                ECDomainNames.secp112r2,
                ECDomainNames.secp128r1,
                ECDomainNames.secp128r2,
                ECDomainNames.secp160r1,
                ECDomainNames.secp160r2,
                ECDomainNames.secp192r1,
                ECDomainNames.secp224r1,
                ECDomainNames.secp256r1,
                ECDomainNames.secp384r1,
                ECDomainNames.secp521r1
            };
            CreateDomainParameterDelegate[] procs = new CreateDomainParameterDelegate[] {
                Create_secp112r1,
                Create_secp112r2,
                Create_secp128r1,
                Create_secp128r2,
                Create_secp160r1,
                Create_secp160r2,
                Create_secp192r1,
                Create_secp224r1,
                Create_secp256r1,
                Create_secp384r1,
                Create_secp521r1
            };
            Uri[] oids = new Uri[] {
                new Uri(OID_CERTICOM_EC + "6"),
                new Uri(OID_CERTICOM_EC + "7"),
                new Uri(OID_CERTICOM_EC + "28"),
                new Uri(OID_CERTICOM_EC + "29"),
                new Uri(OID_CERTICOM_EC + "8"),
                new Uri(OID_CERTICOM_EC + "30"),
                new Uri(OID_ANSI_X9_64_PRIME_CURVE + "1"),
                new Uri(OID_CERTICOM_EC + "33"),
                new Uri(OID_ANSI_X9_64_PRIME_CURVE + "7"),
                new Uri(OID_CERTICOM_EC + "34"),
                new Uri(OID_CERTICOM_EC + "35")
            };

            for (int i = 0; i < names.Length; i++)
            {
                _creator.Add(names[i], procs[i]);
                _oidMap.Add(oids[i], names[i]);
                _oidReverseMap.Add(names[i], oids[i]);
            }
        }
예제 #12
0
        static byte[] ParsePrivateKey(string str_key, string str_passwd, out ECDomainNames domain)
        {
            try {
                string str_domain = null;
                byte[] key        = null;
                if (!char.IsDigit(str_key[0]))
                {
                    if (str_passwd.Length == 0)
                    {
                        throw new CryptographicException("秘密鍵は暗号化されています。パスフレーズを入力してください。");
                    }
                    byte[] pass = ComputeHash(new SHA256Managed(), Encoding.UTF8.GetBytes(str_passwd), true);
                    byte[] iv   = ComputeHash(new SHA1Managed(), Encoding.UTF8.GetBytes(str_passwd), true);
                    Array.Resize <byte> (ref iv, 128 >> 3);
                    string encType = str_key.Substring(0, str_key.IndexOf('='));
                    str_key    = str_key.Substring(str_key.IndexOf('=') + 1);
                    str_domain = str_key.Substring(0, str_key.IndexOf('='));
                    str_key    = str_key.Substring(str_key.IndexOf('=') + 1);
                    byte[] encrypted = Convert.FromBase64String(str_key);
                    try {
                        SymmetricAlgorithm algo = null;
                        switch (encType)
                        {
                        case "camellia256":
                            algo = new CamelliaManaged();
                            break;

                        case "rijndael256":
                            algo = new openCrypto.RijndaelManaged();
                            break;

                        default:
                            throw new CryptographicException("秘密鍵の暗号化タイプを認識できません");
                        }
                        key = Decrypt(algo, CipherMode.CBC, pass, iv, encrypted);
                    } catch {
                        throw new CryptographicException("パスフレーズが違います");
                    }
                }
                else
                {
                    str_domain = str_key.Substring(0, str_key.IndexOf('='));
                    str_key    = str_key.Substring(str_key.IndexOf('=') + 1);
                    key        = Convert.FromBase64String(str_key);
                }
                str_domain = "secp" + str_domain;
                domain     = (ECDomainNames)Enum.Parse(typeof(ECDomainNames), str_domain);
                return(key);
            } catch (CryptographicException) {
                throw;
            } catch {
                throw new CryptographicException("秘密鍵として認識することができません");
            }
        }
예제 #13
0
        private void btnKeyGenerate_Click(object sender, EventArgs e)
        {
            ECDomainNames domain     = (ECDomainNames)cbKeyType.SelectedIndex;
            ECDSA         dsa        = new ECDSA(domain);
            string        domainName = domain.ToString().Substring(4);

            byte[] privateKeyBytes = dsa.Parameters.PrivateKey;
            txtGeneratedKey.Text = ToPrivateKeyString(privateKeyBytes, txtGeneratedKeyPass.Text, domain);
            string publicKey = Convert.ToBase64String(dsa.Parameters.ExportPublicKey(domain != ECDomainNames.secp224r1 ? true : false));

            txtGeneratedPublicKey.Text = domainName + "=" + publicKey;
        }
예제 #14
0
 static byte[] ParsePublicKey(string str_key, out ECDomainNames domain)
 {
     try {
         string str_domain = str_key.Substring(0, str_key.IndexOf('='));
         str_key = str_key.Substring(str_key.IndexOf('=') + 1);
         byte[] key = Convert.FromBase64String(str_key);
         str_domain = "secp" + str_domain;
         domain     = (ECDomainNames)Enum.Parse(typeof(ECDomainNames), str_domain);
         return(key);
     } catch {
         throw new CryptographicException("公開鍵として認識することができません");
     }
 }
예제 #15
0
        public static ECDomainParameters GetDomainParameter(ECDomainNames domainName)
        {
            ECDomainParameters domain;

            lock (_cache) {
                if (_cache.TryGetValue(domainName, out domain))
                {
                    return(domain);
                }
                domain             = _creator[domainName] ();
                _cache[domainName] = domain;
            }
            return(domain);
        }
예제 #16
0
        public void Test_Random()
        {
            ECDomainNames name = ECDomainNames.secp256r1;

            for (int i = 0; i < 10; i++)
            {
                ECMQV ecmqv1     = new ECMQV(name);
                ECMQV ecmqv2     = new ECMQV(name);
                int   keyDataLen = 20;

                byte[] key1 = ecmqv1.PerformKeyAgreement(ecmqv2.Parameters.KeyPair1.PublicKey, ecmqv2.Parameters.KeyPair2.PublicKey, keyDataLen);
                byte[] key2 = ecmqv2.PerformKeyAgreement(ecmqv1.Parameters.KeyPair1.PublicKey, ecmqv1.Parameters.KeyPair2.PublicKey, keyDataLen);
                Assert.AreEqual(key1, key2);
            }
        }
예제 #17
0
        static void SignVerifyTest(ECDomainNames domainName)
        {
            int repeat = 5;

            for (int i = 0; i < repeat; i++)
            {
                ECDSA  ecdsa  = new ECDSA(domainName);
                byte[] pubKey = ecdsa.Parameters.PublicKey;
                byte[] hash   = RNG.GetBytes(ecdsa.KeySize >> 3);
                byte[] sign   = ecdsa.SignHash(hash);
                ecdsa = new ECDSA(domainName);
                ecdsa.Parameters.PublicKey = pubKey;
                Assert.IsTrue(ecdsa.VerifyHash(hash, sign), "Success Test " + domainName.ToString());
                sign[0]++;
                Assert.IsFalse(ecdsa.VerifyHash(hash, sign), "Failure Test " + domainName.ToString());
            }
        }
예제 #18
0
        public void Test_MQV()
        {
            ECDomainNames name       = ECDomainNames.secp160r1;
            ECMQV         ecmqv1     = new ECMQV(name);
            ECMQV         ecmqv2     = new ECMQV(name);
            int           keyDataLen = 20;
            int           keyBytes   = 20;

            ecmqv1.Parameters.KeyPair1.PrivateKey = Number.Parse("971761939728640320549601132085879836204587084162", 10).ToByteArray(keyBytes, false);
            ecmqv1.Parameters.KeyPair2.PrivateKey = Number.Parse("117720748206090884214100397070943062470184499100", 10).ToByteArray(keyBytes, false);
            ecmqv2.Parameters.KeyPair1.PrivateKey = Number.Parse("399525573676508631577122671218044116107572676710", 10).ToByteArray(keyBytes, false);
            ecmqv2.Parameters.KeyPair2.PrivateKey = Number.Parse("141325380784931851783969312377642205317371311134", 10).ToByteArray(keyBytes, false);

            byte[] key1 = ecmqv1.PerformKeyAgreement(ecmqv2.Parameters.KeyPair1.PublicKey, ecmqv2.Parameters.KeyPair2.PublicKey, keyDataLen);
            byte[] key2 = ecmqv2.PerformKeyAgreement(ecmqv1.Parameters.KeyPair1.PublicKey, ecmqv1.Parameters.KeyPair2.PublicKey, keyDataLen);

            Assert.AreEqual(key1, key2, "#1");
            Assert.AreEqual(key1, Number.Parse("C06763F8C3D2452C1CC5D29BD61918FB485063F6", 16).ToByteArray(keyDataLen, false), "#2");
        }
예제 #19
0
        public void Test_Random_with_SharedInfo()
        {
            ECDomainNames name = ECDomainNames.secp256r1;

            for (int i = 0; i < 10; i++)
            {
                ECMQV ecmqv1     = new ECMQV(name);
                ECMQV ecmqv2     = new ECMQV(name);
                int   keyDataLen = 20;

                byte[] sharedInfo = RNG.GetBytes(RNG.GetBytes(1)[0] + 1);
                ecmqv1.SharedInfo = sharedInfo;
                ecmqv2.SharedInfo = sharedInfo;

                byte[] key1 = ecmqv1.PerformKeyAgreement(ecmqv2.Parameters.KeyPair1.PublicKey, ecmqv2.Parameters.KeyPair2.PublicKey, keyDataLen);
                byte[] key2 = ecmqv2.PerformKeyAgreement(ecmqv1.Parameters.KeyPair1.PublicKey, ecmqv1.Parameters.KeyPair2.PublicKey, keyDataLen);
                Assert.AreEqual(key1, key2);
            }
        }
예제 #20
0
파일: KeyTest.cs 프로젝트: kazuki/p2pncs
 public void EC_RoundtripTest()
 {
     ECDomainNames[] domains = new ECDomainNames[] {
         ECDomainNames.secp192r1,
         ECDomainNames.secp256r1
     };
     foreach (ECDomainNames domain in domains) {
         ECKeyPair pair1 = ECKeyPair.Create (domain);
         Key key1 = Key.Create (pair1);
         ECKeyPair pair2 = key1.ToECPublicKey ();
         Assert.AreEqual (pair1.DomainName, pair2.DomainName);
         Assert.AreEqual (pair1.PublicKey, pair2.PublicKey);
         pair2 = ECKeyPairExtensions.CreatePrivate (pair1.PrivateKey);
         Assert.AreEqual (pair1.DomainName, pair2.DomainName);
         Assert.AreEqual (pair1.PrivateKey, pair2.PrivateKey);
         pair2 = ECKeyPairExtensions.CreatePublic (pair1.ExportPublicKey (true));
         Assert.AreEqual (pair1.DomainName, pair2.DomainName);
         Assert.AreEqual (pair1.PublicKey, pair2.PublicKey);
     }
 }
예제 #21
0
        public void Test_GEC2()
        {
            ECDomainNames      domainName = ECDomainNames.secp160r1;
            ECDomainParameters domain     = ECDomains.GetDomainParameter(domainName);
            ECIES     ecies     = new ECIES(domainName);
            Number    V_Private = Number.Parse("45FB58A92A17AD4B15101C66E74F277E2B460866", 16);
            ECKeyPair pair      = new ECKeyPair(V_Private, null, domain);

            pair.CreatePublicKeyFromPrivateKey();
            ecies.Parameters._Q = pair._Q;
            byte[] M         = System.Text.Encoding.ASCII.GetBytes("abcdefghijklmnopqrst");
            byte[] k         = Number.Parse("702232148019446860144825009548118511996283736794", 10).ToByteArray(20, false);
            byte[] C         = ecies.Encrypt(M, k);
            byte[] expectedC = new byte[] { 0x02, 0xCE, 0x28, 0x73, 0xE5, 0xBE, 0x44, 0x95, 0x63, 0x39, 0x1F, 0xEB, 0x47, 0xDD, 0xCB, 0xA2, 0xDC, 0x16, 0x37, 0x91, 0x91, 0x71, 0x23, 0xC8, 0x70, 0xA3, 0x1A, 0x81, 0xEA, 0x75, 0x83, 0x29, 0x0D, 0x1B, 0xA1, 0x7B, 0xC8, 0x75, 0x94, 0x35, 0xED, 0x1C, 0xCD, 0xA9, 0xEB, 0x4E, 0xD2, 0x73, 0x60, 0xBE, 0x89, 0x67, 0x29, 0xAD, 0x18, 0x54, 0x93, 0x62, 0x25, 0x91, 0xE5 };
            Assert.AreEqual(expectedC, C, "Encryption");

            ecies = new ECIES(domainName);
            ecies.Parameters._d = V_Private;
            byte[] M2 = ecies.Decrypt(C);
            Assert.AreEqual(M, M2, "Decryption");
        }
예제 #22
0
        string ToPrivateKeyString(byte[] privateKey, string passphrase, ECDomainNames domain)
        {
            string domainName = domain.ToString().Substring(4);

            if (passphrase.Length > 0)
            {
                byte[] pass = ComputeHash(new SHA256Managed(), Encoding.UTF8.GetBytes(txtGeneratedKeyPass.Text), true);
                byte[] iv   = ComputeHash(new SHA1Managed(), Encoding.UTF8.GetBytes(txtGeneratedKeyPass.Text), true);
                Array.Resize <byte> (ref iv, 128 >> 3);
                string             encType = null;
                SymmetricAlgorithm algo    = null;
                switch (cbPassEncryptType.SelectedIndex)
                {
                case 0:
                    encType = "camellia256";
                    algo    = new CamelliaManaged();
                    break;

                case 1:
                    encType = "rijndael256";
                    algo    = new openCrypto.RijndaelManaged();
                    break;

                default:
                    throw new CryptographicException("暗号化の種類を認識できません");
                }
                byte[] encrypted      = Encrypt(algo, CipherMode.CBC, pass, iv, privateKey);
                string privateKeyText = Convert.ToBase64String(encrypted);
                return(encType + "=" + domainName + "=" + privateKeyText);
            }
            else
            {
                string privateKeyText = Convert.ToBase64String(privateKey);
                return(domainName + "=" + privateKeyText);
            }
        }
예제 #23
0
 public ECDiffieHellman(ECDomainNames name)
     : this(new ECDiffieHellmanParameters(null, null, ECDomains.GetDomainParameter(name)))
 {
 }
예제 #24
0
        void PointCompressTest(ECDomainNames name, int repeats)
        {
            ECDomainParameters domain = ECDomains.GetDomainParameter(name);

            PointCompressTest((int)domain.Bits, domain.Group, domain.G, repeats, name.ToString());
        }
예제 #25
0
 public ECDSA(ECDomainNames domain)
     : this(ECDomains.GetDomainParameter(domain))
 {
 }
예제 #26
0
 public ECMQV(ECDomainNames name)
     : this(ECDomains.GetDomainParameter(name))
 {
 }
예제 #27
0
		public ECMQV (ECDomainNames name)
			: this (ECDomains.GetDomainParameter (name))
		{
		}
예제 #28
0
		static void SignVerifyTest (ECDomainNames domainName)
		{
			int repeat = 5;
			for (int i = 0; i < repeat; i ++) {
				ECDSA ecdsa = new ECDSA (domainName);
				byte[] pubKey = ecdsa.Parameters.PublicKey;
				byte[] hash = RNG.GetBytes (ecdsa.KeySize >> 3);
				byte[] sign = ecdsa.SignHash (hash);
				ecdsa = new ECDSA (domainName);
				ecdsa.Parameters.PublicKey = pubKey;
				Assert.IsTrue (ecdsa.VerifyHash (hash, sign), "Success Test " + domainName.ToString ());
				sign[0]++;
				Assert.IsFalse (ecdsa.VerifyHash (hash, sign), "Failure Test " + domainName.ToString ());
			}
		}
예제 #29
0
		public ECIES (ECDomainNames name, SymmetricAlgorithm symmetricAlgo) : this (name)
		{
			_symmetricAlgo = symmetricAlgo;
		}
예제 #30
0
		static byte[] ParsePublicKey (string str_key, out ECDomainNames domain)
		{
			try {
				string str_domain = str_key.Substring (0, str_key.IndexOf ('='));
				str_key = str_key.Substring (str_key.IndexOf ('=') + 1);
				byte[] key = Convert.FromBase64String (str_key);
				str_domain = "secp" + str_domain;
				domain = (ECDomainNames)Enum.Parse (typeof (ECDomainNames), str_domain);
				return key;
			} catch {
				throw new CryptographicException ("公開鍵として認識することができません");
			}
		}
예제 #31
0
 public ECIES(ECDomainNames name, SymmetricAlgorithm symmetricAlgo) : this(name)
 {
     _symmetricAlgo = symmetricAlgo;
 }
예제 #32
0
		void PointCompressTest (ECDomainNames name, int repeats)
		{
			ECDomainParameters domain = ECDomains.GetDomainParameter (name);
			PointCompressTest ((int)domain.Bits, domain.Group, domain.G, repeats, name.ToString ());
		}
예제 #33
0
		public static ECKeyPair CreatePublic (ECDomainNames domain, byte[] publicKey)
		{
			ECKeyPair pair = Create (domain);
			pair.PublicKey = publicKey;
			return pair;
		}
예제 #34
0
		public static ECKeyPair CreatePrivate (ECDomainNames domain, byte[] privateKey)
		{
			ECKeyPair pair = Create (domain);
			pair.PrivateKey = privateKey;
			return pair;
		}
예제 #35
0
        static void Main()
        {
            CipherMode mode     = CipherMode.ECB;
            int        dataSize = 1024 * 1024;

            double[] result;

            Assembly asm = Assembly.GetAssembly(typeof(openCrypto.CamelliaManaged));

            Console.WriteLine(asm.FullName.Replace(",", Environment.NewLine + " "));
            Console.WriteLine();

            Console.WriteLine("Symmetric-Key Algorithm:");
            result = Run(new CamelliaManaged(), mode, 128, 128, dataSize);
            Console.WriteLine("Camellia 128bit Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1]);
            result = Run(new CamelliaManaged(), mode, 192, 128, dataSize);
            Console.WriteLine("Camellia 192bit Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1]);
            result = Run(new CamelliaManaged(), mode, 256, 128, dataSize);
            Console.WriteLine("Camellia 256bit Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1]);

            result = Run(new RijndaelManaged(), mode, 128, 128, dataSize);
            Console.WriteLine("Rijndael 128bit Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1]);
            result = Run(new RijndaelManaged(), mode, 192, 128, dataSize);
            Console.WriteLine("Rijndael 192bit Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1]);
            result = Run(new RijndaelManaged(), mode, 256, 128, dataSize);
            Console.WriteLine("Rijndael 256bit Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1]);

            if (Environment.ProcessorCount > 1)
            {
                result = Run(new CamelliaManaged(), CipherModePlus.ECB, 128, 128, dataSize, 2);
                Console.WriteLine("Camellia 128bit (2-threads) Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1]);

                result = Run(new RijndaelManaged(), CipherModePlus.ECB, 128, 128, dataSize, 2);
                Console.WriteLine("Rijndael 128bit (2-threads) Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1]);

                if (Environment.ProcessorCount != 2)
                {
                    result = Run(new CamelliaManaged(), CipherModePlus.ECB, 128, 128, dataSize, Environment.ProcessorCount);
                    Console.WriteLine("Camellia 128bit ({2}-threads) Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1], Environment.ProcessorCount);

                    result = Run(new RijndaelManaged(), CipherModePlus.ECB, 128, 128, dataSize, Environment.ProcessorCount);
                    Console.WriteLine("Rijndael 128bit ({2}-threads) Encrypt: {0:f2}Mbps, Decrypt: {1:f2}Mbps", result[0], result[1], Environment.ProcessorCount);
                }
            }
            Console.WriteLine();

            Console.WriteLine("ECDSA:");
            for (int i = (int)ECDomainNames.secp112r1; i <= (int)ECDomainNames.secp521r1; i++)
            {
                ECDomainNames domain = (ECDomainNames)i;
                result = Run(new ECDSAManaged(domain));
                Console.WriteLine("{0}: Sign: {1}ms, Verify: {2}ms", domain, result[0], result[1]);
            }
            Console.WriteLine();

            Console.WriteLine("ECDH:");
            for (int i = (int)ECDomainNames.secp112r1; i <= (int)ECDomainNames.secp521r1; i++)
            {
                ECDomainNames domain = (ECDomainNames)i;
                double        ret    = Run(new ECDiffieHellman(domain));
                Console.WriteLine("{0}: {1}ms", domain, ret);
            }
            Console.WriteLine();

            Console.WriteLine("ECMQV:");
            for (int i = (int)ECDomainNames.secp112r1; i <= (int)ECDomainNames.secp521r1; i++)
            {
                ECDomainNames domain = (ECDomainNames)i;
                double        ret    = Run(new ECMQV(domain));
                Console.WriteLine("{0}: {1}ms", domain, ret);
            }

            Console.WriteLine();
            HashAlgorithm[] hashList = new HashAlgorithm[] {
                new SHA1Managed(), new SHA256Managed(), new SHA384Managed(), new SHA512Managed(),
                new Luffa224Managed(), new Luffa256Managed(), new Luffa384Managed(), new Luffa512Managed(),
                new CMAC(new RijndaelManaged()), new CMAC(new CamelliaManaged())
            };
            string[] hashNames = new string[] {
                "    SHA1", "  SHA256", "  SHA384", "  SHA512",
                "Luffa224", "Luffa256", "Luffa384", "Luffa512",
                "CMAC-AES", "CMAC-CAM"
            };
            int[] testSizes = new int[] { 0, 32, 1024, 1024 * 1024, 1024 * 1024 * 64 };
            Console.WriteLine("    Size |     0B     |     32B    |     1KB    |     1MB    |    64MB    |");
            Console.WriteLine("---------------------------------------------------------------------------");
            for (int i = 0; i < hashList.Length; i++)
            {
                TimeSpan[] results = new TimeSpan[testSizes.Length];
                for (int k = 0; k < results.Length; k++)
                {
                    results[k] = SpeedTest.Run(hashList[i], testSizes[k]);
                }
                Console.Write("{0} | ", hashNames[i]);
                for (int k = 0; k < results.Length; k++)
                {
                    Console.Write("{0}t | ", results[k].Ticks.ToString().PadLeft(9));
                }
                Console.WriteLine();
                Console.Write("         | ");
                for (int k = 0; k < results.Length; k++)
                {
                    Console.Write("{0}Mbps | ", (testSizes[k] / results[k].TotalSeconds * 8.0 / 1024.0 / 1024.0).ToString("f5").Substring(0, 6));
                }
                Console.WriteLine();
            }
        }
예제 #36
0
		/// <param name="d">Private Key</param>
		/// <param name="Q">Public Key</param>
		internal ECKeyPair (Number d, ECPoint Q, ECDomainParameters domain)
			: this (d, Q, domain, ECDomainNames.none)
		{
			_domainName = ECDomains.GetDomainName (domain);
		}
예제 #37
0
		static void Validate (ECDomainNames name)
		{
			Assert.IsTrue (ECDomains.GetDomainParameter (name).Validate (), name.ToString ());
		}
예제 #38
0
 /// <param name="d">Private Key</param>
 /// <param name="Q">Public Key</param>
 internal ECKeyPair(Number d, ECPoint Q, ECDomainParameters domain)
     : this(d, Q, domain, ECDomainNames.none)
 {
     _domainName = ECDomains.GetDomainName(domain);
 }
예제 #39
0
		public ECDiffieHellman (ECDomainNames name)
			: this (new ECDiffieHellmanParameters (null, null, ECDomains.GetDomainParameter (name)))
		{
		}
예제 #40
0
 public static ECKeyPair Create(ECDomainNames domain)
 {
     return(new ECKeyPair(null, null, ECDomains.GetDomainParameter(domain), domain));
 }
예제 #41
0
		static byte[] ParsePrivateKey (string str_key, string str_passwd, out ECDomainNames domain)
		{
			try {
				string str_domain = null;
				byte[] key = null;
				if (!char.IsDigit (str_key[0])) {
					if (str_passwd.Length == 0)
						throw new CryptographicException ("秘密鍵は暗号化されています。パスフレーズを入力してください。");
					byte[] pass = ComputeHash (new SHA256Managed (), Encoding.UTF8.GetBytes (str_passwd), true);
					byte[] iv = ComputeHash (new SHA1Managed (), Encoding.UTF8.GetBytes (str_passwd), true);
					Array.Resize<byte> (ref iv, 128 >> 3);
					string encType = str_key.Substring (0, str_key.IndexOf ('='));
					str_key = str_key.Substring (str_key.IndexOf ('=') + 1);
					str_domain = str_key.Substring (0, str_key.IndexOf ('='));
					str_key = str_key.Substring (str_key.IndexOf ('=') + 1);
					byte[] encrypted = Convert.FromBase64String (str_key);
					try {
						SymmetricAlgorithm algo = null;
						switch (encType) {
							case "camellia256":
								algo = new CamelliaManaged ();
								break;
							case "rijndael256":
								algo = new openCrypto.RijndaelManaged ();
								break;
							default:
								throw new CryptographicException ("秘密鍵の暗号化タイプを認識できません");
						}
						key = Decrypt (algo, CipherMode.CBC, pass, iv, encrypted);
					} catch {
						throw new CryptographicException ("パスフレーズが違います");
					}
				} else {
					str_domain = str_key.Substring (0, str_key.IndexOf ('='));
					str_key = str_key.Substring (str_key.IndexOf ('=') + 1);
					key = Convert.FromBase64String (str_key);
				}
				str_domain = "secp" + str_domain;
				domain = (ECDomainNames)Enum.Parse (typeof (ECDomainNames), str_domain);
				return key;
			} catch (CryptographicException) {
				throw;
			} catch {
				throw new CryptographicException ("秘密鍵として認識することができません");
			}
		}
예제 #42
0
		string ToPrivateKeyString (byte[] privateKey, string passphrase, ECDomainNames domain)
		{
			string domainName = domain.ToString ().Substring (4);
			if (passphrase.Length > 0) {
				byte[] pass = ComputeHash (new SHA256Managed (), Encoding.UTF8.GetBytes (txtGeneratedKeyPass.Text), true);
				byte[] iv = ComputeHash (new SHA1Managed (), Encoding.UTF8.GetBytes (txtGeneratedKeyPass.Text), true);
				Array.Resize<byte> (ref iv, 128 >> 3);
				string encType = null;
				SymmetricAlgorithm algo = null;
				switch (cbPassEncryptType.SelectedIndex) {
					case 0:
						encType = "camellia256";
						algo = new CamelliaManaged ();
						break;
					case 1:
						encType = "rijndael256";
						algo = new openCrypto.RijndaelManaged ();
						break;
					default:
						throw new CryptographicException ("暗号化の種類を認識できません");
				}
				byte[] encrypted = Encrypt (algo, CipherMode.CBC, pass, iv, privateKey);
				string privateKeyText = Convert.ToBase64String (encrypted);
				return encType + "=" + domainName + "=" + privateKeyText;
			} else {
				string privateKeyText = Convert.ToBase64String (privateKey);
				return domainName + "=" + privateKeyText;
			}
		}
예제 #43
0
 static void Validate(ECDomainNames name)
 {
     Assert.IsTrue(ECDomains.GetDomainParameter(name).Validate(), name.ToString());
 }
예제 #44
0
		public static ECKeyPair Create (ECDomainNames domain)
		{
			return new ECKeyPair (null, null, ECDomains.GetDomainParameter (domain), domain);
		}