protected override bool ReleaseHandle() { bool flag = System.IdentityModel.NativeMethods.CryptDestroyKey(base.handle); if (this.provHandle != null) { this.provHandle.DangerousRelease(); this.provHandle = null; } return flag; }
protected override bool ReleaseHandle() { bool flag = System.IdentityModel.NativeMethods.CryptDestroyKey(base.handle); if (this.provHandle != null) { this.provHandle.DangerousRelease(); this.provHandle = null; } return(flag); }
internal static unsafe System.IdentityModel.SafeKeyHandle SafeCryptImportKey(System.IdentityModel.SafeProvHandle provHandle, void *pbDataPtr, int cbData) { bool success = false; int error = 0; System.IdentityModel.SafeKeyHandle phKey = null; RuntimeHelpers.PrepareConstrainedRegions(); try { provHandle.DangerousAddRef(ref success); } catch (Exception exception) { if (success) { provHandle.DangerousRelease(); success = false; } if (!(exception is ObjectDisposedException)) { throw; } } finally { if (success) { success = System.IdentityModel.NativeMethods.CryptImportKey(provHandle, pbDataPtr, (uint)cbData, IntPtr.Zero, 0, out phKey); if (!success) { error = Marshal.GetLastWin32Error(); provHandle.DangerousRelease(); } else { phKey.provHandle = provHandle; } } } if (!success) { Utility.CloseInvalidOutSafeHandle(phKey); string str = (error != 0) ? new Win32Exception(error).Message : string.Empty; throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(System.IdentityModel.SR.GetString("AESCryptImportKeyFailed", new object[] { str }))); } return(phKey); }
public unsafe RijndaelCryptoTransform(byte[] rgbKey, byte[] rgbIV, PaddingMode paddingMode, int blockSizeBits, bool encrypt) { if (((rgbKey.Length != 0x10) && (rgbKey.Length != 0x18)) && (rgbKey.Length != 0x20)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("AESKeyLengthNotSupported", new object[] { rgbKey.Length * 8 }))); } if (rgbIV.Length != 0x10) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("AESIVLengthNotSupported", new object[] { rgbIV.Length * 8 }))); } if ((paddingMode != PaddingMode.PKCS7) && (paddingMode != PaddingMode.ISO10126)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("AESPaddingModeNotSupported", new object[] { paddingMode }))); } this.paddingMode = paddingMode; this.blockSize = blockSizeBits / 8; this.encrypt = encrypt; System.IdentityModel.SafeProvHandle phProv = null; System.IdentityModel.SafeKeyHandle phKey = null; try { ThrowIfFalse("AESCryptAcquireContextFailed", System.IdentityModel.NativeMethods.CryptAcquireContextW(out phProv, null, null, 0x18, 0xf0000000)); int cbData = PLAINTEXTKEYBLOBHEADER.SizeOf + rgbKey.Length; byte[] dst = new byte[cbData]; Buffer.BlockCopy(rgbKey, 0, dst, PLAINTEXTKEYBLOBHEADER.SizeOf, rgbKey.Length); fixed (IntPtr* ptrRef = dst) { PLAINTEXTKEYBLOBHEADER* plaintextkeyblobheaderPtr = (PLAINTEXTKEYBLOBHEADER*) ptrRef; plaintextkeyblobheaderPtr->bType = 8; plaintextkeyblobheaderPtr->bVersion = 2; plaintextkeyblobheaderPtr->reserved = 0; if (rgbKey.Length == 0x10) { plaintextkeyblobheaderPtr->aiKeyAlg = 0x660e; } else if (rgbKey.Length == 0x18) { plaintextkeyblobheaderPtr->aiKeyAlg = 0x660f; } else { plaintextkeyblobheaderPtr->aiKeyAlg = 0x6610; } plaintextkeyblobheaderPtr->keyLength = rgbKey.Length; phKey = System.IdentityModel.SafeKeyHandle.SafeCryptImportKey(phProv, (void*) ptrRef, cbData); } fixed (IntPtr* ptrRef2 = rgbIV) { ThrowIfFalse("AESCryptSetKeyParamFailed", System.IdentityModel.NativeMethods.CryptSetKeyParam(phKey, 1, (void*) ptrRef2, 0)); } this.keyHandle = phKey; this.provHandle = phProv; phKey = null; phProv = null; } finally { if (phKey != null) { phKey.Close(); } if (phProv != null) { phProv.Close(); } } }
public unsafe RijndaelCryptoTransform(byte[] rgbKey, byte[] rgbIV, PaddingMode paddingMode, int blockSizeBits, bool encrypt) { if (((rgbKey.Length != 0x10) && (rgbKey.Length != 0x18)) && (rgbKey.Length != 0x20)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("AESKeyLengthNotSupported", new object[] { rgbKey.Length * 8 }))); } if (rgbIV.Length != 0x10) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("AESIVLengthNotSupported", new object[] { rgbIV.Length * 8 }))); } if ((paddingMode != PaddingMode.PKCS7) && (paddingMode != PaddingMode.ISO10126)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(System.IdentityModel.SR.GetString("AESPaddingModeNotSupported", new object[] { paddingMode }))); } this.paddingMode = paddingMode; this.blockSize = blockSizeBits / 8; this.encrypt = encrypt; System.IdentityModel.SafeProvHandle phProv = null; System.IdentityModel.SafeKeyHandle phKey = null; try { ThrowIfFalse("AESCryptAcquireContextFailed", System.IdentityModel.NativeMethods.CryptAcquireContextW(out phProv, null, null, 0x18, 0xf0000000)); int cbData = PLAINTEXTKEYBLOBHEADER.SizeOf + rgbKey.Length; byte[] dst = new byte[cbData]; Buffer.BlockCopy(rgbKey, 0, dst, PLAINTEXTKEYBLOBHEADER.SizeOf, rgbKey.Length); fixed(IntPtr *ptrRef = dst) { PLAINTEXTKEYBLOBHEADER *plaintextkeyblobheaderPtr = (PLAINTEXTKEYBLOBHEADER *)ptrRef; plaintextkeyblobheaderPtr->bType = 8; plaintextkeyblobheaderPtr->bVersion = 2; plaintextkeyblobheaderPtr->reserved = 0; if (rgbKey.Length == 0x10) { plaintextkeyblobheaderPtr->aiKeyAlg = 0x660e; } else if (rgbKey.Length == 0x18) { plaintextkeyblobheaderPtr->aiKeyAlg = 0x660f; } else { plaintextkeyblobheaderPtr->aiKeyAlg = 0x6610; } plaintextkeyblobheaderPtr->keyLength = rgbKey.Length; phKey = System.IdentityModel.SafeKeyHandle.SafeCryptImportKey(phProv, (void *)ptrRef, cbData); } fixed(IntPtr *ptrRef2 = rgbIV) { ThrowIfFalse("AESCryptSetKeyParamFailed", System.IdentityModel.NativeMethods.CryptSetKeyParam(phKey, 1, (void *)ptrRef2, 0)); } this.keyHandle = phKey; this.provHandle = phProv; phKey = null; phProv = null; } finally { if (phKey != null) { phKey.Close(); } if (phProv != null) { phProv.Close(); } } }