本文整理汇总了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();
}
}
}
示例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);
}
}
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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());
}
示例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);
}
}
}
示例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);
}
}
}
示例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;
}
示例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;
}
}
}
示例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);
}
}
}
}
}
示例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);
}
}
}
}
}
示例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;
}
示例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();
}
示例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);
}
}
}