/// <summary> /// Зашифровать информацию о ключе. /// </summary> public byte[] Encode() { var asnEncoder = new Asn1BerEncodeBuffer(); var keyTransport = new Gost_R3410_KeyTransport(); try { keyTransport.SessionEncryptedKey = new Gost_28147_89_EncryptedKey { EncryptedKey = new Gost_28147_89_Key(SessionEncryptedKey.EncryptedKey), MacKey = new Gost_28147_89_Mac(SessionEncryptedKey.Mac) }; keyTransport.TransportParams = new Gost_R3410_TransportParams { EncryptionParamSet = Gost_28147_89_ParamSet.FromString(SessionEncryptedKey.EncryptionParamSet), EphemeralPublicKey = EncodePublicKey(TransportParameters), Ukm = new Asn1OctetString(SessionEncryptedKey.Ukm) }; keyTransport.Encode(asnEncoder); } catch (Exception exception) { throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, nameof(Gost_R3410_KeyTransport)); } return(asnEncoder.MsgCopy); }
private void DecodeSessionKey(Gost_R3410_KeyTransport keyTransport) { SessionEncryptedKey = new Gost_28147_89_KeyExchangeInfo { EncryptionParamSet = keyTransport.TransportParams.EncryptionParamSet.Oid.Value, EncryptedKey = keyTransport.SessionEncryptedKey.EncryptedKey.Value, Mac = keyTransport.SessionEncryptedKey.MacKey.Value, Ukm = keyTransport.TransportParams.Ukm.Value }; }
/// <summary> /// Расшифровать информацию о ключе. /// </summary> public void Decode(byte[] data) { if (data == null) { throw ExceptionUtility.ArgumentNull(nameof(data)); } try { var asnDecoder = new Asn1BerDecodeBuffer(data); var keyTransport = new Gost_R3410_KeyTransport(); keyTransport.Decode(asnDecoder); DecodeSessionKey(keyTransport); DecodePublicKey(keyTransport); } catch (Exception exception) { throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, nameof(Gost_R3410_KeyTransport)); } }
private void DecodePublicKey(Gost_R3410_KeyTransport keyTransport) { var publicKeyInfo = keyTransport.TransportParams.EphemeralPublicKey; var publicKeyAlgOid = publicKeyInfo.Algorithm.Algorithm.Oid.Value; if (!publicKeyAlgOid.Equals(KeyAlgorithm.Value)) { throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlg, publicKeyAlgOid); } var choice = publicKeyInfo.Algorithm.Parameters as Asn1Choice; if (choice == null) { throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlgorithmParameters); } var publicKeyParams = choice.GetElement() as Gost_R3410_PublicKeyParams; if (publicKeyParams == null) { throw ExceptionUtility.CryptographicException(Resources.Asn1DecodeAlgorithmParameters); } var asnDecoder = new Asn1BerDecodeBuffer(publicKeyInfo.SubjectPublicKey.Value); var publicKey = new Asn1OctetString(); publicKey.Decode(asnDecoder); TransportParameters = CreateKeyExchangeParams(); TransportParameters.DigestParamSet = publicKeyParams.DigestParamSet.Oid.Value; TransportParameters.PublicKeyParamSet = publicKeyParams.PublicKeyParamSet.Oid.Value; TransportParameters.EncryptionParamSet = publicKeyParams.EncryptionParamSet?.Oid.Value; TransportParameters.PublicKey = publicKey.Value; TransportParameters.PrivateKey = null; }