private static void EncriptFile(X509Certificate2 sert, string sourceFile, string encFile) { var publicKey = (Gost3410_2012_256CryptoServiceProvider)sert.PublicKey.Key; var asymmetricAlg = publicKey as Gost3410_2012_256; if (asymmetricAlg == null) { throw new CryptographicException("Not a gost certificate"); } var symmetricKey = Gost28147.Create(); Gost3410_2012_256 senderRndKey = Gost3410_2012_256.Create(); Gost3410Parameters senderRndKeyParameters = senderRndKey.ExportParameters(false); GostSharedSecretAlgorithm agreeKey = senderRndKey.CreateAgree(asymmetricAlg.ExportParameters(false)); var encodedSymmetricKey = agreeKey.Wrap(symmetricKey, GostKeyWrapMethod.CryptoProKeyWrap); ICryptoTransform transform = symmetricKey.CreateEncryptor(); using (FileStream writer = new FileStream(encFile, FileMode.Create)) { BinaryWriter binaryWriter = new BinaryWriter(writer); binaryWriter.Write(encodedSymmetricKey.Length); binaryWriter.Write(encodedSymmetricKey); binaryWriter.Write(symmetricKey.IV.Length); binaryWriter.Write(symmetricKey.IV); BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(writer, senderRndKeyParameters); using (CryptoStream cryptoStream = new CryptoStream(writer, transform, CryptoStreamMode.Write)) { var buffer = new byte[100]; using (FileStream reader = new FileStream(sourceFile, FileMode.Open, FileAccess.Read)) { var length = reader.Read(buffer, 0, buffer.Length); while (length > 0) { cryptoStream.Write(buffer, 0, buffer.Length); length = reader.Read(buffer, 0, buffer.Length); } } } } }
/// <summary> /// Инициализирует новый экземпляр класса <c>GostKeyValue</c> с новым, /// сгенерированным случайным образом открытым ключом ГОСТ 34.10-2012 256. /// </summary> /// <remarks>При создании нового ключа ГОСТ 34.10-2012 256 этот конструктор /// использует реализацию <see cref="Gost3410_2012_256"/> по /// умолчанию, как определено классом /// <see cref="System.Security.Cryptography.CryptoConfig"/>.</remarks> public Gost2012_256KeyValue() { _key = (Gost3410_2012_256)Gost3410_2012_256.Create(); }