本文整理汇总了C#中System.Security.Cryptography.SafeProvHandle类的典型用法代码示例。如果您正苦于以下问题:C# SafeProvHandle类的具体用法?C# SafeProvHandle怎么用?C# SafeProvHandle使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SafeProvHandle类属于System.Security.Cryptography命名空间,在下文中一共展示了SafeProvHandle类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Dispose
protected override void Dispose(bool disposing)
{
if (disposing)
{
SafeKeyHandle hKey = _hKey;
_hKey = null;
if (hKey != null)
{
hKey.Dispose();
}
SafeProvHandle hProvider = _hProvider;
_hProvider = null;
if (hProvider != null)
{
hProvider.Dispose();
}
}
base.Dispose(disposing);
}
开发者ID:dotnet,项目名称:corefx,代码行数:21,代码来源:BasicSymmetricCipherCsp.cs
示例2: OpenCSP
/// <summary>
/// OpenCSP performs the core work of opening and creating CSPs and containers in CSPs
/// </summary>
public static int OpenCSP(CspParameters cspParameters, uint flags, out SafeProvHandle safeProvHandle)
{
string providerName = null;
string containerName = null;
if (null == cspParameters)
{
throw new ArgumentException(SR.Format(SR.CspParameter_invalid, "cspParameters"));
}
//look for provider type in the cspParameters
int providerType = cspParameters.ProviderType;
//look for provider name in the cspParamters
//if CSP provider is not null then use the provider name from cspParameters
if (null != cspParameters.ProviderName)
{
providerName = cspParameters.ProviderName;
}
else //Get the default provider name
{
providerName = GetDefaultProvider(providerType);
cspParameters.ProviderName = providerName;
}
// look to see if the user specified that we should pass
// CRYPT_MACHINE_KEYSET to CAPI to use machine key storage instead
// of user key storage
int cspProviderFlags = (int)cspParameters.Flags;
// If the user specified CSP_PROVIDER_FLAGS_USE_DEFAULT_KEY_CONTAINER,
// then ignore the key container name and hand back the default container
if (!IsFlagBitSet((uint)cspProviderFlags, (uint)CspProviderFlags.UseDefaultKeyContainer))
{
//look for key container name in the cspParameters
if (null != cspParameters.KeyContainerName)
{
containerName = cspParameters.KeyContainerName;
}
}
SafeProvHandle hProv;
// Go ahead and try to open the CSP. If we fail, make sure the CSP
// returned is 0 as that is going to be the error check in the caller.
flags |= MapCspProviderFlags((int)cspParameters.Flags);
if (S_OK != AcquireCryptContext(out hProv, containerName, providerName, providerType, flags))
{
hProv.Dispose();
safeProvHandle = SafeProvHandle.InvalidHandle;
return GetErrorCode();
}
hProv.ContainerName = containerName;
hProv.ProviderName = providerName;
hProv.Types = providerType;
hProv.Flags = flags;
// We never want to delete a key container if it's already there.
if (IsFlagBitSet(flags, (uint)CryptAcquireContextFlags.CRYPT_VERIFYCONTEXT))
{
hProv.PersistKeyInCsp = false;
}
safeProvHandle = hProv;
return S_OK;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:68,代码来源:CapiHelper.cs
示例3: AcquireCsp
/// <summary>
/// This method opens the CSP using CRYPT_VERIFYCONTEXT
/// KeyContainer must be null for the flag CRYPT_VERIFYCONTEXT
/// This method asserts if keyContainer is not null
/// </summary>
/// <param name="cspParameters">CSPParameter to use</param>
/// <param name="safeProvHandle">Safe provider handle</param>
internal static void AcquireCsp(CspParameters cspParameters, out SafeProvHandle safeProvHandle)
{
Debug.Assert(cspParameters != null);
Debug.Assert(cspParameters.KeyContainerName == null);
SafeProvHandle hProv;
//
// We want to just open this CSP. Passing in verify context will
// open it and, if a container is given, map to open the container.
//
int ret = OpenCSP(cspParameters, (uint)CryptAcquireContextFlags.CRYPT_VERIFYCONTEXT, out hProv);
if (S_OK != ret)
{
hProv.Dispose();
throw new CryptographicException(SR.Format(SR.OpenCSP_Failed, Convert.ToString(ret)));
}
safeProvHandle = hProv;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:26,代码来源:CapiHelper.cs
示例4: CryptAcquireContext
public static extern bool CryptAcquireContext(out SafeProvHandle psafeProvHandle, string pszContainer,
string pszProvider, int dwProvType, uint dwFlags);
开发者ID:SGuyGe,项目名称:corefx,代码行数:2,代码来源:CapiHelper.cs
示例5: GetNonZeroBytes
private static extern void GetNonZeroBytes(SafeProvHandle hProv, byte[] randomBytes, int count);
开发者ID:destinyclown,项目名称:coreclr,代码行数:1,代码来源:RNGCryptoServiceProvider.cs
示例6: SetPersistKeyInCsp
/// <summary>
/// Sets the PersistKeyInCsp
/// </summary>
/// <param name="safeProvHandle">Safe Prov Handle. Expects a valid handle</param>
/// <param name="fPersistKeyInCsp">Sets the PersistKeyInCsp value</param>
internal static void SetPersistKeyInCsp(SafeProvHandle safeProvHandle, bool fPersistKeyInCsp)
{
VerifyValidHandle(safeProvHandle);
safeProvHandle.PersistKeyInCsp = fPersistKeyInCsp;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:10,代码来源:CapiHelper.cs
示例7: GetKeyPairHelper
/// <summary>
/// Helper function to get the key pair
/// </summary>
internal static SafeKeyHandle GetKeyPairHelper(
CspAlgorithmType keyType,
CspParameters parameters,
int keySize,
SafeProvHandle safeProvHandle)
{
// If the key already exists, use it, else generate a new one
SafeKeyHandle hKey;
int hr = CapiHelper.GetUserKey(safeProvHandle, parameters.KeyNumber, out hKey);
if (hr != S_OK)
{
hKey.Dispose();
if (IsFlagBitSet((uint)parameters.Flags, (uint)CspProviderFlags.UseExistingKey) ||
(uint)hr != (uint)CryptKeyError.NTE_NO_KEY)
{
throw new CryptographicException(SR.Format(SR.CryptGetUserKey_Failed, Convert.ToString(hr)));
}
// GenerateKey will check for failures and throw an exception
CapiHelper.GenerateKey(safeProvHandle, parameters.KeyNumber, (int)parameters.Flags,
(uint)keySize, out hKey);
}
// check that this is indeed an RSA/DSS key.
byte[] algid = CapiHelper.GetKeyParameter(hKey, Constants.CLR_ALGID);
int dwAlgId = (algid[0] | (algid[1] << 8) | (algid[2] << 16) | (algid[3] << 24));
if ((keyType == CspAlgorithmType.Rsa && dwAlgId != CALG_RSA_KEYX && dwAlgId != CALG_RSA_SIGN) ||
(keyType == CspAlgorithmType.Dss && dwAlgId != CALG_DSS_SIGN))
{
hKey.Dispose();
throw new CryptographicException(SR.Format(SR.Cryptography_CSP_WrongKeySpec, Convert.ToString(keyType)));
}
return hKey;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:40,代码来源:CapiHelper.cs
示例8: GetUserKey
/// <summary>
/// Retrieves the handle for user public / private key pair.
/// </summary>
internal static int GetUserKey(SafeProvHandle safeProvHandle, int keySpec, out SafeKeyHandle safeKeyHandle)
{
int hr = S_OK;
VerifyValidHandle(safeProvHandle);
if (!Interop.CryptGetUserKey(safeProvHandle, keySpec, out safeKeyHandle))
{
hr = GetErrorCode();
}
if (hr == S_OK)
{
safeKeyHandle.KeySpec = keySpec;
}
return hr;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:17,代码来源:CapiHelper.cs
示例9: CryptGetUserKey
public static bool CryptGetUserKey(
SafeProvHandle safeProvHandle,
int dwKeySpec,
out SafeKeyHandle safeKeyHandle)
{
bool response = _CryptGetUserKey(safeProvHandle, dwKeySpec, out safeKeyHandle);
safeKeyHandle.SetParent(safeProvHandle);
return response;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:11,代码来源:CapiHelper.cs
示例10: _CryptCreateHash
private static extern bool _CryptCreateHash(SafeProvHandle hProv, int algId, SafeKeyHandle hKey, CryptCreateHashFlags dwFlags, out SafeHashHandle phHash);
开发者ID:SGuyGe,项目名称:corefx,代码行数:1,代码来源:CapiHelper.cs
示例11: _CryptImportKey
private static extern bool _CryptImportKey(SafeProvHandle hProv, byte[] pbData, int dwDataLen, SafeKeyHandle hPubKey, int dwFlags, out SafeKeyHandle phKey);
开发者ID:SGuyGe,项目名称:corefx,代码行数:1,代码来源:CapiHelper.cs
示例12: _CryptGenKey
private static extern bool _CryptGenKey(SafeProvHandle safeProvHandle, int Algid, int dwFlags, out SafeKeyHandle safeKeyHandle);
开发者ID:SGuyGe,项目名称:corefx,代码行数:1,代码来源:CapiHelper.cs
示例13: _CryptGetUserKey
private static extern bool _CryptGetUserKey(SafeProvHandle safeProvHandle, int dwKeySpec, out SafeKeyHandle safeKeyHandle);
开发者ID:SGuyGe,项目名称:corefx,代码行数:1,代码来源:CapiHelper.cs
示例14: CryptSetProvParam
public static extern bool CryptSetProvParam(SafeProvHandle safeProvHandle, CryptGetProvParam dwParam, ref IntPtr pbData, int dwFlags);
开发者ID:SGuyGe,项目名称:corefx,代码行数:1,代码来源:CapiHelper.cs
示例15: CryptGetProvParam
public static extern bool CryptGetProvParam(SafeProvHandle safeProvHandle, int dwParam, byte[] pbData,
ref int dwDataLen, int dwFlags);
开发者ID:SGuyGe,项目名称:corefx,代码行数:2,代码来源:CapiHelper.cs
示例16: GetProviderParameterWorker
/// <summary>
/// This method helps reduce the duplicate code in the GetProviderParameter method
/// </summary>
internal static int GetProviderParameterWorker(SafeProvHandle safeProvHandle, byte[] impType, ref int cb, CryptGetProvParam flags)
{
int impTypeReturn = 0;
if (!Interop.CryptGetProvParam(safeProvHandle, (int)flags, impType, ref cb, 0))
{
throw new CryptographicException(SR.Format(SR.CryptGetProvParam_Failed, Convert.ToString(GetErrorCode())));
}
if (null != impType && cb == Constants.SIZE_OF_DWORD)
{
impTypeReturn = BitConverter.ToInt32(impType, 0);
}
return impTypeReturn;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:16,代码来源:CapiHelper.cs
示例17: GetProviderParameter
/// <summary>
/// This method queries the key container and get some of it's properties.
/// Those properties should never cause UI to display.
/// </summary>
public static object GetProviderParameter(SafeProvHandle safeProvHandle, int keyNumber, int keyParam)
{
VerifyValidHandle(safeProvHandle);
byte[] impType = new byte[Constants.SIZE_OF_DWORD];
int cb = sizeof(byte) * Constants.SIZE_OF_DWORD;
SafeKeyHandle safeKeyHandle = SafeKeyHandle.InvalidHandle;
int impTypeReturn = 0;
int returnType = 0; //using 0 for bool and 1 for string return types
bool retVal = false;
string retStr = null;
try
{
switch (keyParam)
{
case Constants.CLR_EXPORTABLE:
{
impTypeReturn = GetProviderParameterWorker(safeProvHandle, impType, ref cb, CryptGetProvParam.PP_IMPTYPE);
//If implementation type is not HW
if (!IsFlagBitSet((uint)impTypeReturn, (uint)CryptGetProvParamPPImpTypeFlags.CRYPT_IMPL_HARDWARE))
{
if (!Interop.CryptGetUserKey(safeProvHandle, keyNumber, out safeKeyHandle))
{
throw new CryptographicException(SR.Format(SR.CryptGetUserKey_Failed, Convert.ToString(GetErrorCode())));
}
byte[] permissions = null;
int permissionsReturn = 0;
permissions = new byte[Constants.SIZE_OF_DWORD];
cb = sizeof(byte) * Constants.SIZE_OF_DWORD;
if (!Interop.CryptGetKeyParam(safeKeyHandle, (int)CryptGetKeyParamFlags.KP_PERMISSIONS, permissions, ref cb, 0))
{
throw new CryptographicException(SR.Format(SR.CryptGetKeyParam_Failed, Convert.ToString(GetErrorCode())));
}
permissionsReturn = BitConverter.ToInt32(permissions, 0);
retVal = IsFlagBitSet((uint)permissionsReturn, (uint)CryptGetKeyParamFlags.CRYPT_EXPORT);
}
else
{
//Assumption HW keys are not exportable.
retVal = false;
}
break;
}
case Constants.CLR_REMOVABLE:
{
impTypeReturn = GetProviderParameterWorker(safeProvHandle, impType, ref cb, CryptGetProvParam.PP_IMPTYPE);
retVal = IsFlagBitSet((uint)impTypeReturn, (uint)CryptGetProvParamPPImpTypeFlags.CRYPT_IMPL_REMOVABLE);
break;
}
case Constants.CLR_HARDWARE:
case Constants.CLR_PROTECTED:
{
impTypeReturn = GetProviderParameterWorker(safeProvHandle, impType, ref cb, CryptGetProvParam.PP_IMPTYPE);
retVal = IsFlagBitSet((uint)impTypeReturn, (uint)CryptGetProvParamPPImpTypeFlags.CRYPT_IMPL_HARDWARE);
break;
}
case Constants.CLR_ACCESSIBLE:
{
retVal = Interop.CryptGetUserKey(safeProvHandle, keyNumber, out safeKeyHandle) ? true : false;
break;
}
case Constants.CLR_UNIQUE_CONTAINER:
{
returnType = 1;
byte[] pb = null;
impTypeReturn = GetProviderParameterWorker(safeProvHandle, pb, ref cb, CryptGetProvParam.PP_UNIQUE_CONTAINER);
pb = new byte[cb];
impTypeReturn = GetProviderParameterWorker(safeProvHandle, pb, ref cb, CryptGetProvParam.PP_UNIQUE_CONTAINER);
// GetProviderParameterWorker allocated the null character, we want to not interpret that.
Debug.Assert(cb > 0);
Debug.Assert(pb[cb - 1] == 0);
retStr = Encoding.ASCII.GetString(pb, 0, cb - 1);
break;
}
default:
{
Debug.Assert(false);
break;
}
}
}
finally
{
safeKeyHandle.Dispose();
}
if (returnType == 0)
{
return retVal;
}
else if (returnType == 1)
{
return retStr;
}
return null;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:100,代码来源:CapiHelper.cs
示例18: CryptGenKey
public static bool CryptGenKey(
SafeProvHandle safeProvHandle,
int algId,
int dwFlags,
out SafeKeyHandle safeKeyHandle)
{
bool response = _CryptGenKey(safeProvHandle, algId, dwFlags, out safeKeyHandle);
safeKeyHandle.SetParent(safeProvHandle);
return response;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:12,代码来源:CapiHelper.cs
示例19: GenerateKey
/// <summary>
/// Generates the key if provided CSP handle is valid
/// </summary>
internal static int GenerateKey(SafeProvHandle safeProvHandle, int algID, int flags, uint keySize, out SafeKeyHandle safeKeyHandle)
{
int hr = S_OK;
VerifyValidHandle(safeProvHandle);
int capiFlags = (int)((uint)MapCspKeyFlags(flags) | ((uint)keySize << 16));
if (!Interop.CryptGenKey(safeProvHandle, algID, capiFlags, out safeKeyHandle))
{
hr = GetErrorCode();
}
if (hr != S_OK)
{
throw new CryptographicException(SR.Format(SR.CryptGenKey_Failed, Convert.ToString(GetErrorCode())));
}
safeKeyHandle.KeySpec = algID;
return hr;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:20,代码来源:CapiHelper.cs
示例20: CryptImportKey
public static bool CryptImportKey(
SafeProvHandle hProv,
byte[] pbData,
int dwDataLen,
SafeKeyHandle hPubKey,
int dwFlags,
out SafeKeyHandle phKey)
{
bool response = _CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, out phKey);
phKey.SetParent(hProv);
return response;
}
开发者ID:SGuyGe,项目名称:corefx,代码行数:14,代码来源:CapiHelper.cs
注:本文中的System.Security.Cryptography.SafeProvHandle类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论