当前位置: 首页>>代码示例>>C#>>正文


C# Crypto.CryptoKey类代码示例

本文整理汇总了C#中OpenSSL.Crypto.CryptoKey的典型用法代码示例。如果您正苦于以下问题:C# CryptoKey类的具体用法?C# CryptoKey怎么用?C# CryptoKey使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


CryptoKey类属于OpenSSL.Crypto命名空间,在下文中一共展示了CryptoKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestSealOpen

        public void TestSealOpen()
        {
            string inputMsg = "This is a message";
            byte[] input = Encoding.ASCII.GetBytes(inputMsg);
            const int numKeys = 10;
            var rsas = new RSA[numKeys];
            var pkeys = new CryptoKey[numKeys];
            for (int i = 0; i < numKeys; i++) {
                rsas[i] = new RSA();
                rsas[i].GenerateKeys(1024, BigNumber.One, null, null);
                pkeys[i] = new CryptoKey(rsas[i]);
            }

            try {
                foreach (var cipher in Ciphers(true)) {
                    using (var cc = new CipherContext(cipher)) {
                        var env = cc.Seal(pkeys, input);
                        Assert.AreNotEqual(input, env.Data);

                        for (int i = 0; i < numKeys; i++) {
                            var result = cc.Open(env.Data, env.Keys[i], env.IV, pkeys[i]);
                            Assert.AreEqual(input, result);
                        }
                    }
                }
            }
            finally {
                for (int i = 0; i < numKeys; i++) {
                    pkeys[i].Dispose();
                    rsas[i].Dispose();
                }
            }
        }
开发者ID:LiorNaor,项目名称:openssl-net,代码行数:33,代码来源:TestCipher.cs

示例2: TestRsaSha1

		public void TestRsaSha1()
		{
			using (Configuration cfg = new Configuration("openssl.cnf"))
			{
				// Test RSA/SHA1 with other SelfSigned method
				BigNumber bn = 0x10001;
				CryptoKey key;

				using (RSA rsa = new RSA())
				{
					rsa.GenerateKeys(2048, bn, OnGenerator, null);
					key = new CryptoKey(rsa);
					// rsa is assigned, we no longer need this instance
				}

				using (var root = X509CertificateAuthority.SelfSigned(
					                  cfg,
					                  new SimpleSerialNumber(),
					                  key,
					                  MessageDigest.SHA1,
					                  "Root1",
					                  DateTime.Now,
					                  TimeSpan.FromDays(365)))
				{
					Console.WriteLine(root.Certificate);
				}
			}
		}
开发者ID:yaobos,项目名称:openssl-net,代码行数:28,代码来源:TestX509.cs

示例3: X509Request

		/// <summary>
		/// Calls X509_REQ_new() and then initializes version, subject, and key.
		/// </summary>
		/// <param name="version"></param>
		/// <param name="subject"></param>
		/// <param name="key"></param>
		public X509Request(int version, X509Name subject, CryptoKey key)
			: this()
		{
			this.Version = version;
			this.Subject = subject;
			this.PublicKey = key;
		}
开发者ID:challal,项目名称:scallion,代码行数:13,代码来源:X509Request.cs

示例4: CanAddRequestExtensions

        public void CanAddRequestExtensions()
        {
            var extList = new List<X509V3ExtensionValue> {
                new X509V3ExtensionValue("subjectAltName", false, "DNS:foo.com,DNS:bar.org"),
                new X509V3ExtensionValue("keyUsage", false, "cRLSign,keyCertSign"),
            };

            var start = DateTime.Now;
            var end = start + TimeSpan.FromMinutes(10);
            using (var key = new CryptoKey(RSA.FromPrivateKey(new BIO(RSA_KEY))))
            using (var request = new X509Request(1,new X509Name("foo"),key))
            {
                OpenSSL.Core.Stack<X509Extension> extensions = new OpenSSL.Core.Stack<X509Extension>();
                foreach (var extValue in extList)
                {
                    using (var ext = new X509Extension(request, extValue.Name, extValue.IsCritical, extValue.Value))
                    {
                        Console.WriteLine(ext);
                        extensions.Add(ext);
                    }
                }

                request.AddExtensions(extensions);

                Assert.AreEqual(EXPECTED_CERT, request.PEM);
            }
        }
开发者ID:ebekker,项目名称:openssl-net,代码行数:27,代码来源:TestX509Request.cs

示例5: CanAddExtensions

        public void CanAddExtensions()
        {
            X509V3ExtensionList extList = new X509V3ExtensionList();
            extList.Add(new X509V3ExtensionValue("subjectKeyIdentifier", false, "hash"));
            extList.Add(new X509V3ExtensionValue("authorityKeyIdentifier", false, "keyid:always,issuer:always"));
            extList.Add(new X509V3ExtensionValue("basicConstraints", true, "critical,CA:true"));
            extList.Add(new X509V3ExtensionValue("keyUsage", false, "cRLSign,keyCertSign"));

            DateTime start = DateTime.Now;
            DateTime end = start + TimeSpan.FromMinutes(10);
            CryptoKey key = new CryptoKey(new DSA(true));
            using (X509Certificate cert = new X509Certificate(101, "CN=Root", "CN=Root", key, start, end)) {
                foreach (X509V3ExtensionValue extValue in extList) {
                    using (X509Extension ext = new X509Extension(cert, cert, extValue.Name, extValue.IsCritical, extValue.Value)) {
                        cert.AddExtension(ext);
                    }
                }

                foreach (X509Extension ext in cert.Extensions) {
                    Console.WriteLine(ext);
                }

                Assert.AreEqual(extList.Count, cert.Extensions.Count);
            }
        }
开发者ID:LiorNaor,项目名称:openssl-net,代码行数:25,代码来源:TestX509Certificate.cs

示例6: Bug3017248

 public void Bug3017248()
 {
     CryptoKey key = new CryptoKey(new DSA(true));
     BIO output = BIO.MemoryBuffer();
     key.WritePrivateKey(output, Cipher.Null, "password");
     output.SetClose(BIO.CloseOption.Close);
     Console.WriteLine(output.ReadString());
 }
开发者ID:LiorNaor,项目名称:openssl-net,代码行数:8,代码来源:SourceForgeBugs.cs

示例7: TestCipher

		static TestCipher()
		{
			const int numKeys = 10;
			Keys = new CryptoKey[numKeys];
			for (int i = 0; i < numKeys; i++)
			{
				using (var rsa = new RSA())
				{
					rsa.GenerateKeys(1024, BigNumber.One, null, null);
					Keys[i] = new CryptoKey(rsa);
				}
			}
		}
开发者ID:chang892886597,项目名称:openssl-net,代码行数:13,代码来源:TestCipher.cs

示例8: CanCompareDSA

		public void CanCompareDSA()
		{
			using (var dsa = new DSA(true))
			using (var lhs = new CryptoKey(dsa))
			{
				Assert.AreEqual(lhs, lhs);
				using (var rhs = new CryptoKey(dsa))
				{
					Assert.AreEqual(lhs, rhs);
				}

				using (var dsa2 = new DSA(true))
				using (var other = new CryptoKey(dsa2))
				{
					Assert.AreNotEqual(lhs, other);
				}
			}
		}
开发者ID:chang892886597,项目名称:openssl-net,代码行数:18,代码来源:TestCryptoKey.cs

示例9: CreateCertificate

        private static X509Certificate CreateCertificate()
        {
            BigNumber bn = 0x10001;
            var rsa = new RSA();
            rsa.GenerateKeys(2048, bn, null, null);
            var key = new CryptoKey(rsa);

            var cert = new X509Certificate(
                new SimpleSerialNumber().Next(),
                new X509Name("Mooege"),
                new X509Name("Mooege"),
                key,
                DateTime.Now,
                DateTime.Now + TimeSpan.FromDays(365));

            cert.PrivateKey = key;
            return cert;
        }
开发者ID:wow4all,项目名称:mooege,代码行数:18,代码来源:CertificateHelper.cs

示例10: CreateCertificate

		X509Certificate CreateCertificate(X509CertificateAuthority ca, string name, Configuration cfg, string section)
		{
			var now = DateTime.Now;
			var future = now + TimeSpan.FromDays(365);

			using (var subject = new X509Name(name))
			using (var rsa = new RSA())
			{
				rsa.GenerateKeys(1024, BigNumber.One, null, null);
				using (var key = new CryptoKey(rsa))
				{
					var request = new X509Request(1, subject, key);
					var cert = ca.ProcessRequest(request, now, future, cfg, section);
					cert.PrivateKey = key;
					return cert;
				}
			}
		}
开发者ID:yaobos,项目名称:openssl-net,代码行数:18,代码来源:TestSSL.cs

示例11: CanCompare

		public void CanCompare()
		{
			using (DSA dsa = new DSA(true))
			{
				using (CryptoKey lhs = new CryptoKey(dsa))
				{
					Assert.AreEqual(lhs, lhs);
					using (CryptoKey rhs = new CryptoKey(dsa))
					{
						Assert.AreEqual(lhs, rhs);
					}

					using (DSA dsa2 = new DSA(true))
					{
						using (CryptoKey other = new CryptoKey(dsa2))
						{
							Assert.IsFalse(lhs == other);
						}
					}
				}
			}

			using (RSA rsa = new RSA())
			{
				rsa.GenerateKeys(1024, BigNumber.One, null, null);
				using (CryptoKey lhs = new CryptoKey(rsa))
				{
					Assert.AreEqual(lhs, lhs);
					using (CryptoKey rhs = new CryptoKey(rsa))
					{
						Assert.AreEqual(lhs, rhs);
					}

					using (RSA rsa2 = new RSA())
					{
						rsa2.GenerateKeys(1024, BigNumber.One, null, null);
						using (CryptoKey other = new CryptoKey(rsa2))
						{
							Assert.IsFalse(lhs == other);
						}
					}
				}
			}
		}
开发者ID:Nangal,项目名称:http2-katana,代码行数:44,代码来源:TestCryptoKey.cs

示例12: CanCompareRSA

		public void CanCompareRSA()
		{
			using (var rsa = new RSA())
			{
				rsa.GenerateKeys(1024, BigNumber.One, null, null);
				using (var lhs = new CryptoKey(rsa))
				{
					Assert.AreEqual(lhs, lhs);
					using (var rhs = new CryptoKey(rsa))
					{
						Assert.AreEqual(lhs, rhs);
					}

					using (var rsa2 = new RSA())
					{
						rsa2.GenerateKeys(1024, BigNumber.One, null, null);
						using (var other = new CryptoKey(rsa2))
						{
							Assert.AreNotEqual(lhs, other);
						}
					}
				}
			}
		}
开发者ID:chang892886597,项目名称:openssl-net,代码行数:24,代码来源:TestCryptoKey.cs

示例13: Sign

		/// <summary>
		/// Calls EVP_SignFinal()
		/// </summary>
		/// <param name="md"></param>
		/// <param name="bio"></param>
		/// <param name="pkey"></param>
		/// <returns></returns>
		public static byte[] Sign(MessageDigest md, BIO bio, CryptoKey pkey)
		{
			BIO bmd = BIO.MessageDigest(md);
			bmd.Push(bio);

			while (true)
			{
				ArraySegment<byte> bytes = bmd.ReadBytes(1024 * 4);
				if (bytes.Count == 0)
					break;
			}

			MessageDigestContext ctx = new MessageDigestContext(bmd);

			byte[] sig = new byte[pkey.Size];
			uint len = (uint)sig.Length;
			Native.ExpectSuccess(Native.EVP_SignFinal(ctx.Handle, sig, ref len, pkey.Handle));
			byte[] ret = new byte[len];
			Buffer.BlockCopy(sig, 0, ret, 0, (int)len);
			return ret;
		}
开发者ID:wow4all,项目名称:mooege,代码行数:28,代码来源:MessageDigest.cs

示例14: Open

		/// <summary>
		/// Calls EVP_OpenInit() and EVP_OpenFinal()
		/// </summary>
		/// <param name="input"></param>
		/// <param name="ekey"></param>
		/// <param name="iv"></param>
		/// <param name="pkey"></param>
		/// <returns></returns>
		public byte[] Open(byte[] input, byte[] ekey, byte[] iv, CryptoKey pkey) 
		{
			Native.ExpectSuccess(Native.EVP_OpenInit(
				this.ptr, this.cipher.Handle, ekey, ekey.Length, iv, pkey.Handle));
			
			MemoryStream memory = new MemoryStream();
			byte[] output = new byte[input.Length + this.Cipher.BlockSize]; 
			int len;
			
			Native.ExpectSuccess(Native.EVP_DecryptUpdate(this.ptr, output, out len, input, input.Length));
			memory.Write(output, 0, len);
			
			Native.ExpectSuccess(Native.EVP_OpenFinal(this.ptr, output, out len));
			memory.Write(output, 0, len);

			return memory.ToArray();
		}
开发者ID:langhuihui,项目名称:csharprtmp,代码行数:25,代码来源:Cipher.cs

示例15: CanCreateFromDH

		public void CanCreateFromDH()
		{
			using (DH dh = new DH())
			{
				dh.GenerateKeys();
				using (CryptoKey key = new CryptoKey(dh)) {
					Assert.AreEqual(CryptoKey.KeyType.DH, key.Type);
				}
			}
		}
开发者ID:Nangal,项目名称:http2-katana,代码行数:10,代码来源:TestCryptoKey.cs


注:本文中的OpenSSL.Crypto.CryptoKey类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。