本文整理汇总了C#中Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair类的典型用法代码示例。如果您正苦于以下问题:C# AsymmetricCipherKeyPair类的具体用法?C# AsymmetricCipherKeyPair怎么用?C# AsymmetricCipherKeyPair使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AsymmetricCipherKeyPair类属于Org.BouncyCastle.Crypto命名空间,在下文中一共展示了AsymmetricCipherKeyPair类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPrivateKey
/// <summary>
/// Gets the PEM for the Private Key from the Asymmetric Key Pair.
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
public static String GetPrivateKey(AsymmetricCipherKeyPair keys)
{
TextWriter textWriter = new StringWriter();
PemWriter pemWriter = new PemWriter(textWriter);
pemWriter.WriteObject(keys.Private);
pemWriter.Writer.Flush();
return textWriter.ToString();
}
示例2: encodeUserKeyPairPrivate
public static byte[] encodeUserKeyPairPrivate(AsymmetricCipherKeyPair userKey)
{
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(userKey.Private);
byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetDerEncoded();
return serializedPrivateBytes;
}
示例3: SaveToFile
public static void SaveToFile(
X509Certificate newCert,
AsymmetricCipherKeyPair kp,
string FilePath,
string CertAlias,
string Password)
{
var newStore = new Pkcs12Store();
var certEntry = new X509CertificateEntry(newCert);
newStore.SetCertificateEntry(
CertAlias,
certEntry
);
newStore.SetKeyEntry(
CertAlias,
new AsymmetricKeyEntry(kp.Private),
new[] { certEntry }
);
using (var certFile = File.Create(FilePath))
{
newStore.Save(
certFile,
Password.ToCharArray(),
new SecureRandom(new CryptoApiRandomGenerator())
);
}
}
示例4: MakeCertificate
public static X509Certificate MakeCertificate(AsymmetricCipherKeyPair _subKP,
string _subDN, AsymmetricCipherKeyPair _issKP, string _issDN, string algorithm, bool _ca)
{
AsymmetricKeyParameter _subPub = _subKP.Public;
AsymmetricKeyParameter _issPriv = _issKP.Private;
AsymmetricKeyParameter _issPub = _issKP.Public;
X509V3CertificateGenerator _v3CertGen = new X509V3CertificateGenerator();
_v3CertGen.Reset();
_v3CertGen.SetSerialNumber(allocateSerialNumber());
_v3CertGen.SetIssuerDN(new X509Name(_issDN));
_v3CertGen.SetNotBefore(DateTime.UtcNow);
_v3CertGen.SetNotAfter(DateTime.UtcNow.AddDays(100));
_v3CertGen.SetSubjectDN(new X509Name(_subDN));
_v3CertGen.SetPublicKey(_subPub);
_v3CertGen.SetSignatureAlgorithm(algorithm);
_v3CertGen.AddExtension(X509Extensions.SubjectKeyIdentifier, false,
createSubjectKeyId(_subPub));
_v3CertGen.AddExtension(X509Extensions.AuthorityKeyIdentifier, false,
createAuthorityKeyId(_issPub));
_v3CertGen.AddExtension(X509Extensions.BasicConstraints, false,
new BasicConstraints(_ca));
X509Certificate _cert = _v3CertGen.Generate(_issPriv);
_cert.CheckValidity(DateTime.UtcNow);
_cert.Verify(_issPub);
return _cert;
}
示例5: GetObsoleteSharedSecret
public static byte[] GetObsoleteSharedSecret(AsymmetricCipherKeyPair localKeyWithPrivate, byte[] remotePublicKeyDerEncoded)
{
var remotePublicKey = PublicKeyFactory.CreateKey(remotePublicKeyDerEncoded);
var agreement = new ECDHBasicAgreement();
agreement.Init(localKeyWithPrivate.Private);
return agreement.CalculateAgreement(remotePublicKey).ToByteArray();
}
示例6: PgpKeyPair
public PgpKeyPair(
PublicKeyAlgorithmTag algorithm,
AsymmetricCipherKeyPair keyPair,
DateTime time)
: this(algorithm, keyPair.Public, keyPair.Private, time)
{
}
示例7: encodeUserKeyPairPublic
public static byte[] encodeUserKeyPairPublic(AsymmetricCipherKeyPair userKey)
{
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(userKey.Public);
byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
return serializedPublicBytes;
}
示例8: GenerateSigningRequestViaOpenSSL
private void GenerateSigningRequestViaOpenSSL(string TargetCertRequestFileName, AsymmetricCipherKeyPair KeyPair)
{
// We expect openssl.exe to exist in the same directory as iPhonePackager
string OpenSSLPath = Path.GetDirectoryName( Application.ExecutablePath ) + @"\openssl.exe";
if (!File.Exists( OpenSSLPath ))
{
MessageBox.Show("A version of OpenSSL is required to generate certificate requests. Please place OpenSSL.exe in Binaries\\DotNET\\IOS", Config.AppDisplayName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string EffectiveBuildPath = (Program.GameName.Length > 0) ? Config.BuildDirectory : Path.GetFullPath(".");
// Create a temporary file to write the key pair out to (in a format that OpenSSL understands)
string KeyFileName = Path.GetTempFileName();
TextWriter KeyWriter = new StreamWriter(KeyFileName);
PemWriter KeyWriterPEM = new PemWriter(KeyWriter);
KeyWriterPEM.WriteObject(KeyPair);
KeyWriter.Close();
// Create a temporary file containing the configuration settings to drive OpenSSL
string ConfigFileName = Path.GetTempFileName();
TextWriter ConfigFile = new StreamWriter(ConfigFileName);
ConfigFile.WriteLine("[ req ]");
ConfigFile.WriteLine("distinguished_name = req_distinguished_name");
ConfigFile.WriteLine("prompt = no");
ConfigFile.WriteLine("[ req_distinguished_name ]");
ConfigFile.WriteLine("emailAddress = {0}", EMailEditBox.Text);
ConfigFile.WriteLine("commonName = {0}", CommonNameEditBox.Text);
ConfigFile.WriteLine("countryName = {0}", System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName);
ConfigFile.Close();
// Invoke OpenSSL to generate the certificate request
Program.Log("Running OpenSSL to generate certificate request...");
string ResultsText;
string Executable = OpenSSLPath;
string Arguments = String.Format("req -new -nodes -out \"{0}\" -key \"{1}\" -config \"{2}\"",
TargetCertRequestFileName, KeyFileName, ConfigFileName);
Utilities.RunExecutableAndWait(Executable, Arguments, out ResultsText);
Program.Log(ResultsText);
if (!File.Exists(TargetCertRequestFileName))
{
Program.Error("... Failed to generate certificate request");
}
else
{
Program.Log("... Successfully generated certificate request '{0}'", TargetCertRequestFileName);
}
// Clean up the temporary files we created
File.Delete(KeyFileName);
File.Delete(ConfigFileName);
}
示例9: PkcsCertificate
/// <summary>
/// Public constructor for a Pkcs certificate, takes in values to describe it's state.
/// </summary>
/// <param name="password">The value used to secure (encrypt) the private key with the certificate</param>
/// <param name="keypair">An object used for manipulating/accessing the public and private key of a certificate</param>
/// <param name="cert">An easy to manipulate version of an X509 certificate</param>
/// <param name="store">A store of Pkcs12 certificates used to store and manipulate certificat chains and key associations</param>
public PkcsCertificate(string password ,AsymmetricCipherKeyPair keypair, Org.BouncyCastle.X509.X509Certificate cert, Pkcs12Store store)
{
this.Password = password;
this.Keypair = keypair;
this.X509Certificate = cert;
this.PkcsStore = store;
// this is not the best place to do this
}
示例10: DkimTests
static DkimTests ()
{
using (var stream = new StreamReader (Path.Combine ("..", "..", "TestData", "dkim", "example.pem"))) {
var reader = new PemReader (stream);
DkimKeys = reader.ReadObject () as AsymmetricCipherKeyPair;
}
}
示例11: ExportCertificate
private static byte[] ExportCertificate(X509Certificate certificate, AsymmetricCipherKeyPair subjectKeyPair, TCertificateFormat certificateFormat)
{
byte[] result = null;
switch (certificateFormat)
{
case TCertificateFormat.NotSet:
break;
case TCertificateFormat.PEM:
using (MemoryStream stream = new MemoryStream())
{
using (StreamWriter writer = new StreamWriter(stream))
{
Org.BouncyCastle.Utilities.IO.Pem.PemWriter pemWriter = new Org.BouncyCastle.Utilities.IO.Pem.PemWriter(writer);
if (subjectKeyPair.Private is ECKeyParameters)
{
ECPrivateKeyParameters priv = (ECPrivateKeyParameters)subjectKeyPair.Private;
ECDomainParameters dp = priv.Parameters;
int orderBitLength = dp.N.BitLength;
Org.BouncyCastle.Asn1.Sec.ECPrivateKeyStructure ec;
Org.BouncyCastle.Asn1.X9.X962Parameters x962;
if (priv.PublicKeyParamSet == null)
{
Org.BouncyCastle.Asn1.X9.X9ECParameters ecP = new Org.BouncyCastle.Asn1.X9.X9ECParameters(dp.Curve, dp.G, dp.N, dp.H, dp.GetSeed());
x962 = new Org.BouncyCastle.Asn1.X9.X962Parameters(ecP);
}
else
{
x962 = new Org.BouncyCastle.Asn1.X9.X962Parameters(priv.PublicKeyParamSet);
}
ec = new Org.BouncyCastle.Asn1.Sec.ECPrivateKeyStructure(orderBitLength, priv.D, SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(subjectKeyPair.Public).PublicKeyData, x962);
pemWriter.WriteObject(new Org.BouncyCastle.Utilities.IO.Pem.PemObject("EC PRIVATE KEY", ec.GetEncoded()));
}
else
{
pemWriter.WriteObject(new Org.BouncyCastle.OpenSsl.MiscPemGenerator(subjectKeyPair.Private));
}
pemWriter.WriteObject(new Org.BouncyCastle.OpenSsl.MiscPemGenerator(subjectKeyPair.Public));
pemWriter.WriteObject(new Org.BouncyCastle.OpenSsl.MiscPemGenerator(certificate));
writer.Flush();
result = stream.ToArray();
}
}
break;
case TCertificateFormat.PFX:
//Asn1Sequence asn1Sequence = Asn1Sequence.GetInstance(Asn1Object.FromByteArray(certificate.GetEncoded()));
//asn1Sequence.GetObjects
//Org.BouncyCastle.Asn1.Pkcs.Pfx pfx = new Org.BouncyCastle.Asn1.Pkcs.Pfx();
//Org.BouncyCastle.Asn1.Pkcs.PrivateKeyInfo info = Org.BouncyCastle.Pkcs.PrivateKeyInfoFactory.CreatePrivateKeyInfo(subjectKeyPair.Private);
//result = pfx.GetEncoded(Asn1Encodable.Der);
break;
case TCertificateFormat.CER:
result = certificate.GetEncoded();
break;
default:
break;
}
return result;
}
示例12: GetPrivateKey
// This returns a
public static string GetPrivateKey(AsymmetricCipherKeyPair keyPair)
{
var dhPrivateKeyParameters = keyPair.Private as DHPrivateKeyParameters;
if (dhPrivateKeyParameters != null)
{
return dhPrivateKeyParameters.X.ToString();
}
throw new NullReferenceException("The key pair provided is not a valid DH keypair.");
}
示例13: DHProvider
public DHProvider(DHParameters parameters)
{
_parameters = parameters;
IAsymmetricCipherKeyPairGenerator keyGen = GeneratorUtilities.GetKeyPairGenerator("DH");
KeyGenerationParameters kgp = new DHKeyGenerationParameters(new SecureRandom(), _parameters);
keyGen.Init(kgp);
_kp = keyGen.GenerateKeyPair();
}
示例14: GetSharedSecret
public static byte[] GetSharedSecret(AsymmetricCipherKeyPair localKeyWithPrivate, byte[] remotePublicKeyDerEncoded)
{
var remotePublicKey = PublicKeyFactory.CreateKey(remotePublicKeyDerEncoded);
var agreement = new ECDHBasicAgreement();
agreement.Init(localKeyWithPrivate.Private);
using (var sha = SHA256.Create()) {
// CalculateAgreement returns a BigInteger, whose length is variable, and bits are not whitened.
// So hash it.
return sha.ComputeHash(agreement.CalculateAgreement(remotePublicKey).ToByteArray());
}
}
示例15: ServerAuthority
public ServerAuthority(DHParameters parameters)
{
this.parameters = parameters;
IAsymmetricCipherKeyPairGenerator keyGen = GeneratorUtilities.GetKeyPairGenerator("DH");
KeyGenerationParameters kgp = new DHKeyGenerationParameters(new SecureRandom(), parameters);
keyGen.Init(kgp);
kp = keyGen.GenerateKeyPair();
agreement = AgreementUtilities.GetBasicAgreement("DH");
agreement.Init(kp.Private);
}