protected void Dispose(bool disposing) { if (disposing) { if (this.hSessionKey != null) { if (!this.hSessionKey.IsInvalid) { this.hSessionKey.Dispose(); } this.hSessionKey = null; } if ((_hStaticRSAKey == null) && (this.hRSAKey != null)) { if (!this.hRSAKey.IsInvalid) { this.hRSAKey.Dispose(); } this.hRSAKey = null; } if ((_hStaticProv == null) && (this.hProv != null)) { if (!this.hProv.IsInvalid) { this.hProv.Dispose(); } this.hProv = null; } } }
private PSRSACryptoServiceProvider(bool serverMode) { if (serverMode) { this.hProv = new PSSafeCryptProvHandle(); bool flag = PSCryptoNativeUtils.CryptAcquireContext(ref this.hProv, null, null, 0x18, Int32.MaxValue); // (0xf0000000).ToInt32()); this.CheckStatus(flag); this.hRSAKey = new PSSafeCryptKey(); } this.hSessionKey = new PSSafeCryptKey(); }
internal void GenerateKeyPair() { if (!keyPairGenerated) { lock (syncObject) { if (!keyPairGenerated) { _hStaticProv = new PSSafeCryptProvHandle(); bool flag = PSCryptoNativeUtils.CryptAcquireContext(ref _hStaticProv, null, null, 0x18, (0xf0000000).ToInt32()); this.CheckStatus(flag); _hStaticRSAKey = new PSSafeCryptKey(); flag = PSCryptoNativeUtils.CryptGenKey(_hStaticProv, 1, 0x8000001, ref _hStaticRSAKey); this.CheckStatus(flag); keyPairGenerated = true; } } } this.hProv = _hStaticProv; this.hRSAKey = _hStaticRSAKey; }
public static extern bool CryptGenKey(PSSafeCryptProvHandle hProv, uint Algid, uint dwFlags, ref PSSafeCryptKey phKey);
//[SecurityPermission(SecurityAction.Demand, UnmanagedCode=true)] protected void Dispose(bool disposing) { if (disposing) { if (null != _hSessionKey) { if (!_hSessionKey.IsInvalid) { _hSessionKey.Dispose(); } _hSessionKey = null; } // we need to dismiss the provider and key // only if the static members are not allocated // since otherwise, these are just references // to the static members if (null == s_hStaticRSAKey) { if (null != _hRSAKey) { if (!_hRSAKey.IsInvalid) { _hRSAKey.Dispose(); } _hRSAKey = null; } } if (null == s_hStaticProv) { if (null != _hProv) { if (!_hProv.IsInvalid) { _hProv.Dispose(); } _hProv = null; } } } }
/// <summary> /// Generates key pair in a thread safe manner /// the first time when required /// </summary> internal void GenerateKeyPair() { if (!s_keyPairGenerated) { lock (s_syncObject) { if (!s_keyPairGenerated) { s_hStaticProv = new PSSafeCryptProvHandle(); // We need PROV_RSA_AES to support AES-256 symmetric key // encryption. PROV_RSA_FULL supports only RC2 and RC4 bool ret = PSCryptoNativeUtils.CryptAcquireContext(ref s_hStaticProv, null, null, PSCryptoNativeUtils.PROV_RSA_AES, PSCryptoNativeUtils.CRYPT_VERIFYCONTEXT); CheckStatus(ret); s_hStaticRSAKey = new PSSafeCryptKey(); ret = PSCryptoNativeUtils.CryptGenKey(s_hStaticProv, PSCryptoNativeUtils.AT_KEYEXCHANGE, 0x08000000 | PSCryptoNativeUtils.CRYPT_EXPORTABLE, // key length -> 2048 ref s_hStaticRSAKey); CheckStatus(ret); // key needs to be generated once s_keyPairGenerated = true; } } } _hProv = s_hStaticProv; _hRSAKey = s_hStaticRSAKey; }
/// <summary> /// Private constructor /// </summary> /// <param name="serverMode">indicates if this service /// provider is operating in server mode</param> private PSRSACryptoServiceProvider(bool serverMode) { if (serverMode) { _hProv = new PSSafeCryptProvHandle(); // We need PROV_RSA_AES to support AES-256 symmetric key // encryption. PROV_RSA_FULL supports only RC2 and RC4 bool ret = PSCryptoNativeUtils.CryptAcquireContext(ref _hProv, null, null, PSCryptoNativeUtils.PROV_RSA_AES, PSCryptoNativeUtils.CRYPT_VERIFYCONTEXT); CheckStatus(ret); _hRSAKey = new PSSafeCryptKey(); } _hSessionKey = new PSSafeCryptKey(); }
public static extern bool CryptDuplicateKey(PSSafeCryptKey hKey, ref uint pdwReserved, uint dwFlags, ref PSSafeCryptKey phKey);
public static extern bool CryptDuplicateKey( PSSafeCryptKey hKey, ref uint pdwReserved, uint dwFlags, ref PSSafeCryptKey phKey);
public static extern bool CryptExportKey(PSSafeCryptKey hKey, PSSafeCryptKey hExpKey, uint dwBlobType, uint dwFlags, byte[] pbData, ref uint pdwDataLen);
public static extern bool CryptDecrypt(PSSafeCryptKey hKey, IntPtr hHash, [MarshalAsAttribute(UnmanagedType.Bool)] bool Final, uint dwFlags, byte[] pbData, ref int pdwDataLen);
public static bool CryptImportKey(PSSafeCryptProvHandle hProv, byte[] pbData, int dwDataLen, PSSafeCryptKey hPubKey, int dwFlags, ref PSSafeCryptKey phKey) { return true; }
public static bool CryptGenKey(PSSafeCryptProvHandle hProv, int Algid, int dwFlags, ref PSSafeCryptKey phKey) { return true; }
public static bool CryptExportKey(PSSafeCryptKey hKey, PSSafeCryptKey hExpKey, int dwBlobType, int dwFlags, byte[] pbData, ref int pdwDataLen) { pdwDataLen = pbData.Length; return true; }
public static bool CryptEncrypt(PSSafeCryptKey hKey, IntPtr hHash, [MarshalAs(UnmanagedType.Bool)] bool Final, int dwFlags, byte[] pbData, ref int pdwDataLen, int dwBufLen) { pdwDataLen = pbData.Length; return true; }
public static bool CryptDuplicateKey(PSSafeCryptKey hKey, ref int pdwReserved, int dwFlags, ref PSSafeCryptKey phKey) { return true; }
public static extern bool CryptImportKey(PSSafeCryptProvHandle hProv, byte[] pbData, int dwDataLen, PSSafeCryptKey hPubKey, uint dwFlags, ref PSSafeCryptKey phKey);
public static extern bool CryptGenKey( PSSafeCryptProvHandle hProv, uint Algid, uint dwFlags, ref PSSafeCryptKey phKey);