public static void AssertKeyIsValid(Gost_28147_89_SymmetricAlgorithmBase key) { var encryptedData = EncryptData(key, TestData); var decryptedData = DecryptData(key, encryptedData); CollectionAssert.AreEqual(TestData, decryptedData); }
public static void AssertKeysAreNotEqual(Gost_28147_89_SymmetricAlgorithmBase key1, Gost_28147_89_SymmetricAlgorithmBase key2) { var encryptedData1 = EncryptData(key1, TestData); var encryptedData2 = EncryptData(key2, TestData); CollectionAssert.AreNotEqual(encryptedData1, encryptedData2); }
protected Gost_R3411_HMAC(Gost_28147_89_SymmetricAlgorithmBase keyAlgorithm, int hashSize) : base(keyAlgorithm.ProviderType, hashSize) { if (keyAlgorithm == null) { throw ExceptionUtility.ArgumentNull(nameof(keyAlgorithm)); } InitDefaults(Gost_28147_89_SymmetricAlgorithm.CreateFromKey(keyAlgorithm)); }
private static XmlDocument EncryptXmlDocument(XmlDocument xmlDocument, Gost_28147_89_SymmetricAlgorithmBase sharedKey) { // Создание объекта для шифрации XML var encryptedXml = new GostEncryptedXml(sharedKey.ProviderType); // Поиск элементов для шифрации var elements = xmlDocument.SelectNodes("//SomeElement[@Encrypt='true']"); if (elements != null) { var elementIndex = 0; foreach (XmlElement element in elements) { // Создание случайного сессионного ключа using (var sessionKey = new Gost_28147_89_SymmetricAlgorithm(sharedKey.ProviderType)) { // Шифрация элемента var encryptedData = encryptedXml.EncryptData(element, sessionKey, false); // Шифрация сессионного ключа с использованием общего симметричного ключа var encryptedSessionKeyData = GostEncryptedXml.EncryptKey(sessionKey, sharedKey, GostKeyExchangeExportMethod.CryptoProKeyExport); // Формирование элемента EncryptedData var elementEncryptedData = new EncryptedData(); elementEncryptedData.Id = "EncryptedElement" + elementIndex++; elementEncryptedData.Type = EncryptedXml.XmlEncElementUrl; elementEncryptedData.EncryptionMethod = new EncryptionMethod(sessionKey.AlgorithmName); elementEncryptedData.CipherData.CipherValue = encryptedData; elementEncryptedData.KeyInfo = new KeyInfo(); // Формирование информации о зашифрованном сессионном ключе var encryptedSessionKey = new EncryptedKey(); encryptedSessionKey.CipherData = new CipherData(encryptedSessionKeyData); encryptedSessionKey.EncryptionMethod = new EncryptionMethod(GostEncryptedXml.XmlEncGostCryptoProKeyExportUrl); encryptedSessionKey.AddReference(new DataReference { Uri = "#" + elementEncryptedData.Id }); encryptedSessionKey.KeyInfo.AddClause(new KeyInfoName { Value = "SharedKey1" }); // Добавление ссылки на зашифрованный ключ, используемый при шифровании данных elementEncryptedData.KeyInfo.AddClause(new KeyInfoEncryptedKey(encryptedSessionKey)); // Замена элемента его зашифрованным представлением GostEncryptedXml.ReplaceElement(element, elementEncryptedData, false); } } } return(xmlDocument); }
public static byte[] EncryptKey(Gost_28147_89_SymmetricAlgorithmBase sessionKey, Gost_28147_89_SymmetricAlgorithmBase sharedKey, GostKeyExchangeExportMethod exportMethod) { if (sessionKey == null) { throw ExceptionUtility.ArgumentNull(nameof(sessionKey)); } if (sharedKey == null) { throw ExceptionUtility.ArgumentNull(nameof(sharedKey)); } return(sharedKey.EncodePrivateKey(sessionKey, exportMethod)); }
private static bool VerifyHmacDataStream(Gost_28147_89_SymmetricAlgorithmBase sharedKey, Stream hmacDataStream) { // Создание объекта для вычисления HMAC using (var hmac = new Gost_R3411_94_HMAC(sharedKey)) { // Считывание HMAC из потока данных var hmacValue = new byte[hmac.HashSize / 8]; hmacDataStream.Read(hmacValue, 0, hmacValue.Length); // Вычисление реального значения HMAC для потока данных var expectedHmacValue = hmac.ComputeHash(hmacDataStream); // Сравнение исходного HMAC с ожидаемым return(hmacValue.SequenceEqual(expectedHmacValue)); } }
private static bool VerifyImitDataStream(Gost_28147_89_SymmetricAlgorithmBase sharedKey, Stream imitDataStream) { // Создание объекта для вычисления имитовставки using (var imitHash = new Gost_28147_89_ImitHashAlgorithm(sharedKey)) { // Считывание имитовставки из потока данных var imitHashValue = new byte[imitHash.HashSize / 8]; imitDataStream.Read(imitHashValue, 0, imitHashValue.Length); // Вычисление реального значения имитовставки для потока данных var expectedImitHashValue = imitHash.ComputeHash(imitDataStream); // Сравнение исходной имитовставки с ожидаемой return(imitHashValue.SequenceEqual(expectedImitHashValue)); } }
public static byte[] EncryptKey(Gost_28147_89_SymmetricAlgorithmBase sessionKey, GostAsymmetricAlgorithm publicKey) { if (sessionKey == null) { throw ExceptionUtility.ArgumentNull(nameof(sessionKey)); } if (publicKey == null) { throw ExceptionUtility.ArgumentNull(nameof(publicKey)); } var formatter = publicKey.CreateKeyExchangeFormatter(); return(formatter.CreateKeyExchangeData(sessionKey)); }
private static Stream CreateHmacDataStream(Gost_28147_89_SymmetricAlgorithmBase sharedKey, Stream dataStream) { // Создание объекта для вычисления HMAC using (var hmac = new Gost_R3411_94_HMAC(sharedKey)) { // Вычисление HMAC для потока данных var hmacValue = hmac.ComputeHash(dataStream); // Запись HMAC в начало выходного потока данных var hmacDataStream = new MemoryStream(); hmacDataStream.Write(hmacValue, 0, hmacValue.Length); // Копирование исходного потока данных в выходной поток dataStream.Position = 0; dataStream.CopyTo(hmacDataStream); hmacDataStream.Position = 0; return(hmacDataStream); } }
private static Stream CreateImitDataStream(Gost_28147_89_SymmetricAlgorithmBase sharedKey, Stream dataStream) { // Создание объекта для вычисления имитовставки using (var imitHash = new Gost_28147_89_ImitHashAlgorithm(sharedKey)) { // Вычисление имитовставки для потока данных var imitHashValue = imitHash.ComputeHash(dataStream); // Запись имитовставки в начало выходного потока данных var imitDataStream = new MemoryStream(); imitDataStream.Write(imitHashValue, 0, imitHashValue.Length); // Копирование исходного потока данных в выходной поток dataStream.Position = 0; dataStream.CopyTo(imitDataStream); imitDataStream.Position = 0; return(imitDataStream); } }
public static byte[] DecryptData(Gost_28147_89_SymmetricAlgorithmBase key, byte[] data) { var transform = key.CreateDecryptor(); return(transform.TransformFinalBlock(data, 0, data.Length)); }
private static XmlDocument DecryptXmlDocument(XmlDocument encryptedXmlDocument, Gost_28147_89_SymmetricAlgorithmBase sharedKey) { // Создание объекта для дешифрации XML var encryptedXml = new GostEncryptedXml(sharedKey.ProviderType, encryptedXmlDocument); // Добавление ссылки на общий симметричный ключ encryptedXml.AddKeyNameMapping("SharedKey1", sharedKey); // Расшифровка зашифрованных элементов документа encryptedXml.DecryptDocument(); return(encryptedXmlDocument); }
public Gost_R3411_2012_256_PRF(Gost_28147_89_SymmetricAlgorithmBase key, byte[] label, byte[] seed) : base(key, label, seed) { }
protected Gost_R3411_PRF(Gost_28147_89_SymmetricAlgorithmBase key, byte[] label, byte[] seed) : this(key.ProviderType, Gost_28147_89_SymmetricAlgorithm.CreateFromKey(key), label, seed) { }
public Gost_R3411_2012_256_HMAC(Gost_28147_89_SymmetricAlgorithmBase keyAlgorithm) : base(keyAlgorithm, Gost_R3411_2012_256_HashAlgorithm.DefaultHashSizeValue) { }