Esempio n. 1
0
 /// <summary>
 ///     Создать <see cref="KeyContainer" />.
 /// </summary>
 /// <param name="keyContainerName">Название ключевого контейнера.</param>
 /// <param name="keyNumber">Тип ключа.</param>
 /// <returns>
 ///     Экземпляр <see cref="KeyContainer" />.
 /// </returns>
 public static KeyContainer Create(string keyContainerName, KeyNumber keyNumber)
 {
     var container = new KeyContainer();
     container.AcquireContext(keyContainerName, ProviderName, ProviderType, Constants.NewKeySet);
     container.GenerateRandomKey(keyNumber);
     return container;
 }
Esempio n. 2
0
        /// <summary>
        ///     Создать <see cref="KeyContainer" />.
        /// </summary>
        /// <param name="keyContainerName">Название ключевого контейнера.</param>
        /// <param name="keyNumber">Тип ключа.</param>
        /// <returns>
        ///     Экземпляр <see cref="KeyContainer" />.
        /// </returns>
        public static KeyContainer Create(string keyContainerName, KeyNumber keyNumber)
        {
            var container = new KeyContainer();

            container.AcquireContext(keyContainerName, ProviderName, ProviderType, Constants.NewKeySet);
            container.GenerateRandomKey(keyNumber);
            return(container);
        }
Esempio n. 3
0
        /// <summary>
        ///     Открыть существующий контейнер.
        /// </summary>
        /// <param name="keyContainerName">Название контейнера.</param>
        /// <param name="keycontainerPassword">Пароль ключевого контейнера.</param>
        /// <returns>
        ///     Экземпляр <see cref="KeyContainer" />.
        /// </returns>
        public static KeyContainer Open(string keyContainerName, string keycontainerPassword)
        {
            var container = new KeyContainer();

            container.AcquireContext(keyContainerName, ProviderName, ProviderType, 0);
            container.SetPassword(keycontainerPassword);
            return(container);
        }
Esempio n. 4
0
 /// <summary>
 ///     Получить сертификат для конкретного ключа
 /// </summary>
 /// <returns></returns>
 public static byte[] ExportCertificateData(string keyContainerName)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(keyContainerName, ProviderName, ProviderType, 0);
         return(container.ExportCertificateData());
     }
 }
Esempio n. 5
0
 /// <summary>
 /// Возвращает открытый ключ сертификата
 /// </summary>
 /// <param name="certificateData">данные сертификата</param>
 /// <returns></returns>
 public static byte[] GetCertificatePublicKey(byte[] certificateData)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(null, ProviderName, ProviderType, Constants.CryptVerifycontext);
         using (KeyContext keyContext = container.ImportSertificate(certificateData))
         {
             return(keyContext.ExportPublicKey());
         }
     }
 }
Esempio n. 6
0
 /// <summary>
 ///     Удаление ключевого контейнера.
 /// </summary>
 /// <param name="keyContainerName">Название контейнера.</param>
 public static void Remove(string keyContainerName)
 {
     try
     {
         var container = new KeyContainer();
         container.AcquireContext(keyContainerName, ProviderName, ProviderType, Constants.DeleteKeySet);
     }
     catch (Win32Exception)
     {
     }
 }
Esempio n. 7
0
 /// <summary>
 ///     Подсчет хэша.
 /// </summary>
 /// <param name="data">Данные.</param>
 /// <returns>Хэш.</returns>
 public static byte[] ComputeHash(byte[] data)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(null, ProviderName, ProviderType, Constants.CryptVerifycontext);
         using (HashContext hashContext = container.CreateHash(null, Constants.CpcspHashId, 0))
         {
             hashContext.AddData(data, 0);
             return hashContext.GetValue();
         }
     }
 }
Esempio n. 8
0
 /// <summary>
 ///     Подсчет хэша.
 /// </summary>
 /// <param name="data">Данные.</param>
 /// <returns>Хэш.</returns>
 public static byte[] ComputeHash(byte[] data)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(null, ProviderName, ProviderType, Constants.CryptVerifycontext);
         using (HashContext hashContext = container.CreateHash(null, Constants.CpcspHashId, 0))
         {
             hashContext.AddData(data, 0);
             return(hashContext.GetValue());
         }
     }
 }
Esempio n. 9
0
 /// <summary>
 ///     Провекра наличия контейнера.
 /// </summary>
 /// <param name="keyContainerName">Название контейнера.</param>
 /// <returns>True - контейнер существует, иначе False.</returns>
 public static bool Exist(string keyContainerName)
 {
     try
     {
         using (var container = new KeyContainer())
         {
             container.AcquireContext(keyContainerName, ProviderName, ProviderType, Constants.SilentMode);
             container.GetUserKey();
             return(true);
         }
     }
     catch (Win32Exception)
     {
         return(false);
     }
 }
Esempio n. 10
0
 /// <summary>
 ///     Проверка подписи.
 /// </summary>
 /// <param name="signature">Подпись.</param>
 /// <param name="data">Данные.</param>
 /// <param name="certificateData">Сертификат.</param>
 /// <returns>True - провека прошла успешно, иначе False.</returns>
 public static bool VerifyCertificate(byte[] signature, byte[] data, byte[] certificateData)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(null, ProviderName, ProviderType, Constants.CryptVerifycontext);
         using (KeyContext keyContext = container.ImportSertificate(certificateData))
         {
             using (HashContext hashContext =
                        container.CreateHash(null, Constants.CpcspHashId, 0))
             {
                 hashContext.AddData(data, 0);
                 return(keyContext.VerifySignature(signature, hashContext, 0));
             }
         }
     }
 }
Esempio n. 11
0
 /// <summary>
 ///     Провекра наличия контейнера.
 /// </summary>
 /// <param name="keyContainerName">Название контейнера.</param>
 /// <returns>True - контейнер существует, иначе False.</returns>
 public static bool Exist(string keyContainerName)
 {
     try
     {
         using (var container = new KeyContainer())
         {
             container.AcquireContext(keyContainerName, ProviderName, ProviderType, Constants.SilentMode);
             container.GetUserKey();
             return true;
         }
     }
     catch (Win32Exception)
     {
         return false;
     }
 }
Esempio n. 12
0
 /// <summary>
 ///     Проверка подписи.
 /// </summary>
 /// <param name="signature">Подпись.</param>
 /// <param name="data">Данные.</param>
 /// <param name="publicKey">Открытый ключ.</param>
 /// <returns>True - провека прошла успешно, иначе False.</returns>
 public static bool VerifySignature(byte[] signature, byte[] data, byte[] publicKey)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(null, ProviderName, ProviderType, Constants.CryptVerifycontext);
         using (KeyContext keyContext = container.ImportKey(null, publicKey, publicKey.Length, 0))
         {
             using (HashContext hashContext =
                 container.CreateHash(null, Constants.CpcspHashId, 0))
             {
                 hashContext.AddData(data, 0);
                 return keyContext.VerifySignature(signature, hashContext, 0);
             }
         }
     }
 }
Esempio n. 13
0
 /// <summary>
 ///     Удаление ключевого контейнера.
 /// </summary>
 /// <param name="keyContainerName">Название контейнера.</param>
 public static void Remove(string keyContainerName)
 {
     try
     {
         var container = new KeyContainer();
         container.AcquireContext(keyContainerName, ProviderName, ProviderType, Constants.DeleteKeySet);
     }
     catch (Win32Exception)
     {
     }
 }
Esempio n. 14
0
 /// <summary>
 ///     Открыть существующий контейнер.
 /// </summary>
 /// <param name="keyContainerName">Название контейнера.</param>
 /// <param name="keycontainerPassword">Пароль ключевого контейнера.</param>
 /// <returns>
 ///     Экземпляр <see cref="KeyContainer" />.
 /// </returns>
 public static KeyContainer Open(string keyContainerName, string keycontainerPassword)
 {
     var container = new KeyContainer();
     container.AcquireContext(keyContainerName, ProviderName, ProviderType, 0);
     container.SetPassword(keycontainerPassword);
     return container;
 }
 /// <summary>
 ///     Получить сертификат для конкретного ключа
 /// </summary>
 /// <returns></returns>
 public static byte[] ExportCertificateData(string keyContainerName)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(keyContainerName, ProviderName, ProviderType, 0);
         return container.ExportCertificateData();
     }
 }
Esempio n. 16
0
 /// <summary>
 ///     Экспорт открытого ключа.
 /// </summary>
 /// <param name="keyContainerName">Название контейнера.</param>
 /// <returns>Открытый ключ.</returns>
 public static byte[] ExportPublicKey(string keyContainerName)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(keyContainerName, ProviderName, ProviderType, 0);
         using (KeyContext keyContext = container.GetUserKey())
         {
             return keyContext.ExportPublicKey();
         }
     }
 }
 /// <summary>
 /// Возвращает открытый ключ сертификата
 /// </summary>
 /// <param name="certificateData">данные сертификата</param>
 /// <returns></returns>
 public static byte[] GetCertificatePublicKey(byte[] certificateData)
 {
     using (var container = new KeyContainer())
     {
         container.AcquireContext(null, ProviderName, ProviderType, Constants.CryptVerifycontext);
         using (KeyContext keyContext = container.ImportSertificate(certificateData))
         {
             return keyContext.ExportPublicKey();
         }
     }
 }