public void Decode(byte[] data) { if (data == null) { throw ExceptionUtility.ArgumentNull("data"); } try { var asnDecoder = new Asn1BerDecodeBuffer(data); var keyTransport = new GostR3410KeyTransport(); keyTransport.Decode(asnDecoder); SessionEncryptedKey = DecodeSessionKey(keyTransport); TransportParameters = DecodePublicKey(keyTransport); } catch (Exception exception) { throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, typeof(GostR3410KeyTransport).FullName); } }
public void Decode(byte[] data) { if (data == null) { throw ExceptionUtility.ArgumentNull("data"); } try { var s = Asn1Sequence.FromByteArray(data) as Asn1Sequence; var s0 = s[0] as Asn1Sequence; var s1 = (s[1] as Asn1TaggedObject).GetObject() as Asn1Sequence; var s11 = (s1[1] as Asn1TaggedObject).GetObject() as Asn1Sequence; var s1101 = (s11[0] as Asn1Sequence)[1] as Asn1Sequence; SessionEncryptedKey = new GostKeyExchangeInfo { EncryptionParamSet = (s1[0] as DerObjectIdentifier).Id, EncryptedKey = (s0[0] as DerOctetString).GetOctets(), Mac = (s0[1] as DerOctetString).GetOctets(), Ukm = (s1[2] as DerOctetString).GetOctets() }; TransportParameters = new GostKeyExchangeParameters { PublicKeyParamSet = (s1101[0] as DerObjectIdentifier).Id, DigestParamSet = (s1101[1] as DerObjectIdentifier).Id, EncryptionParamSet = s1101.Count > 2 ? (s1101[2] as DerObjectIdentifier).Id : null, PublicKey = (DerOctetString.FromByteArray((s11[1] as DerBitString).GetBytes()) as DerOctetString).GetOctets(), PrivateKey = null }; } catch (Exception exception) { throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, "GostR3410KeyTransportDecode"); } }
/// <summary> /// Шифрует общий секретный ключ. /// </summary> /// <param name="keyExchangeAlgorithm">Алгоритм шифрации общего секретного ключа.</param> /// <exception cref="ArgumentNullException"></exception> public GostKeyExchange CreateKeyExchangeInfo(SymmetricAlgorithm keyExchangeAlgorithm) { if (keyExchangeAlgorithm == null) { throw ExceptionUtility.ArgumentNull("keyExchangeAlgorithm"); } var keyExchange = new GostKeyExchange(); var keyExchangeParameters = _publicKey.ExportParameters(false); using (var keyExchangeAsym = new Gost3410EphemeralAsymmetricAlgorithm(keyExchangeParameters)) { byte[] encodedKeyExchangeInfo; using (var keyExchangeAlg = keyExchangeAsym.CreateKeyExchange(keyExchangeParameters)) { encodedKeyExchangeInfo = keyExchangeAlg.EncodeKeyExchange(keyExchangeAlgorithm, GostKeyExchangeExportMethod.CryptoProKeyExport); } var keyExchangeInfo = new GostKeyExchangeInfo(); keyExchangeInfo.Decode(encodedKeyExchangeInfo); keyExchange.SessionEncryptedKey = keyExchangeInfo; keyExchange.TransportParameters = keyExchangeAsym.ExportParameters(false); } return keyExchange; }
public override SymmetricAlgorithm DecodePrivateKey(byte[] encodedKeyExchangeData, GostKeyExchangeExportMethod keyExchangeExportMethod) { if (encodedKeyExchangeData == null) { throw ExceptionUtility.ArgumentNull("encodedKeyExchangeData"); } int keyExchangeExportAlgId; if (keyExchangeExportMethod == GostKeyExchangeExportMethod.GostKeyExport) { keyExchangeExportAlgId = Constants.CALG_SIMPLE_EXPORT; } else if (keyExchangeExportMethod == GostKeyExchangeExportMethod.CryptoProKeyExport) { keyExchangeExportAlgId = Constants.CALG_PRO_EXPORT; } else { throw ExceptionUtility.ArgumentOutOfRange("keyExchangeExportMethod"); } var providerHandle = CryptoApiHelper.ProviderHandle; var keyExchangeInfo = new GostKeyExchangeInfo(); keyExchangeInfo.Decode(encodedKeyExchangeData); using (var keyHandle = CryptoApiHelper.DuplicateKey(InternalKeyHandle)) { CryptoApiHelper.SetKeyParameterInt32(keyHandle, Constants.KP_ALGID, keyExchangeExportAlgId); var keyExchangeHandle = CryptoApiHelper.ImportKeyExchange(providerHandle, keyExchangeInfo, keyHandle); return new Gost28147SymmetricAlgorithm(providerHandle, keyExchangeHandle); } }