public static string HashToPlainText(string hashTextPswd) { string decryptedPassword = ""; byte[] encryptedBytes = TextString2BinString(hashTextPswd); try { uint dwErrCode = 0; IntPtr hContext = IntPtr.Zero; IntPtr hKey = IntPtr.Zero; if ((dwErrCode = CryptoApiHelper.AcquireContext("OKmzdy", ref hContext)) == ERROR_SUCCESS) { if ((dwErrCode = CryptoApiHelper.GenerateKey(ENCRYPT_ALGORITHM, MASTER_PHRASE, ref hKey, hContext)) == ERROR_SUCCESS) { uint data_size = Convert.ToUInt32(Math.Min(hashTextPswd.Length / 2, MAX_PSBUFFERLEN)); if (CryptoApiHelper.CryptDecrypt(hKey, IntPtr.Zero, true, 0, encryptedBytes, ref data_size)) { decryptedPassword = encFrom.GetString(encryptedBytes).Substring(0, (int)data_size); } } } } catch (Exception ex) { System.Diagnostics.Debug.Print(string.Format("Exception loading file: {0}", ex.ToString())); } return(decryptedPassword); }
private void InitHash() { var hProv = CryptoApiHelper.ProviderHandle; var hHash = CryptoApiHelper.CreateHashImit(hProv, _keyAlgorithm.InternalKeyHandle); _hashHandle = hHash; }
public static Gost_28147_89_SymmetricAlgorithm CreateFromPassword(HashAlgorithm hashAlgorithm, byte[] password) { if (hashAlgorithm == null) { throw ExceptionUtility.ArgumentNull(nameof(hashAlgorithm)); } if (!(hashAlgorithm is IGostAlgorithm gostHashAlgorithm)) { throw ExceptionUtility.ArgumentOutOfRange(nameof(hashAlgorithm)); } if (!(hashAlgorithm is ISafeHandleProvider <SafeHashHandleImpl> hashHandleProvider)) { throw ExceptionUtility.ArgumentOutOfRange(nameof(hashAlgorithm)); } if (password == null) { throw ExceptionUtility.ArgumentNull(nameof(password)); } hashAlgorithm.TransformBlock(password, 0, password.Length, password, 0); var providerType = gostHashAlgorithm.ProviderType; var providerHandle = CryptoApiHelper.GetProviderHandle(providerType); var symKeyHandle = CryptoApiHelper.DeriveSymKey(providerHandle, hashHandleProvider.SafeHandle); return(new Gost_28147_89_SymmetricAlgorithm(providerType, providerHandle, symKeyHandle)); }
public static string PlainTextToHash(string plainTextPswd) { string encryptedPassword = ""; byte[] decryptedBytes = TextString2ByteArray(plainTextPswd); try { uint dwErrCode = 0; IntPtr hContext = IntPtr.Zero; IntPtr hKey = IntPtr.Zero; if ((dwErrCode = CryptoApiHelper.AcquireContext("OKmzdy", ref hContext)) == ERROR_SUCCESS) { if ((dwErrCode = CryptoApiHelper.GenerateKey(ENCRYPT_ALGORITHM, MASTER_PHRASE, ref hKey, hContext)) == ERROR_SUCCESS) { uint data_size = Convert.ToUInt32(plainTextPswd.Length); if (CryptoApiHelper.CryptEncrypt(hKey, IntPtr.Zero, true, 0, decryptedBytes, ref data_size, MAX_PSBUFFERLEN)) { encryptedPassword = BinString2TextString(decryptedBytes, (int)data_size).ToUpper(); } } } } catch (Exception ex) { System.Diagnostics.Debug.Print(string.Format("Exception loading file: {0}", ex.ToString())); } return(encryptedPassword); }
public override byte[] ComputeHash(HashAlgorithm hash) { SafeHashHandleImpl hashHandle; if (hash is Gost3411HashAlgorithm) { hashHandle = ((Gost3411HashAlgorithm)hash).InternalHashHandle; } else if (hash is Gost3411Hmac) { hashHandle = ((Gost3411Hmac)hash).InternalHashHandle; } else if (hash is Gost28147ImitHashAlgorithm) { hashHandle = ((Gost28147ImitHashAlgorithm)hash).InternalHashHandle; } else if (hash is Gost3411_2012_256HashAlgorithm) { hashHandle = ((Gost3411_2012_256HashAlgorithm)hash).InternalHashHandle; } else if (hash is Gost3411_2012_512HashAlgorithm) { hashHandle = ((Gost3411_2012_512HashAlgorithm)hash).InternalHashHandle; } else { throw ExceptionUtility.Argument("hash", Resources.RequiredGostHash); } CryptoApiHelper.HashKeyExchange(hashHandle, InternalKeyHandle); return(CryptoApiHelper.EndHashData(hashHandle)); }
private void InitDefaults(Gost_28147_89_SymmetricAlgorithm keyAlgorithm) { HashName = typeof(THash).Name; _keyAlgorithm = keyAlgorithm; _hmacHandle = CreateHashHMAC(keyAlgorithm.ProviderType, CryptoApiHelper.GetProviderHandle(keyAlgorithm.ProviderType), keyAlgorithm.GetSafeHandle()); }
public override void GenerateKey() { _keyHandle = CryptoApiHelper.GenerateKey(_providerHandle, Constants.CALG_G28147, CspProviderFlags.NoFlags); KeyValue = null; KeySizeValue = DefaultKeySize; }
private byte[] SignHash(byte[] hash) { if (hash == null) { throw ExceptionUtility.ArgumentNull("hash"); } if (hash.Length != 32) { throw ExceptionUtility.ArgumentOutOfRange("hash", Resources.InvalidHashSize); } if (IsPublicKeyOnly) { throw ExceptionUtility.CryptographicException(Resources.NoPrivateKey); } GetKeyPair(); if (!CspKeyContainerInfo.RandomlyGenerated) { var keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); var keyContainerAccessEntry = new KeyContainerPermissionAccessEntry(_providerParameters, KeyContainerPermissionFlags.Sign); keyContainerPermission.AccessEntries.Add(keyContainerAccessEntry); keyContainerPermission.Demand(); } return(CryptoApiHelper.SignValue(_providerHandle, _providerParameters.KeyNumber, hash)); }
public override void Initialize() { var hmacHandle = CreateHashHMAC(ProviderType, CryptoApiHelper.GetProviderHandle(ProviderType), _keyAlgorithm.GetSafeHandle()); _hmacHandle.TryDispose(); _hmacHandle = hmacHandle; }
private byte[] SignHash(byte[] hash) { ValidateHashParameter(hash); if (IsPublicKeyOnly) { throw ExceptionUtility.CryptographicException(Resources.NoPrivateKey); } GetKeyPair(); if (!CspKeyContainerInfo.RandomlyGenerated) { var keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); var keyContainerAccessEntry = new KeyContainerPermissionAccessEntry(_providerParameters, KeyContainerPermissionFlags.Sign); keyContainerPermission.AccessEntries.Add(keyContainerAccessEntry); keyContainerPermission.Demand(); } using (var hashAlgorithm = CreateHashAlgorithm()) { var hashHandleProvider = (ISafeHandleProvider <SafeHashHandleImpl>)hashAlgorithm; return(CryptoApiHelper.SignValue(_providerHandle, hashHandleProvider.SafeHandle, _providerParameters.KeyNumber, hash)); } }
private bool VerifyHash(byte[] hash, byte[] signature) { if (hash == null) { throw ExceptionUtility.ArgumentNull("hash"); } if (signature == null) { throw ExceptionUtility.ArgumentNull("signature"); } if (hash.Length != 64) { throw ExceptionUtility.ArgumentOutOfRange("InvalidHashSize"); } bool res = false; UsingKey(h => { res = CryptoApiHelper.VerifySign(_providerHandle, h, hash, signature, GostAlgorithmType.Gost2012_512); }); return(res); }
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { if (inputBuffer == null) { throw ExceptionUtility.ArgumentNull(nameof(inputBuffer)); } if (inputOffset < 0) { throw ExceptionUtility.ArgumentOutOfRange(nameof(inputOffset)); } if ((inputCount < 0) || (inputCount > inputBuffer.Length)) { throw ExceptionUtility.ArgumentOutOfRange(nameof(inputOffset), Resources.InvalidDataOffset); } if ((inputBuffer.Length - inputCount) < inputOffset) { throw ExceptionUtility.ArgumentOutOfRange(nameof(inputOffset), Resources.InvalidDataOffset); } byte[] buffer = null; if (_transformMode == Gost_28147_89_CryptoTransformMode.Encrypt) { CryptoApiHelper.EncryptData(_providerType, _keyHandle, inputBuffer, inputOffset, inputCount, ref buffer, 0, _paddingValue, true, _isStreamModeValue); Reset(); return(buffer); } if (_isStreamModeValue) { CryptoApiHelper.DecryptData(_keyHandle, inputBuffer, inputOffset, inputCount, ref buffer, 0, _paddingValue, true); Reset(); return(buffer); } if ((inputCount % InputBlockSize) != 0) { throw ExceptionUtility.CryptographicException(Resources.DecryptInvalidDataSize); } if (_dataBuffer == null) { CryptoApiHelper.DecryptData(_keyHandle, inputBuffer, inputOffset, inputCount, ref buffer, 0, _paddingValue, true); Reset(); return(buffer); } var destinationArray = new byte[_dataBuffer.Length + inputCount]; Array.Copy(_dataBuffer, 0, destinationArray, 0, _dataBuffer.Length); Array.Copy(inputBuffer, inputOffset, destinationArray, _dataBuffer.Length, inputCount); CryptoApiHelper.DecryptData(_keyHandle, destinationArray, 0, destinationArray.Length, ref buffer, 0, _paddingValue, true); Reset(); return(buffer); }
private void InitializeHmac() { var hashHmacHandle = CryptoApiHelper.CreateHashHmac(CryptoApiHelper.ProviderHandle, _key.InternalKeyHandle); _hashHmacHandle.TryDispose(); _hashHmacHandle = hashHmacHandle; }
public Gost3411Hmac() { HashName = DefaultHashName; HashSizeValue = DefaultHashSize; _keyAlgorithm = new Gost28147SymmetricAlgorithm(); _hashHandle = CryptoApiHelper.CreateHashHmac(CryptoApiHelper.ProviderHandle, _keyAlgorithm.InternalKeyHandle); }
public override void Initialize() { var hashHmacHandle = CryptoApiHelper.CreateHashHmac(CryptoApiHelper.ProviderHandle, _keyAlgorithm.InternalKeyHandle); _hashHandle.TryDispose(); _hashHandle = hashHmacHandle; }
private void InitializeHmac() { var hashHmacHandle = CryptoApiHelper.CreateHashHmac(CryptoApiHelper.ProviderHandle, _key.InternalKeyHandle, Constants.CALG_GR3411_2012_512_HMAC); _hashHmacHandle.TryDispose(); _hashHmacHandle = hashHmacHandle; }
private void InitializeHmac() { var hashHmacHandle = CryptoApiHelper.CreateHashHmac(CryptoApiHelper.ProviderHandle, _key.InternalKeyHandle, GostCryptoConfig.ProviderType == ProviderTypes.VipNet ? Constants.CALG_GR3411_HMAC34 : Constants.CALG_GR3411_HMAC); _hashHmacHandle.TryDispose(); _hashHmacHandle = hashHmacHandle; }
public Gost3411Hmac() { HashName = DefaultHashName; HashSizeValue = DefaultHashSize; _keyAlgorithm = new Gost28147SymmetricAlgorithm(); _hashHandle = CryptoApiHelper.CreateHashHmac(CryptoApiHelper.ProviderHandle, _keyAlgorithm.InternalKeyHandle, GostCryptoConfig.ProviderType == ProviderTypes.VipNet ? Constants.CALG_GR3411_HMAC34 : Constants.CALG_GR3411_HMAC); }
public Gost28147 DeriveKey() { GenerateNextBytes(); var symKeyHandle = CryptoApiHelper.DeriveSymKey(CryptoApiHelper.ProviderHandle, _hashHmacHandle); return(new Gost28147SymmetricAlgorithm(CryptoApiHelper.ProviderHandle, symKeyHandle)); }
public Gost3411_2012_256Hmac() { HashName = DefaultHashName; HashSizeValue = DefaultHashSize; _keyAlgorithm = new Gost28147SymmetricAlgorithm(); _hashHandle = CryptoApiHelper.CreateHashHmac(CryptoApiHelper.ProviderHandle, _keyAlgorithm.InternalKeyHandle, Constants.CALG_GR3411_2012_256_HMAC); }
public override void Initialize() { var hashHmacHandle = CryptoApiHelper.CreateHashHmac(CryptoApiHelper.ProviderHandle, _keyAlgorithm.InternalKeyHandle, Constants.CALG_GR3411_2012_256_HMAC); _hashHandle.TryDispose(); _hashHandle = hashHmacHandle; }
public override GostKeyExchangeParameters ExportParameters(bool includePrivateKey) { if (includePrivateKey) { throw ExceptionUtility.NotSupported(Resources.EphemKeyOperationNotSupported); } return(CryptoApiHelper.ExportPublicKey(_keyHandle, GostAlgorithmType.Gost2012_256)); }
public override Asn1.Common.GostKeyExchangeParameters ExportParameters(bool includePrivateKey) { if (includePrivateKey) { throw ExceptionUtility.NotSupported(Resources.EphemKeyOperationNotSupported); } return(CryptoApiHelper.ExportPublicKey(_keyHandle)); }
protected override void HashCore(byte[] data, int dataOffset, int dataLength) { if (_hashHandle == null) { InitHash(); } CryptoApiHelper.HashData(_hashHandle, data, dataOffset, dataLength); }
protected override byte[] HashFinal() { if (_hashHandle == null) { InitHash(); } return(CryptoApiHelper.EndHashData(_hashHandle)); }
protected Gost_R3410_EphemeralAsymmetricAlgorithm(ProviderType providerType, TKeyParams keyParameters, int keySize) : base(providerType, keySize) { if (keyParameters == null) { throw ExceptionUtility.ArgumentNull(nameof(keyParameters)); } _providerHandle = CryptoApiHelper.GetProviderHandle(ProviderType).DangerousAddRef(); _keyHandle = CryptoApiHelper.GenerateDhEphemeralKey(providerType, _providerHandle, ExchangeAlgId, keyParameters.DigestParamSet, keyParameters.PublicKeyParamSet); }
internal Gost28147SymmetricAlgorithm(SafeProvHandleImpl provHandle, SafeKeyHandleImpl keyHandle) : this() { _provHandle = provHandle.DangerousAddRef(); _keyHandle = CryptoApiHelper.DuplicateKey(keyHandle); if (CryptoApiHelper.GetKeyParameterInt32(_keyHandle, Constants.KP_ALGID) != Constants.CALG_G28147) { throw ExceptionUtility.Argument("keyHandle", Resources.RequiredGost28147); } }
public Gost28147SymmetricAlgorithm(IntPtr provHandle, IntPtr keyHandle) : this() { _provHandle = new SafeProvHandleImpl(provHandle, true); _keyHandle = CryptoApiHelper.DuplicateKey(keyHandle); if (CryptoApiHelper.GetKeyParameterInt32(_keyHandle, Constants.KP_ALGID) != Constants.CALG_G28147) { throw ExceptionUtility.Argument("keyHandle", Resources.RequiredGost28147); } }
public Gost3410_2012_256EphemeralAsymmetricAlgorithm(GostKeyExchangeParameters keyParameters) { if (keyParameters == null) { throw ExceptionUtility.ArgumentNull("keyParameters"); } _provHandle = CryptoApiHelper.ProviderHandle.DangerousAddRef(); _keyHandle = CryptoApiHelper.GenerateDhEphemeralKey(_provHandle, Constants.CALG_DH_GR3410_12_256_EPHEM, keyParameters.DigestParamSet, keyParameters.PublicKeyParamSet); }
public Gost3410EphemeralAsymmetricAlgorithm(Asn1.Common.GostKeyExchangeParameters keyParameters) { if (keyParameters == null) { throw ExceptionUtility.ArgumentNull("keyParameters"); } _provHandle = CryptoApiHelper.ProviderHandle.DangerousAddRef(); _keyHandle = CryptoApiHelper.GenerateDhEphemeralKey(_provHandle, keyParameters.DigestParamSet, keyParameters.PublicKeyParamSet); }