本文整理汇总了C#中SafeKeyHandle.Close方法的典型用法代码示例。如果您正苦于以下问题:C# SafeKeyHandle.Close方法的具体用法?C# SafeKeyHandle.Close怎么用?C# SafeKeyHandle.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SafeKeyHandle
的用法示例。
在下文中一共展示了SafeKeyHandle.Close方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RijndaelCryptoTransform
public unsafe RijndaelCryptoTransform(byte[] rgbKey, byte[] rgbIV, PaddingMode paddingMode, int blockSizeBits, bool encrypt)
{
if (rgbKey.Length != 16 && rgbKey.Length != 24 && rgbKey.Length != 32)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.AESKeyLengthNotSupported, rgbKey.Length * 8)));
if (rgbIV.Length != 16)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.AESIVLengthNotSupported, rgbIV.Length * 8)));
if (paddingMode != PaddingMode.PKCS7 && paddingMode != PaddingMode.ISO10126)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.AESPaddingModeNotSupported, paddingMode)));
this.paddingMode = paddingMode;
DiagnosticUtility.DebugAssert((blockSizeBits % 8) == 0, "Bits must be byte aligned.");
this.blockSize = blockSizeBits / 8;
this.encrypt = encrypt;
SafeProvHandle provHandle = null;
SafeKeyHandle keyHandle = null;
try
{
#pragma warning suppress 56523
ThrowIfFalse(SR.AESCryptAcquireContextFailed, NativeMethods.CryptAcquireContextW(out provHandle, null, null, NativeMethods.PROV_RSA_AES, NativeMethods.CRYPT_VERIFYCONTEXT));
// (BLOBHEADER + keyLen) + Key
int cbData = PLAINTEXTKEYBLOBHEADER.SizeOf + rgbKey.Length;
byte[] pbData = new byte[cbData];
Buffer.BlockCopy(rgbKey, 0, pbData, PLAINTEXTKEYBLOBHEADER.SizeOf, rgbKey.Length);
fixed (void* pbDataPtr = &pbData[0])
{
PLAINTEXTKEYBLOBHEADER* pbhdr = (PLAINTEXTKEYBLOBHEADER*)pbDataPtr;
pbhdr->bType = NativeMethods.PLAINTEXTKEYBLOB;
pbhdr->bVersion = NativeMethods.CUR_BLOB_VERSION;
pbhdr->reserved = 0;
if (rgbKey.Length == 16)
pbhdr->aiKeyAlg = NativeMethods.CALG_AES_128;
else if (rgbKey.Length == 24)
pbhdr->aiKeyAlg = NativeMethods.CALG_AES_192;
else
pbhdr->aiKeyAlg = NativeMethods.CALG_AES_256;
pbhdr->keyLength = rgbKey.Length;
keyHandle = SafeKeyHandle.SafeCryptImportKey(provHandle, pbDataPtr, cbData);
}
#if DEBUG
uint ivLen = 0;
#pragma warning suppress 56523 // win32 error checked in ThrowIfFalse() method
ThrowIfFalse(SR.AESCryptGetKeyParamFailed, NativeMethods.CryptGetKeyParam(keyHandle, NativeMethods.KP_IV, IntPtr.Zero, ref ivLen, 0));
DiagnosticUtility.DebugAssert(rgbIV.Length == ivLen, "Mismatch iv size");
#endif
fixed (void* pbIVPtr = &rgbIV[0])
{
#pragma warning suppress 56523
ThrowIfFalse(SR.AESCryptSetKeyParamFailed, NativeMethods.CryptSetKeyParam(keyHandle, NativeMethods.KP_IV, pbIVPtr, 0));
}
// Save
this.keyHandle = keyHandle;
this.provHandle = provHandle;
keyHandle = null;
provHandle = null;
}
finally
{
if (keyHandle != null)
keyHandle.Close();
if (provHandle != null)
provHandle.Close();
}
}