GetBytes() public method

public GetBytes ( ) : byte[]
return byte[]
Beispiel #1
0
		public void PrivateKeyInfo () 
		{
			PKCS8.PrivateKeyInfo p8 = new PKCS8.PrivateKeyInfo ();
			Assert.IsNull (p8.Algorithm, "Default-Algorithm");
			Assert.AreEqual (0, p8.Attributes.Count, "Default-Attributes");
			Assert.IsNull (p8.PrivateKey, "Default-PrivateKey");
			Assert.AreEqual (0, p8.Version, "Default-Version");
			
			byte[] key = new byte [8];
			p8.Algorithm = "1.2.3.4.5";
			p8.Attributes.Add (new ASN1 (0x05)); // NULL
			p8.PrivateKey = key;
			p8.Version = 1;
			Assert.AreEqual ("1.2.3.4.5", p8.Algorithm, "Algorithm");
			Assert.AreEqual (1, p8.Attributes.Count, "Attributes");
			Assert.AreEqual (8, p8.PrivateKey.Length, "PrivateKey");
			Assert.AreEqual (1, p8.Version, "Version");
			
			key [0] = 1;
			Assert.AreEqual (0, p8.PrivateKey [0], "PrivateKey not directly accessible");
			
			byte[] p8pki = p8.GetBytes ();
			
			PKCS8.PrivateKeyInfo decoded = new PKCS8.PrivateKeyInfo (p8pki);
			Assert.AreEqual ("1.2.3.4.5", decoded.Algorithm, "Decoded-Algorithm");
			Assert.AreEqual (1, decoded.Attributes.Count, "Decoded-Attributes");
			Assert.AreEqual (8, decoded.PrivateKey.Length, "Decoded-PrivateKey");
			Assert.AreEqual (1, decoded.Version, "Decoded-Version");
		}
		private ASN1 KeyBagSafeBag (AsymmetricAlgorithm aa, IDictionary attributes) 
		{
			PKCS8.PrivateKeyInfo pki = new PKCS8.PrivateKeyInfo ();
			if (aa is RSA) {
				pki.Algorithm = "1.2.840.113549.1.1.1";
				pki.PrivateKey = PKCS8.PrivateKeyInfo.Encode ((RSA)aa);
			}
			else if (aa is DSA) {
				pki.Algorithm = null;
				pki.PrivateKey = PKCS8.PrivateKeyInfo.Encode ((DSA)aa);
			}
			else
				throw new CryptographicException ("Unknown asymmetric algorithm {0}", aa.ToString ());

			ASN1 safeBag = new ASN1 (0x30);
			safeBag.Add (ASN1Convert.FromOid (keyBag));
			ASN1 bagValue = new ASN1 (0xA0);
			bagValue.Add (new ASN1 (pki.GetBytes ()));
			safeBag.Add (bagValue);

			if (attributes != null) {
				ASN1 bagAttributes = new ASN1 (0x31);
				IDictionaryEnumerator de = attributes.GetEnumerator ();

				while (de.MoveNext ()) {
					string oid = (string)de.Key;
					switch (oid) {
					case PKCS9.friendlyName:
						ArrayList names = (ArrayList)de.Value;
						if (names.Count > 0) {
							ASN1 pkcs12Attribute = new ASN1 (0x30);
							pkcs12Attribute.Add (ASN1Convert.FromOid (PKCS9.friendlyName));
							ASN1 attrValues = new ASN1 (0x31);
							foreach (byte[] name in names) {
								ASN1 attrValue = new ASN1 (0x1e);
								attrValue.Value = name;
								attrValues.Add (attrValue);
							}
							pkcs12Attribute.Add (attrValues);
							bagAttributes.Add (pkcs12Attribute);
						}
						break;
					case PKCS9.localKeyId:
						ArrayList keys = (ArrayList)de.Value;
						if (keys.Count > 0) {
							ASN1 pkcs12Attribute = new ASN1 (0x30);
							pkcs12Attribute.Add (ASN1Convert.FromOid (PKCS9.localKeyId));
							ASN1 attrValues = new ASN1 (0x31);
							foreach (byte[] key in keys) {
								ASN1 attrValue = new ASN1 (0x04);
								attrValue.Value = key;
								attrValues.Add (attrValue);
							}
							pkcs12Attribute.Add (attrValues);
							bagAttributes.Add (pkcs12Attribute);
						}
						break;
					default:
						break;
					}
				}

				if (bagAttributes.Count > 0) {
					safeBag.Add (bagAttributes);
				}
			}

			return safeBag;
		}
Beispiel #3
0
		public void PrivateKeyInfo () 
		{
			PKCS8.PrivateKeyInfo p8 = new PKCS8.PrivateKeyInfo ();
			AssertNull ("Default-Algorithm", p8.Algorithm);
			AssertEquals ("Default-Attributes", 0, p8.Attributes.Count);
			AssertNull ("Default-PrivateKey", p8.PrivateKey);
			AssertEquals ("Default-Version", 0, p8.Version);
			
			byte[] key = new byte [8];
			p8.Algorithm = "1.2.3.4.5";
			p8.Attributes.Add (new ASN1 (0x05)); // NULL
			p8.PrivateKey = key;
			p8.Version = 1;
			AssertEquals ("Algorithm", "1.2.3.4.5", p8.Algorithm);
			AssertEquals ("Attributes", 1, p8.Attributes.Count);
			AssertEquals ("PrivateKey", 8, p8.PrivateKey.Length);
			AssertEquals ("Version", 1, p8.Version);
			
			key [0] = 1;
			AssertEquals ("PrivateKey not directly accessible", 0, p8.PrivateKey [0]);
			
			byte[] p8pki = p8.GetBytes ();
			
			PKCS8.PrivateKeyInfo decoded = new PKCS8.PrivateKeyInfo (p8pki);
			AssertEquals ("Decoded-Algorithm", "1.2.3.4.5", decoded.Algorithm);
			AssertEquals ("Decoded-Attributes", 1, decoded.Attributes.Count);
			AssertEquals ("Decoded-PrivateKey", 8, decoded.PrivateKey.Length);
			AssertEquals ("Decoded-Version", 1, decoded.Version);
		}