// methods private void Decode(byte[] data) { ASN1 encryptedPrivateKeyInfo = new ASN1(data); if (encryptedPrivateKeyInfo.Tag != 0x30) { throw new CryptographicException("invalid EncryptedPrivateKeyInfo"); } ASN1 encryptionAlgorithm = encryptedPrivateKeyInfo[0]; if (encryptionAlgorithm.Tag != 0x30) { throw new CryptographicException("invalid encryptionAlgorithm"); } ASN1 algorithm = encryptionAlgorithm[0]; if (algorithm.Tag != 0x06) { throw new CryptographicException("invalid algorithm"); } _algorithm = ASN1Convert.ToOid(algorithm); // parameters ANY DEFINED BY algorithm OPTIONAL if (encryptionAlgorithm.Count > 1) { ASN1 parameters = encryptionAlgorithm[1]; if (parameters.Tag != 0x30) { throw new CryptographicException("invalid parameters"); } ASN1 salt = parameters[0]; if (salt.Tag != 0x04) { throw new CryptographicException("invalid salt"); } _salt = salt.Value; ASN1 iterationCount = parameters[1]; if (iterationCount.Tag != 0x02) { throw new CryptographicException("invalid iterationCount"); } _iterations = ASN1Convert.ToInt32(iterationCount); } ASN1 encryptedData = encryptedPrivateKeyInfo[1]; if (encryptedData.Tag != 0x04) { throw new CryptographicException("invalid EncryptedData"); } _data = encryptedData.Value; }
// methods private void Decode(byte[] data) { ASN1 privateKeyInfo = new ASN1(data); if (privateKeyInfo.Tag != 0x30) { throw new CryptographicException("invalid PrivateKeyInfo"); } ASN1 version = privateKeyInfo[0]; if (version.Tag != 0x02) { throw new CryptographicException("invalid version"); } _version = version.Value[0]; ASN1 privateKeyAlgorithm = privateKeyInfo[1]; if (privateKeyAlgorithm.Tag != 0x30) { throw new CryptographicException("invalid algorithm"); } ASN1 algorithm = privateKeyAlgorithm[0]; if (algorithm.Tag != 0x06) { throw new CryptographicException("missing algorithm OID"); } _algorithm = ASN1Convert.ToOid(algorithm); ASN1 privateKey = privateKeyInfo[2]; _key = privateKey.Value; // attributes [0] IMPLICIT Attributes OPTIONAL if (privateKeyInfo.Count > 3) { ASN1 attributes = privateKeyInfo[3]; for (int i = 0; i < attributes.Count; i++) { _list.Add(attributes[i]); } } }
public static RSAParameters DecodePkcs8PublicKey(byte[] pkcs8blob) { // See: http://tools.ietf.org/html/rfc5280 // http://www.cryptopp.com/wiki/Keys_and_Formats#Dumping_PKCS_.238_and_X.509_Keys // // SubjectPublicKeyInfo ::= SEQUENCE { // algorithm AlgorithmIdentifier, // subjectPublicKey BIT STRING } ASN1 publicKeyInfo = new ASN1(pkcs8blob); if (publicKeyInfo.Tag != 0x30) { throw new CryptographicException("invalid PublicKeyInfo"); } ASN1 keyAlgorithm = publicKeyInfo[0]; if (keyAlgorithm.Tag != 0x30) { throw new CryptographicException("invalid algorithm"); } ASN1 algorithm = keyAlgorithm[0]; if (algorithm.Tag != 0x06) { throw new CryptographicException("missing algorithm OID"); } if (ASN1Convert.ToOid(algorithm) != Oid.rsaEncryption) { throw new NotSupportedException("Only RSA keys are currently supported."); } var keyBits = publicKeyInfo[1]; if (keyBits.Tag != 0x03) { throw new CryptographicException("Invalid or corrupted key container, expecint a BIT STRING, found tag: " + keyBits.Tag); } var sanitizedKeyBits = RemoveLeadingZero(keyBits.Value); return(DecodeAsn1RsaPublicKey(new ASN1(sanitizedKeyBits))); }