本文整理匯總了C#中System.Security.Cryptography.X509Certificates.PublicKey類的典型用法代碼示例。如果您正苦於以下問題:C# PublicKey類的具體用法?C# PublicKey怎麽用?C# PublicKey使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PublicKey類屬於System.Security.Cryptography.X509Certificates命名空間,在下文中一共展示了PublicKey類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: X509SubjectKeyIdentifierExtension
public X509SubjectKeyIdentifierExtension(PublicKey key, bool critical)
{
Contract.Requires(key.EncodedKeyValue != null);
Contract.Requires(key.EncodedKeyValue.RawData != null);
Contract.Requires(key.EncodedParameters != null);
Contract.Requires(key.EncodedParameters.RawData != null);
Contract.Ensures(key.EncodedParameters.RawData != null);
}
開發者ID:asvishnyakov,項目名稱:CodeContracts,代碼行數:8,代碼來源:System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension.cs
示例2: Reset
public override void Reset()
{
_lazyRawData = null;
_lazySignatureAlgorithm = null;
_lazyVersion = 0;
_lazySubjectName = null;
_lazyIssuerName = null;
_lazyPublicKey = null;
_lazyPrivateKey = null;
_lazyExtensions = null;
base.Reset();
}
示例3: ComputeCapiSha1OfPublicKey
public byte[] ComputeCapiSha1OfPublicKey(PublicKey key)
{
// The CapiSha1 value is the SHA-1 of the SubjectPublicKeyInfo field, inclusive
// of the DER structural bytes.
//SubjectPublicKeyInfo::= SEQUENCE {
// algorithm AlgorithmIdentifier{ { SupportedAlgorithms} },
// subjectPublicKey BIT STRING,
// ... }
//
//AlgorithmIdentifier{ ALGORITHM: SupportedAlgorithms} ::= SEQUENCE {
// algorithm ALGORITHM.&id({ SupportedAlgorithms}),
// parameters ALGORITHM.&Type({ SupportedAlgorithms}
// { @algorithm}) OPTIONAL,
// ... }
//
//ALGORITHM::= CLASS {
// &Type OPTIONAL,
// &id OBJECT IDENTIFIER UNIQUE }
//WITH SYNTAX {
// [&Type]
//IDENTIFIED BY &id }
// key.EncodedKeyValue corresponds to SubjectPublicKeyInfo.subjectPublicKey, except it
// has had the BIT STRING envelope removed.
//
// key.EncodedParameters corresponds to AlgorithmIdentifier.Parameters precisely
// (DER NULL for RSA, DER Constructed SEQUENCE for DSA)
byte[] empty = Array.Empty<byte>();
byte[][] algorithmOid = DerEncoder.SegmentedEncodeOid(key.Oid);
// Because ConstructSegmentedSequence doesn't look to see that it really is tag+length+value (but does check
// that the array has length 3), just hide the joined TLV triplet in the last element.
byte[][] segmentedParameters = { empty, empty, key.EncodedParameters.RawData };
byte[][] algorithmIdentifier = DerEncoder.ConstructSegmentedSequence(algorithmOid, segmentedParameters);
byte[][] subjectPublicKey = DerEncoder.SegmentedEncodeBitString(key.EncodedKeyValue.RawData);
using (SHA1 hash = SHA1.Create())
{
return hash.ComputeHash(
DerEncoder.ConstructSequence(
algorithmIdentifier,
subjectPublicKey));
}
}
示例4: Constructor_Dsa_FromScratch
public void Constructor_Dsa_FromScratch ()
{
// providing Oid for parameters and keyvalue isn't required
PublicKey pk = new PublicKey (new Oid (dsaOid.Value),
new AsnEncodedData (dsa_params),
new AsnEncodedData (dsa_public_key));
Assert.AreEqual (dsaOid.Value, pk.Oid.Value, "Oid.Value");
Assert.IsNull (pk.EncodedParameters.Oid, "EncodedParameters.Oid");
Assert.AreEqual (BitConverter.ToString (dsa_params), BitConverter.ToString (pk.EncodedParameters.RawData), "EncodedParameters.RawData");
Assert.IsNull (pk.EncodedKeyValue.Oid, "EncodedKeyValue.Oid");
Assert.AreEqual (BitConverter.ToString (dsa_public_key), BitConverter.ToString (pk.EncodedKeyValue.RawData), "EncodedKeyValue.RawData");
Assert.AreEqual (dsa_public_key_xml, pk.Key.ToXmlString (false), "Key");
Assert.IsTrue ((pk.Key as DSACryptoServiceProvider).PublicOnly, "Key.PublicOnly");
}
示例5: ExportPublicKey
private static byte[] ExportPublicKey(PublicKey key)
{
// From: http://pstaev.blogspot.fr/2010/08/convert-rsa-public-key-from-xml-to-pem.html
byte[] oid = { 0x30, 0xD, 0x6, 0x9, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0xD, 0x1, 0x1, 0x1, 0x5, 0x0 }; // Object ID for RSA
//Transform the public key to PEM Base64 Format
List<byte> binaryPublicKey = key.EncodedKeyValue.RawData.ToList();
binaryPublicKey.Insert(0, 0x0); // Add NULL value
CalculateAndAppendLength(ref binaryPublicKey);
binaryPublicKey.Insert(0, 0x3);
binaryPublicKey.InsertRange(0, oid);
CalculateAndAppendLength(ref binaryPublicKey);
binaryPublicKey.Insert(0, 0x30);
return binaryPublicKey.ToArray();
}
示例6: X509SubjectKeyIdentifierExtension
public X509SubjectKeyIdentifierExtension(PublicKey key, bool critical)
: this(key, X509SubjectKeyIdentifierHashAlgorithm.Sha1, critical)
{
}
示例7: EncodeExtension
private static byte[] EncodeExtension(PublicKey key, X509SubjectKeyIdentifierHashAlgorithm algorithm)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
byte[] subjectKeyIdentifier = GenerateSubjectKeyIdentifierFromPublicKey(key, algorithm);
return EncodeExtension(subjectKeyIdentifier);
}
示例8: EncodePublicKey
// Construct CERT_PUBLIC_KEY_INFO2 in unmanged memory from given encoded blobs.
private static unsafe SafeLocalAllocHandle EncodePublicKey (PublicKey key) {
SafeLocalAllocHandle publicKeyInfo = SafeLocalAllocHandle.InvalidHandle;
CAPI.CERT_PUBLIC_KEY_INFO2 * pPublicKeyInfo = null;
string objId = key.Oid.Value;
byte[] encodedParameters = key.EncodedParameters.RawData;
byte[] encodedKeyValue = key.EncodedKeyValue.RawData;
uint cbPublicKeyInfo = (uint) (Marshal.SizeOf(typeof(CAPI.CERT_PUBLIC_KEY_INFO2)) +
X509Utils.AlignedLength((uint) (objId.Length + 1)) +
X509Utils.AlignedLength((uint) encodedParameters.Length) +
encodedKeyValue.Length);
publicKeyInfo = CAPI.LocalAlloc(CAPI.LPTR, new IntPtr(cbPublicKeyInfo));
pPublicKeyInfo = (CAPI.CERT_PUBLIC_KEY_INFO2 *) publicKeyInfo.DangerousGetHandle();
IntPtr pszObjId = new IntPtr((long) pPublicKeyInfo + Marshal.SizeOf(typeof(CAPI.CERT_PUBLIC_KEY_INFO2)));
IntPtr pbParameters = new IntPtr((long) pszObjId + X509Utils.AlignedLength(((uint) (objId.Length + 1))));
IntPtr pbPublicKey = new IntPtr((long) pbParameters + X509Utils.AlignedLength((uint) encodedParameters.Length));
pPublicKeyInfo->Algorithm.pszObjId = pszObjId;
byte[] szObjId = new byte[objId.Length + 1];
Encoding.ASCII.GetBytes(objId, 0, objId.Length, szObjId, 0);
Marshal.Copy(szObjId, 0, pszObjId, szObjId.Length);
if (encodedParameters.Length > 0) {
pPublicKeyInfo->Algorithm.Parameters.cbData = (uint) encodedParameters.Length;
pPublicKeyInfo->Algorithm.Parameters.pbData = pbParameters;
Marshal.Copy(encodedParameters, 0, pbParameters, encodedParameters.Length);
}
pPublicKeyInfo->PublicKey.cbData = (uint) encodedKeyValue.Length;
pPublicKeyInfo->PublicKey.pbData = pbPublicKey;
Marshal.Copy(encodedKeyValue, 0, pbPublicKey, encodedKeyValue.Length);
return publicKeyInfo;
}
示例9: Constructor_Dsa_UnknownOid_Key
public void Constructor_Dsa_UnknownOid_Key ()
{
// providing Oid for parameters and keyvalue isn't required
PublicKey pk = new PublicKey (new Oid (unknownOid.Value),
new AsnEncodedData (dsa_params),
new AsnEncodedData (dsa_public_key));
Assert.AreEqual (dsa_public_key_xml, pk.Key.ToXmlString (false), "Key");
}
示例10: Constructor_Dsa_UnknownOid
public void Constructor_Dsa_UnknownOid ()
{
// providing Oid for parameters and keyvalue isn't required
PublicKey pk = new PublicKey (new Oid (unknownOid.Value),
new AsnEncodedData (dsa_params),
new AsnEncodedData (dsa_public_key));
Assert.AreEqual (unknownOid.Value, pk.Oid.Value, "Oid.Value");
Assert.IsNull (pk.EncodedParameters.Oid, "EncodedParameters.Oid");
Assert.AreEqual (BitConverter.ToString (dsa_params), BitConverter.ToString (pk.EncodedParameters.RawData), "EncodedParameters.RawData");
Assert.IsNull (pk.EncodedKeyValue.Oid, "EncodedKeyValue.Oid");
Assert.AreEqual (BitConverter.ToString (dsa_public_key), BitConverter.ToString (pk.EncodedKeyValue.RawData), "EncodedKeyValue.RawData");
}
示例11: Constructor_Dsa_WeirdParameters_Key
public void Constructor_Dsa_WeirdParameters_Key ()
{
PublicKey pk = new PublicKey (new Oid (dsaOid.Value),
new AsnEncodedData (new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }),
new AsnEncodedData (dsa_public_key));
Assert.AreEqual (dsa_public_key_xml, pk.Key.ToXmlString (false), "Key");
}
示例12: Constructor_Dsa_WeirdParameters
public void Constructor_Dsa_WeirdParameters ()
{
PublicKey pk = new PublicKey (new Oid (dsaOid.Value),
new AsnEncodedData (new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }),
new AsnEncodedData (dsa_public_key));
Assert.AreEqual (dsaOid.Value, pk.Oid.Value, "Oid.Value");
Assert.IsNull (pk.EncodedParameters.Oid, "EncodedParameters.Oid");
Assert.AreEqual ("00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F", BitConverter.ToString (pk.EncodedParameters.RawData), "EncodedParameters.RawData");
Assert.IsNull (pk.EncodedKeyValue.Oid, "EncodedKeyValue.Oid");
Assert.AreEqual (BitConverter.ToString (dsa_public_key), BitConverter.ToString (pk.EncodedKeyValue.RawData), "EncodedKeyValue.RawData");
}
示例13: Constructor_Dsa_EmptyParameters_Key
public void Constructor_Dsa_EmptyParameters_Key ()
{
PublicKey pk = new PublicKey (new Oid (dsaOid.Value),
new AsnEncodedData (new byte[0]), // same as NULL (0x05, 0x00)
new AsnEncodedData (dsa_public_key));
Assert.AreEqual (dsa_public_key_xml, pk.Key.ToXmlString (false), "Key");
}
示例14: Constructor_Dsa_EmptyParameters
public void Constructor_Dsa_EmptyParameters ()
{
PublicKey pk = new PublicKey (new Oid (dsaOid.Value),
new AsnEncodedData (new byte[0]), // same as NULL (0x05, 0x00)
new AsnEncodedData (dsa_public_key));
Assert.AreEqual (dsaOid.Value, pk.Oid.Value, "Oid.Value");
Assert.IsNull (pk.EncodedParameters.Oid, "EncodedParameters.Oid");
Assert.AreEqual (String.Empty, BitConverter.ToString (pk.EncodedParameters.RawData), "EncodedParameters.RawData");
Assert.IsNull (pk.EncodedKeyValue.Oid, "EncodedKeyValue.Oid");
Assert.AreEqual (BitConverter.ToString (dsa_public_key), BitConverter.ToString (pk.EncodedKeyValue.RawData), "EncodedKeyValue.RawData");
}
示例15: X509SubjectKeyIdentifierExtension
public X509SubjectKeyIdentifierExtension (PublicKey key, X509SubjectKeyIdentifierHashAlgorithm algorithm, bool critical)
{
if (key == null)
throw new ArgumentNullException ("key");
byte[] pkraw = key.EncodedKeyValue.RawData;
// compute SKI
switch (algorithm) {
// hash of the public key, excluding Tag, Length and unused bits values
case X509SubjectKeyIdentifierHashAlgorithm.Sha1:
_subjectKeyIdentifier = SHA1.Create ().ComputeHash (pkraw);
break;
// 0100 bit pattern followed by the 60 last bit of the hash
case X509SubjectKeyIdentifierHashAlgorithm.ShortSha1:
byte[] hash = SHA1.Create ().ComputeHash (pkraw);
_subjectKeyIdentifier = new byte [8];
Buffer.BlockCopy (hash, 12, _subjectKeyIdentifier, 0, 8);
_subjectKeyIdentifier [0] = (byte) (0x40 | (_subjectKeyIdentifier [0] & 0x0F));
break;
// hash of the public key, including Tag, Length and unused bits values
case X509SubjectKeyIdentifierHashAlgorithm.CapiSha1:
// CryptoAPI does that hash on the complete subjectPublicKeyInfo (unlike PKIX)
// http://groups.google.ca/groups?selm=e7RqM%24plCHA.1488%40tkmsftngp02&oe=UTF-8&output=gplain
ASN1 subjectPublicKeyInfo = new ASN1 (0x30);
ASN1 algo = subjectPublicKeyInfo.Add (new ASN1 (0x30));
algo.Add (new ASN1 (CryptoConfig.EncodeOID (key.Oid.Value)));
algo.Add (new ASN1 (key.EncodedParameters.RawData));
// add an extra byte for the unused bits (none)
byte[] full = new byte [pkraw.Length + 1];
Buffer.BlockCopy (pkraw, 0, full, 1, pkraw.Length);
subjectPublicKeyInfo.Add (new ASN1 (0x03, full));
_subjectKeyIdentifier = SHA1.Create ().ComputeHash (subjectPublicKeyInfo.GetBytes ());
break;
default:
throw new ArgumentException ("algorithm");
}
_oid = new Oid (oid, friendlyName);
base.Critical = critical;
RawData = Encode ();
}