本文整理汇总了C#中Mono.Security.ASN1.GetBytes方法的典型用法代码示例。如果您正苦于以下问题:C# ASN1.GetBytes方法的具体用法?C# ASN1.GetBytes怎么用?C# ASN1.GetBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Security.ASN1
的用法示例。
在下文中一共展示了ASN1.GetBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RSAKeyToASN1
///
/// SEQUENCE (a)
/// +- INTEGER (V) // Version - 0 (v1998)
/// +- SEQUENCE (b)
/// | +- OID (oid) // 1.2.840.113549.1.1.1
/// | +- Nil (c)
/// +- OCTETSTRING(PRVKY) (os) // Private Key Parameter
///
/// However, OCTETSTRING(PRVKY) wraps
/// SEQUENCE(
/// INTEGER(0) // Version - 0 (v1998)
/// INTEGER(N)
/// INTEGER(E)
/// INTEGER(D)
/// INTEGER(P)
/// INTEGER(Q)
/// INTEGER(DP)
/// INTEGER(DQ)
/// INTEGER(InvQ)
/// )
public static byte[] RSAKeyToASN1(RSAParameters PrivateKey) {
ASN1 v = ASN1Convert.FromUnsignedBigInteger(new byte[] {0});
ASN1 b = PKCS7.AlgorithmIdentifier ("1.2.840.113549.1.1.1");
ASN1 os = new ASN1(0x30);
os.Add(ASN1Convert.FromUnsignedBigInteger(new byte[] {0}));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.Modulus));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.Exponent));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.D));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.P));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.Q));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.DP));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.DQ));
os.Add(ASN1Convert.FromUnsignedBigInteger(PrivateKey.InverseQ));
ASN1 pem = new ASN1(0x30);
pem.Add(v);
pem.Add(b);
// Make this into an OCTET string
pem.Add(new ASN1(0x04, os.GetBytes()));
return pem.GetBytes();
}
示例2: Encode
internal byte[] Encode ()
{
ASN1 ex = new ASN1 (0x04, _subjectKeyIdentifier);
return ex.GetBytes ();
}
示例3: 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 ();
}
示例4: GetBytes
public byte[] GetBytes ()
{
if (InnerList.Count < 1)
return null;
ASN1 sequence = new ASN1 (0x30);
for (int i=0; i < InnerList.Count; i++) {
X509Extension x = (X509Extension) InnerList [i];
sequence.Add (x.ASN1);
}
return sequence.GetBytes ();
}
示例5: GetBytes
/*
* SafeContents ::= SEQUENCE OF SafeBag
*
* SafeBag ::= SEQUENCE {
* bagId BAG-TYPE.&id ({PKCS12BagSet}),
* bagValue [0] EXPLICIT BAG-TYPE.&Type({PKCS12BagSet}{@bagId}),
* bagAttributes SET OF PKCS12Attribute OPTIONAL
* }
*/
public byte[] GetBytes ()
{
// TODO (incomplete)
ASN1 safeBagSequence = new ASN1 (0x30);
// Sync Safe Bag list since X509CertificateCollection may be updated
ArrayList scs = new ArrayList ();
foreach (SafeBag sb in _safeBags) {
if (sb.BagOID.Equals (certBag)) {
ASN1 safeBag = sb.ASN1;
ASN1 bagValue = safeBag [1];
PKCS7.ContentInfo cert = new PKCS7.ContentInfo (bagValue.Value);
scs.Add (new X509Certificate (cert.Content [0].Value));
}
}
ArrayList addcerts = new ArrayList ();
ArrayList removecerts = new ArrayList ();
foreach (X509Certificate c in Certificates) {
bool found = false;
foreach (X509Certificate lc in scs) {
if (Compare (c.RawData, lc.RawData)) {
found = true;
}
}
if (!found) {
addcerts.Add (c);
}
}
foreach (X509Certificate c in scs) {
bool found = false;
foreach (X509Certificate lc in Certificates) {
if (Compare (c.RawData, lc.RawData)) {
found = true;
}
}
if (!found) {
removecerts.Add (c);
}
}
foreach (X509Certificate c in removecerts) {
RemoveCertificate (c);
}
foreach (X509Certificate c in addcerts) {
AddCertificate (c);
}
// Sync done
if (_safeBags.Count > 0) {
ASN1 certsSafeBag = new ASN1 (0x30);
foreach (SafeBag sb in _safeBags) {
if (sb.BagOID.Equals (certBag)) {
certsSafeBag.Add (sb.ASN1);
}
}
if (certsSafeBag.Count > 0) {
byte[] certsSalt = new byte [8];
RNG.GetBytes (certsSalt);
ASN1 seqParams = new ASN1 (0x30);
seqParams.Add (new ASN1 (0x04, certsSalt));
seqParams.Add (ASN1Convert.FromInt32 (_iterations));
ASN1 seqPbe = new ASN1 (0x30);
seqPbe.Add (ASN1Convert.FromOid (pbeWithSHAAnd3KeyTripleDESCBC));
seqPbe.Add (seqParams);
byte[] encrypted = Encrypt (pbeWithSHAAnd3KeyTripleDESCBC, certsSalt, _iterations, certsSafeBag.GetBytes ());
ASN1 encryptedCerts = new ASN1 (0x80, encrypted);
ASN1 seq = new ASN1 (0x30);
seq.Add (ASN1Convert.FromOid (PKCS7.Oid.data));
seq.Add (seqPbe);
seq.Add (encryptedCerts);
ASN1 certsVersion = new ASN1 (0x02, new byte [1] { 0x00 });
ASN1 encData = new ASN1 (0x30);
encData.Add (certsVersion);
encData.Add (seq);
ASN1 certsContent = new ASN1 (0xA0);
certsContent.Add (encData);
//.........这里部分代码省略.........
示例6: ProcessSpnegoInitialContextTokenResponse
// Class {
// Class {
// Class { Enum },
// Class { OID(NTLMSSP) },
// Class { OctetStream } } }
public byte [] ProcessSpnegoInitialContextTokenResponse ()
{
ASN1 top = new ASN1 (0xA1);
ASN1 asn = new ASN1 (0x30);
ASN1 asn1 = new ASN1 (0xA0);
// FIXME: what is this enum?
asn1.Add (new ASN1 (0x0A, new byte [] {1})); // Enum whatever
ASN1 asn2 = new ASN1 (0xA1);
asn2.Add (ASN1Convert.FromOid (Constants.OidNtlmSsp));
ASN1 asn3 = new ASN1 (0xA2);
asn3.Add (new ASN1 (0x04, ProcessMessageType2 ()));
asn.Add (asn1);
asn.Add (asn2);
asn.Add (asn3);
top.Add (asn);
return top.GetBytes ();
}
示例7: Sign
public virtual byte[] Sign (DSA key)
{
string oid = "1.2.840.10040.4.3";
ASN1 tbs = ToBeSigned (oid);
HashAlgorithm ha = HashAlgorithm.Create (hashName);
if (!(ha is SHA1))
throw new NotSupportedException ("Only SHA-1 is supported for DSA");
byte[] hash = ha.ComputeHash (tbs.GetBytes ());
DSASignatureFormatter dsa = new DSASignatureFormatter (key);
dsa.SetHashAlgorithm (hashName);
byte[] rs = dsa.CreateSignature (hash);
// split R and S
byte[] r = new byte [20];
Buffer.BlockCopy (rs, 0, r, 0, 20);
byte[] s = new byte [20];
Buffer.BlockCopy (rs, 20, s, 0, 20);
ASN1 signature = new ASN1 (0x30);
signature.Add (new ASN1 (0x02, r));
signature.Add (new ASN1 (0x02, s));
// dsaWithSha1 (1 2 840 10040 4 3)
return Build (tbs, oid, signature.GetBytes ());
}
示例8: Sign
public bool Sign (string fileName)
{
try {
Open (fileName);
HashAlgorithm hash = HashAlgorithm.Create (Hash);
// 0 to 215 (216) then skip 4 (checksum)
byte[] digest = GetHash (hash);
byte[] signature = Header (digest, Hash);
if (timestamp != null) {
byte[] ts = Timestamp (signature);
// add timestamp information inside the current pkcs7 SignedData instance
// (this is possible because the data isn't yet signed)
ProcessTimestamp (ts);
}
PKCS7.ContentInfo sign = new PKCS7.ContentInfo (signedData);
sign.Content.Add (pkcs7.ASN1);
authenticode = sign.ASN1;
Close ();
return Save (fileName, authenticode.GetBytes ());
}
catch (Exception e) {
Console.WriteLine (e);
}
return false;
}
示例9: GetKeyIdentifier
private byte[] GetKeyIdentifier (Mono.Security.X509.X509Certificate x509)
{
// if present in certificate return value of the SubjectKeyIdentifier
Mono.Security.X509.X509Extension extn = x509.Extensions ["2.5.29.14"];
if (extn != null) {
ASN1 bs = new ASN1 (extn.Value.Value);
return bs.Value;
}
// strangely DEPRECATED keyAttributes isn't used here (like KeyUsage)
// if not then we must calculate the SubjectKeyIdentifier ourselve
// Note: MS 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 (x509.KeyAlgorithm)));
// FIXME: does it work for DSA certs (without an 2.5.29.14 extension ?)
algo.Add (new ASN1 (x509.KeyAlgorithmParameters));
byte[] pubkey = x509.PublicKey;
byte[] bsvalue = new byte [pubkey.Length + 1]; // add unused bits (0) before the public key
Array.Copy (pubkey, 0, bsvalue, 1, pubkey.Length);
subjectPublicKeyInfo.Add (new ASN1 (0x03, bsvalue));
SHA1 sha = SHA1.Create ();
return sha.ComputeHash (subjectPublicKeyInfo.GetBytes ());
}
示例10: EncryptedContentInfo
// Creates an encrypted PKCS#7 ContentInfo with safeBags as its SafeContents. Used in GetBytes(), above.
private PKCS7.ContentInfo EncryptedContentInfo(ASN1 safeBags, string algorithmOid)
{
byte[] salt = new byte [8];
RNG.GetBytes (salt);
ASN1 seqParams = new ASN1 (0x30);
seqParams.Add (new ASN1 (0x04, salt));
seqParams.Add (ASN1Convert.FromInt32 (_iterations));
ASN1 seqPbe = new ASN1 (0x30);
seqPbe.Add (ASN1Convert.FromOid (algorithmOid));
seqPbe.Add (seqParams);
byte[] encrypted = Encrypt (algorithmOid, salt, _iterations, safeBags.GetBytes ());
ASN1 encryptedContent = new ASN1 (0x80, encrypted);
ASN1 seq = new ASN1 (0x30);
seq.Add (ASN1Convert.FromOid (PKCS7.Oid.data));
seq.Add (seqPbe);
seq.Add (encryptedContent);
ASN1 version = new ASN1 (0x02, new byte [1] { 0x00 });
ASN1 encData = new ASN1 (0x30);
encData.Add (version);
encData.Add (seq);
ASN1 finalContent = new ASN1 (0xA0);
finalContent.Add (encData);
PKCS7.ContentInfo bag = new PKCS7.ContentInfo (PKCS7.Oid.encryptedData);
bag.Content = finalContent;
return bag;
}
示例11: Sign
public bool Sign (string fileName)
{
string hashAlgorithm = "MD5";
byte[] file = null;
using (FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
file = new byte [fs.Length];
fs.Read (file, 0, file.Length);
fs.Close ();
}
// MZ - DOS header
if (BitConverterLE.ToUInt16 (file, 0) != 0x5A4D)
return false;
// find offset of PE header
int peOffset = BitConverterLE.ToInt32 (file, 60);
if (peOffset > file.Length)
return false;
// PE - NT header
if (BitConverterLE.ToUInt16 (file, peOffset) != 0x4550)
return false;
// IMAGE_DIRECTORY_ENTRY_SECURITY
int dirSecurityOffset = BitConverterLE.ToInt32 (file, peOffset + 152);
int dirSecuritySize = BitConverterLE.ToInt32 (file, peOffset + 156);
if (dirSecuritySize > 8) {
entry = new byte [dirSecuritySize - 8];
Buffer.BlockCopy (file, dirSecurityOffset + 8, entry, 0, entry.Length);
}
else
entry = null;
HashAlgorithm hash = HashAlgorithm.Create (hashAlgorithm);
// 0 to 215 (216) then skip 4 (checksum)
int pe = peOffset + 88;
hash.TransformBlock (file, 0, pe, file, 0);
pe += 4;
// 220 to 279 (60) then skip 8 (IMAGE_DIRECTORY_ENTRY_SECURITY)
hash.TransformBlock (file, pe, 60, file, pe);
pe += 68;
// 288 to end of file
int n = file.Length - pe;
// minus any authenticode signature (with 8 bytes header)
if (dirSecurityOffset != 0)
n -= (dirSecuritySize);
hash.TransformFinalBlock (file, pe, n);
//
byte[] signature = Header (hash.Hash, hashAlgorithm);
if (timestamp != null) {
ASN1 tsreq = TimestampRequest (signature);
WebClient wc = new WebClient ();
wc.Headers.Add ("Content-Type", "application/octet-stream");
wc.Headers.Add ("Accept", "application/octet-stream");
byte[] tsdata = Encoding.ASCII.GetBytes (Convert.ToBase64String (tsreq.GetBytes ()));
byte[] tsres = wc.UploadData (timestamp.ToString (), tsdata);
ProcessTimestamp (tsres);
}
PKCS7.ContentInfo sign = new PKCS7.ContentInfo (signedData);
sign.Content.Add (pkcs7.ASN1);
authenticode = sign.ASN1;
byte[] asn = authenticode.GetBytes ();
#if DEBUG
using (FileStream fs = File.Open (fileName + ".sig", FileMode.Create, FileAccess.Write)) {
fs.Write (asn, 0, asn.Length);
fs.Close ();
}
#endif
// someday I may be sure enough to move this into DEBUG ;-)
File.Copy (fileName, fileName + ".bak", true);
using (FileStream fs = File.Open (fileName, FileMode.Create, FileAccess.Write)) {
int filesize = (dirSecurityOffset == 0) ? file.Length : dirSecurityOffset;
// IMAGE_DIRECTORY_ENTRY_SECURITY (offset, size)
byte[] data = BitConverterLE.GetBytes (filesize);
file [peOffset + 152] = data [0];
file [peOffset + 153] = data [1];
file [peOffset + 154] = data [2];
file [peOffset + 155] = data [3];
int size = asn.Length + 8;
// must be a multiple of 8 bytes
int addsize = (size % 8);
if (addsize > 0)
addsize = 8 - addsize;
size += addsize;
data = BitConverterLE.GetBytes (size); // header
file [peOffset + 156] = data [0];
file [peOffset + 157] = data [1];
file [peOffset + 158] = data [2];
file [peOffset + 159] = data [3];
fs.Write (file, 0, filesize);
fs.Write (data, 0, data.Length); // length (again)
data = BitConverterLE.GetBytes (0x00020200); // magic
fs.Write (data, 0, data.Length);
fs.Write (asn, 0, asn.Length);
// fill up
//.........这里部分代码省略.........
示例12: ConvertOID_LargeX
public void ConvertOID_LargeX ()
{
ASN1 asn = new ASN1 (0x06, new byte [] { 0xA8, 0x00, 0x00 });
string oid = ASN1Convert.ToOid (asn);
Assert.AreEqual ("2.88.0.0", oid, "ToOID");
Assert.AreEqual (BitConverter.ToString (asn.GetBytes ()),
BitConverter.ToString (ASN1Convert.FromOid (oid).GetBytes ()), "FromOID");
}
示例13: VerifySignature
public bool VerifySignature (AsymmetricAlgorithm aa)
{
if (aa == null) {
return false;
}
RSAPKCS1SignatureDeformatter r = new RSAPKCS1SignatureDeformatter (aa);
r.SetHashAlgorithm (hashAlgorithm);
HashAlgorithm ha = HashAlgorithm.Create (hashAlgorithm);
byte[] signature = signerInfo.Signature;
byte[] hash = null;
if (mda) {
ASN1 asn = new ASN1 (0x31);
foreach (ASN1 attr in signerInfo.AuthenticatedAttributes)
asn.Add (attr);
hash = ha.ComputeHash (asn.GetBytes ());
} else {
hash = ha.ComputeHash (contentInfo.Content[0].Value);
}
if (hash != null && signature != null) {
return r.VerifySignature (hash, signature);
}
return false;
}
示例14: ProcessSpnegoInitialContextTokenRequest
// Class(60) {
// OID(spnego),
// Class(A0) {
// Class(30) {
// Class(A0) {
// Class(30) { OID,OID,OID} },
// Class(A2) { OctetStream } } } }
public byte [] ProcessSpnegoInitialContextTokenRequest ()
{
Type1Message type1 = new Type1Message (NtlmVersion.Version3);
type1.Flags = unchecked ((NtlmFlags) 0xE21882B7);
type1.Domain = "WORKGROUP"; // FIXME: remove it
ASN1 asn = new ASN1 (0x60);
ASN1 asn2 = new ASN1 (0xA0);
ASN1 asn21 = new ASN1 (0x30);
ASN1 asn211 = new ASN1 (0xA0);
ASN1 asn2111 = new ASN1 (0x30);
asn211.Add (asn2111);
asn2111.Add (ASN1Convert.FromOid (Constants.OidNtlmSsp));
asn2111.Add (ASN1Convert.FromOid (Constants.OidKerberos5));
asn2111.Add (ASN1Convert.FromOid (Constants.OidMIT));
ASN1 asn212 = new ASN1 (0xA2);
ASN1 asn2121 = new ASN1 (0x4);
asn2121.Value = type1.GetBytes ();
asn212.Add (asn2121);
asn21.Add (asn211);
asn21.Add (asn212);
asn2.Add (asn21);
asn.Add (ASN1Convert.FromOid (Constants.OidSpnego));
asn.Add (asn2);
return asn.GetBytes ();
}
示例15: Encode
internal byte[] Encode ()
{
ASN1 ex = new ASN1 (0x30);
if (_certificateAuthority)
ex.Add (new ASN1 (0x01, new byte[] { 0xFF }));
if (_hasPathLengthConstraint) {
// MS encodes the 0 (pathLengthConstraint is OPTIONAL)
// and in a long form (02 00 versus 02 01 00)
if (_pathLengthConstraint == 0)
ex.Add (new ASN1 (0x02, new byte[] { 0x00 }));
else
ex.Add (ASN1Convert.FromInt32 (_pathLengthConstraint));
}
return ex.GetBytes ();
}