示例#1
0
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        private string EncryptText(string clearText)
        {
            if (clearText == null || clearText.Length < 1)
            {
                return(clearText);
            }

            DATA_BLOB inputData, entData, outputData;
            SafeNativeMemoryHandle safeInputDataHandle  = new SafeNativeMemoryHandle();
            SafeNativeMemoryHandle safeOutputDataHandle = new SafeNativeMemoryHandle(true);
            SafeNativeMemoryHandle safeEntDataHandle    = new SafeNativeMemoryHandle();

            inputData.pbData = entData.pbData = outputData.pbData = IntPtr.Zero;
            inputData.cbData = entData.cbData = outputData.cbData = 0;
            try {
                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                }
                finally {
                    inputData = PrepareDataBlob(clearText);
                    safeInputDataHandle.SetDataHandle(inputData.pbData);

                    entData = PrepareDataBlob(_KeyEntropy);
                    safeEntDataHandle.SetDataHandle(entData.pbData);
                }

                CRYPTPROTECT_PROMPTSTRUCT prompt = PreparePromptStructure();
                UInt32 dwFlags = CRYPTPROTECT_UI_FORBIDDEN;
                if (UseMachineProtection)
                {
                    dwFlags |= CRYPTPROTECT_LOCAL_MACHINE;
                }
                bool success = false;

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                }
                finally {
                    success = UnsafeNativeMethods.CryptProtectData(ref inputData, "", ref entData,
                                                                   IntPtr.Zero, ref prompt,
                                                                   dwFlags, ref outputData);
                    safeOutputDataHandle.SetDataHandle(outputData.pbData);
                }
                if (!success || outputData.pbData == IntPtr.Zero)
                {
                    outputData.pbData = IntPtr.Zero;
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }
                byte[] buf = new byte[outputData.cbData];
                Marshal.Copy(outputData.pbData, buf, 0, buf.Length);
                return(Convert.ToBase64String(buf));
            } finally {
                if (!(safeOutputDataHandle == null || safeOutputDataHandle.IsInvalid))
                {
                    safeOutputDataHandle.Dispose();
                    outputData.pbData = IntPtr.Zero;
                }
                if (!(safeEntDataHandle == null || safeEntDataHandle.IsInvalid))
                {
                    safeEntDataHandle.Dispose();
                    entData.pbData = IntPtr.Zero;
                }
                if (!(safeInputDataHandle == null || safeInputDataHandle.IsInvalid))
                {
                    safeInputDataHandle.Dispose();
                    inputData.pbData = IntPtr.Zero;
                }
            }
        }
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        private string EncryptText(string clearText)
        {
            if (clearText == null || clearText.Length < 1)
                return clearText;

            DATA_BLOB inputData, entData, outputData;
            SafeNativeMemoryHandle safeInputDataHandle = new SafeNativeMemoryHandle();
            SafeNativeMemoryHandle safeOutputDataHandle = new SafeNativeMemoryHandle(true);
            SafeNativeMemoryHandle safeEntDataHandle = new SafeNativeMemoryHandle();

            inputData.pbData = entData.pbData = outputData.pbData = IntPtr.Zero;
            inputData.cbData = entData.cbData = outputData.cbData = 0;
            try {

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                }
                finally {
                    inputData = PrepareDataBlob(clearText);
                    safeInputDataHandle.SetDataHandle(inputData.pbData);

                    entData = PrepareDataBlob(_KeyEntropy);
                    safeEntDataHandle.SetDataHandle(entData.pbData);
                }

                CRYPTPROTECT_PROMPTSTRUCT   prompt      = PreparePromptStructure();
                UInt32                      dwFlags     = CRYPTPROTECT_UI_FORBIDDEN;
                if (UseMachineProtection)
                    dwFlags |= CRYPTPROTECT_LOCAL_MACHINE;
                bool success = false;

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                }
                finally {
                    success = UnsafeNativeMethods.CryptProtectData(ref inputData, "", ref entData,
                                                                    IntPtr.Zero, ref prompt,
                                                                    dwFlags, ref outputData);
                    safeOutputDataHandle.SetDataHandle(outputData.pbData);
                }
                if (!success || outputData.pbData == IntPtr.Zero) {
                    outputData.pbData = IntPtr.Zero;
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }
                byte[] buf = new byte[outputData.cbData];
                Marshal.Copy(outputData.pbData, buf, 0, buf.Length);
                return Convert.ToBase64String(buf);
            } finally {
                if (!(safeOutputDataHandle == null || safeOutputDataHandle.IsInvalid)) {
                    safeOutputDataHandle.Dispose();
                    outputData.pbData = IntPtr.Zero;
                }
                if (!(safeEntDataHandle == null || safeEntDataHandle.IsInvalid)) {
                    safeEntDataHandle.Dispose();
                    entData.pbData = IntPtr.Zero;
                }
                if (!(safeInputDataHandle == null || safeInputDataHandle.IsInvalid)) {
                    safeInputDataHandle.Dispose();
                    inputData.pbData = IntPtr.Zero;
                }
            }
        }
        private string DecryptText(string encText)
        {
            DATA_BLOB data_blob;
            DATA_BLOB data_blob2;
            DATA_BLOB data_blob3;
            string    str;

            if ((encText == null) || (encText.Length < 1))
            {
                return(encText);
            }
            SafeNativeMemoryHandle handle  = new SafeNativeMemoryHandle();
            SafeNativeMemoryHandle handle2 = new SafeNativeMemoryHandle(true);
            SafeNativeMemoryHandle handle3 = new SafeNativeMemoryHandle();

            data_blob.pbData = data_blob2.pbData = data_blob3.pbData = IntPtr.Zero;
            data_blob.cbData = data_blob2.cbData = data_blob3.cbData = 0;
            try
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    data_blob = PrepareDataBlob(Convert.FromBase64String(encText));
                    handle.SetDataHandle(data_blob.pbData);
                    data_blob2 = PrepareDataBlob(this._KeyEntropy);
                    handle3.SetDataHandle(data_blob2.pbData);
                }
                CRYPTPROTECT_PROMPTSTRUCT promptStruct = PreparePromptStructure();
                uint flags = 1;
                if (this.UseMachineProtection)
                {
                    flags |= 4;
                }
                bool flag = false;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    flag = Microsoft.Win32.UnsafeNativeMethods.CryptUnprotectData(ref data_blob, IntPtr.Zero, ref data_blob2, IntPtr.Zero, ref promptStruct, flags, ref data_blob3);
                    handle2.SetDataHandle(data_blob3.pbData);
                }
                if (!flag || (data_blob3.pbData == IntPtr.Zero))
                {
                    data_blob3.pbData = IntPtr.Zero;
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }
                byte[] destination = new byte[data_blob3.cbData];
                Marshal.Copy(data_blob3.pbData, destination, 0, destination.Length);
                str = Encoding.Unicode.GetString(destination);
            }
            finally
            {
                if ((handle2 != null) && !handle2.IsInvalid)
                {
                    handle2.Dispose();
                    data_blob3.pbData = IntPtr.Zero;
                }
                if ((handle3 != null) && !handle3.IsInvalid)
                {
                    handle3.Dispose();
                    data_blob2.pbData = IntPtr.Zero;
                }
                if ((handle != null) && !handle.IsInvalid)
                {
                    handle.Dispose();
                    data_blob.pbData = IntPtr.Zero;
                }
            }
            return(str);
        }