• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# CRYPTPROTECT_PROMPTSTRUCT类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中CRYPTPROTECT_PROMPTSTRUCT的典型用法代码示例。如果您正苦于以下问题:C# CRYPTPROTECT_PROMPTSTRUCT类的具体用法?C# CRYPTPROTECT_PROMPTSTRUCT怎么用?C# CRYPTPROTECT_PROMPTSTRUCT使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



CRYPTPROTECT_PROMPTSTRUCT类属于命名空间,在下文中一共展示了CRYPTPROTECT_PROMPTSTRUCT类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: CryptUnprotectData

 bool CryptUnprotectData(ref DATA_BLOB pCipherText,
                         ref string pszDescription,
                         ref DATA_BLOB pEntropy,
                             IntPtr pReserved,
                         ref CRYPTPROTECT_PROMPTSTRUCT pPrompt,
                             int dwFlags,
                         ref DATA_BLOB pPlainText);
开发者ID:philippthiele,项目名称:ChromePasswordDump,代码行数:7,代码来源:DPAPI.cs


示例2: InitPromptstruct

		private static void InitPromptstruct(ref CRYPTPROTECT_PROMPTSTRUCT ps) 
		{
			ps.cbSize = Marshal.SizeOf(typeof(CRYPTPROTECT_PROMPTSTRUCT));
			ps.dwPromptFlags = 0;
			ps.hwndApp = IntPtr.Zero;
			ps.szPrompt = null;
		}
开发者ID:jpespartero,项目名称:WorldWind,代码行数:7,代码来源:DataProtection.cs


示例3: CryptUnprotectData

 private static extern bool CryptUnprotectData(
                                   ref DATA_BLOB pDataIn,
                                   String szDataDescr,
                                   ref DATA_BLOB pOptionalEntropy,
                                   IntPtr pvReserved,
                                   ref CRYPTPROTECT_PROMPTSTRUCT
                                     pPromptStruct,
                                   int dwFlags,
                                   ref DATA_BLOB pDataOut);
开发者ID:Nullstr1ng,项目名称:MultiRDPClient.NET,代码行数:9,代码来源:DataProtector.cs


示例4: Decrypt

 public static byte[] Decrypt(byte[] cipherTextBytes, byte[] entropyBytes, out string description)
 {
     DATA_BLOB pPlainText = new DATA_BLOB();
     DATA_BLOB dataBlob1 = new DATA_BLOB();
     DATA_BLOB dataBlob2 = new DATA_BLOB();
     CRYPTPROTECT_PROMPTSTRUCT cryptprotectPromptstruct = new CRYPTPROTECT_PROMPTSTRUCT();
     DataProtection.InitPrompt(ref cryptprotectPromptstruct);
     description = string.Empty;
     try
     {
         try
         {
             DataProtection.InitBLOB(cipherTextBytes, ref dataBlob1);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize ciphertext BLOB.", ex);
         }
         try
         {
             DataProtection.InitBLOB(entropyBytes, ref dataBlob2);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize entropy BLOB.", ex);
         }
         int dwFlags = 1;
         if (!Advent.Common.Interop.NativeMethods.CryptUnprotectData(ref dataBlob1, ref description, ref dataBlob2, IntPtr.Zero, ref cryptprotectPromptstruct, dwFlags, ref pPlainText))
             throw new Exception("CryptUnprotectData failed.", (Exception)new Win32Exception(Marshal.GetLastWin32Error()));
         byte[] destination = new byte[pPlainText.cbData];
         Marshal.Copy(pPlainText.pbData, destination, 0, pPlainText.cbData);
         return destination;
     }
     catch (Exception ex)
     {
         throw new Exception("Unable to decrypt data.", ex);
     }
     finally
     {
         if (pPlainText.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(pPlainText.pbData);
         if (dataBlob1.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(dataBlob1.pbData);
         if (dataBlob2.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(dataBlob2.pbData);
     }
 }
开发者ID:kingpin2k,项目名称:MCS,代码行数:47,代码来源:DataProtection.cs


示例5: Encrypt

        public static string Encrypt(string unencrypted)
        {
            CryptProtectFlags flags = CryptProtectFlags.CRYPTPROTECT_UI_FORBIDDEN;
            DATA_BLOB unencryptedBlob = ConvertData(Encoding.Unicode.GetBytes(unencrypted));
            DATA_BLOB encryptedBlob = new DATA_BLOB();
            DATA_BLOB dataOption = new DATA_BLOB();

            try
            {
                CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
                if (!CryptProtectData(ref unencryptedBlob, "psw", ref dataOption, IntPtr.Zero, ref prompt, flags, ref encryptedBlob))
                {
                    int errCode = Marshal.GetLastWin32Error();
                    throw new AmazonClientException("CryptProtectData failed. Error Code: " + errCode);
                }

                byte[] outData = new byte[encryptedBlob.cbData];
                Marshal.Copy(encryptedBlob.pbData, outData, 0, outData.Length);


                StringBuilder encrypted = new StringBuilder();
                for (int i = 0; i <= outData.Length - 1; i++)
                {
                    encrypted.Append(
                        Convert.ToString(outData[i], 16).PadLeft(2, '0').ToUpper(CultureInfo.InvariantCulture));
                }

                string encryptedPassword = encrypted.ToString().ToUpper(CultureInfo.InvariantCulture);
                return encryptedPassword;
            }
            finally
            {
                if (unencryptedBlob.pbData != IntPtr.Zero)
                    Marshal.FreeHGlobal(unencryptedBlob.pbData);
                if (encryptedBlob.pbData != IntPtr.Zero)
                    Marshal.FreeHGlobal(encryptedBlob.pbData);
            }
        }
开发者ID:aws,项目名称:aws-sdk-net,代码行数:38,代码来源:UserCrypto.cs


示例6: Decrypt

        public static string Decrypt(string encrypted)
        {
            List<Byte> dataIn = new List<byte>();
            for (int i = 0; i < encrypted.Length; i = i + 2)
            {
                byte data = Convert.ToByte(encrypted.Substring(i, 2), 16);
                dataIn.Add(data);
            }

            CryptProtectFlags flags = CryptProtectFlags.CRYPTPROTECT_UI_FORBIDDEN;
            DATA_BLOB encryptedBlob = ConvertData(dataIn.ToArray());
            DATA_BLOB unencryptedBlob = new DATA_BLOB();
            DATA_BLOB dataOption = new DATA_BLOB();

            try
            {

                CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
                if (!CryptUnprotectData(ref encryptedBlob, "psw", ref dataOption, IntPtr.Zero, ref prompt, flags, ref unencryptedBlob))
                {
                    int errCode = Marshal.GetLastWin32Error();
                    throw new AmazonClientException("CryptProtectData failed. Error Code: " + errCode);
                }

                byte[] outData = new byte[unencryptedBlob.cbData];
                Marshal.Copy(unencryptedBlob.pbData, outData, 0, outData.Length);

                string unencrypted = Encoding.Unicode.GetString(outData);
                return unencrypted;
            }
            finally
            {
                if (encryptedBlob.pbData != IntPtr.Zero)
                    Marshal.FreeHGlobal(encryptedBlob.pbData);
                if (unencryptedBlob.pbData != IntPtr.Zero)
                    Marshal.FreeHGlobal(unencryptedBlob.pbData);
            }
        }
开发者ID:aws,项目名称:aws-sdk-net,代码行数:38,代码来源:UserCrypto.cs


示例7: decrypt

 public static byte[] decrypt(byte[] cipherTextBytes)
 {
     try
     {
         DATA_BLOB plainTextBlob = new DATA_BLOB();
         DATA_BLOB cipherTextBlob = new DATA_BLOB();
         DATA_BLOB entropyBlob = new DATA_BLOB();
         CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
         string description = String.Empty;
         InitPrompt(ref prompt);
         try
         {
             InitBLOB(cipherTextBytes, ref cipherTextBlob);
         }
         catch { }
         int flags = 0x1;
         bool success = CryptUnprotectData(ref cipherTextBlob,
                                           ref description,
                                           ref entropyBlob,
                                               IntPtr.Zero,
                                           ref prompt,
                                               flags,
                                           ref plainTextBlob);
         if (success)
         {
             byte[] plainTextBytes = new byte[plainTextBlob.cbData];
             Marshal.Copy(plainTextBlob.pbData,
                      plainTextBytes,
                      0,
                      plainTextBlob.cbData);
             return plainTextBytes;
         }
     }
     catch { }
     return null;
 }
开发者ID:jimbojetset,项目名称:GetChromePasswords,代码行数:36,代码来源:DPAPI.cs


示例8: Unprotect

		public static byte[] Unprotect (byte[] encryptedData, byte[] optionalEntropy, DataProtectionScope scope)
		{
			byte[] decdata = null;
			int hr = 0;

			DATA_BLOB cipher = new DATA_BLOB ();
			DATA_BLOB entropy = new DATA_BLOB ();
			DATA_BLOB data = new DATA_BLOB ();
			try {
				CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT (0);
				cipher.Alloc (encryptedData);
				entropy.Alloc (optionalEntropy);

				// note: the scope/flags has already been check by the public caller
				uint flags = CRYPTPROTECT_UI_FORBIDDEN;
				if (scope == DataProtectionScope.LocalMachine)
					flags |= CRYPTPROTECT_LOCAL_MACHINE;

				if (CryptUnprotectData (ref cipher, null, ref entropy, IntPtr.Zero,
					ref prompt, flags, ref data)) {
					// copy decrypted data back to managed codde
					decdata = data.ToBytes ();
				} else {
					hr = Marshal.GetLastWin32Error ();
				}
			}
			catch (Exception ex) {
				string msg = Locale.GetText ("Error protecting data.");
				throw new CryptographicException (msg, ex);
			}
			finally {
				cipher.Free ();
				data.Free ();
				entropy.Free ();
			}

			if ((decdata == null) || (hr != 0)) {
				throw new CryptographicException (hr);
			}
			return decdata;
		}
开发者ID:Profit0004,项目名称:mono,代码行数:41,代码来源:NativeDapiProtection.cs


示例9: Decrypt

        /// <summary>
        /// Decrypt byte data
        /// </summary>
        /// <param name="cipherText">Data to be decoded</param>
        /// <param name="optionalEntropy">Additional entropy, recommended for machine-specific case</param>
        /// <returns>Returns a byte array with the encoded data</returns>
        internal byte[] Decrypt(byte[] cipherText, byte[] optionalEntropy)
        {
            bool retVal = false;

            DATA_BLOB plainTextBlob = new DATA_BLOB();
            DATA_BLOB cipherBlob = new DATA_BLOB();
            CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
            InitPromptstruct(ref prompt);

            try
            {
                try
                {
                    int cipherTextSize = cipherText.Length;
                    cipherBlob.pbData = Marshal.AllocHGlobal(cipherTextSize);
                    if(IntPtr.Zero == cipherBlob.pbData)
                    {
                        throw new Exception("Unable to allocate cipherText buffer.");
                    }
                    cipherBlob.cbData = cipherTextSize;
                    Marshal.Copy(cipherText, 0, cipherBlob.pbData,
                        cipherBlob.cbData);
                }
                catch(Exception ex)
                {
                    throw new Exception("Exception marshalling data. " +
                        ex.Message);
                }
                DATA_BLOB entropyBlob = new DATA_BLOB();
                int dwFlags;
                if(Store.USE_MACHINE_STORE == store)
                {
                    //Using the machine store, should be providing entropy.
                    dwFlags =
                        CRYPTPROTECT_LOCAL_MACHINE|CRYPTPROTECT_UI_FORBIDDEN;
                    //Check to see if the entropy is null
                    if(null == optionalEntropy)
                    {
                        //Allocate something
                        optionalEntropy = new byte[0];
                    }
                    try
                    {
                        int bytesSize = optionalEntropy.Length;
                        entropyBlob.pbData = Marshal.AllocHGlobal(bytesSize);
                        if(IntPtr.Zero == entropyBlob.pbData)
                        {
                            throw new Exception("Unable to allocate entropy buffer.");
                        }
                        entropyBlob.cbData = bytesSize;
                        Marshal.Copy(optionalEntropy, 0, entropyBlob.pbData,
                            bytesSize);
                    }
                    catch(Exception ex)
                    {
                        throw new Exception("Exception marshalling entropy data. " +
                            ex.Message);
                    }
                }
                else
                {
                    //Using the user store
                    dwFlags = CRYPTPROTECT_UI_FORBIDDEN;
                }
                retVal = CryptUnprotectData(ref cipherBlob, null, ref
                    entropyBlob,
                    IntPtr.Zero, ref prompt, dwFlags,
                    ref plainTextBlob);
                if(false == retVal)
                {
                    throw new Exception("Decryption failed. " +
                        Win32Message.GetMessage(Marshal.GetLastWin32Error()));
                }
                //Free the blob and entropy.
                if(IntPtr.Zero != cipherBlob.pbData)
                {
                    Marshal.FreeHGlobal(cipherBlob.pbData);
                }
                if(IntPtr.Zero != entropyBlob.pbData)
                {
                    Marshal.FreeHGlobal(entropyBlob.pbData);
                }
            }
            catch(Exception ex)
            {
                throw new Exception("Exception decrypting. " + ex.Message);
            }
            byte[] plainText = new byte[plainTextBlob.cbData];
            Marshal.Copy(plainTextBlob.pbData, plainText, 0, plainTextBlob.cbData);
            Marshal.FreeHGlobal(plainTextBlob.pbData);
            return plainText;
        }
开发者ID:paladin74,项目名称:Dapple,代码行数:98,代码来源:DataProtection.cs


示例10: CryptProtectData

 private static extern bool CryptProtectData(ref DATA_BLOB pPlainText, string szDescription, ref DATA_BLOB pEntropy, IntPtr pReserved,
     ref CRYPTPROTECT_PROMPTSTRUCT pPrompt, int dwFlags, ref DATA_BLOB pCipherText);
开发者ID:Xiryl,项目名称:ChromeRec,代码行数:2,代码来源:DPAPI.cs


示例11: Encrypt

 public byte[] Encrypt(byte[] plainText, byte[] optionalEntropy)
 {
     DATA_BLOB pDataIn = new DATA_BLOB();
     DATA_BLOB pDataOut = new DATA_BLOB();
     DATA_BLOB pOptionalEntropy = new DATA_BLOB();
     CRYPTPROTECT_PROMPTSTRUCT ps = new CRYPTPROTECT_PROMPTSTRUCT();
     this.InitPromptstruct(ref ps);
     try
     {
         int num;
         try
         {
             int length = plainText.Length;
             pDataIn.pbData = Marshal.AllocHGlobal(length);
             if (IntPtr.Zero == pDataIn.pbData)
             {
                 throw new Exception("Unable to allocate plaintext buffer.");
             }
             pDataIn.cbData = length;
             Marshal.Copy(plainText, 0, pDataIn.pbData, length);
         }
         catch (Exception exception)
         {
             throw new Exception("Exception marshalling data. " + exception.Message);
         }
         if (Store.Machine == this.store)
         {
             num = 5;
             if (optionalEntropy == null)
             {
                 optionalEntropy = new byte[0];
             }
             try
             {
                 int num3 = optionalEntropy.Length;
                 pOptionalEntropy.pbData = Marshal.AllocHGlobal(optionalEntropy.Length);
                 if (IntPtr.Zero == pOptionalEntropy.pbData)
                 {
                     throw new Exception("Unable to allocate entropy data buffer.");
                 }
                 Marshal.Copy(optionalEntropy, 0, pOptionalEntropy.pbData, num3);
                 pOptionalEntropy.cbData = num3;
                 goto Label_010F;
             }
             catch (Exception exception2)
             {
                 throw new Exception("Exception entropy marshalling data. " + exception2.Message);
             }
         }
         num = 1;
     Label_010F:
         if (!CryptProtectData(ref pDataIn, "", ref pOptionalEntropy, IntPtr.Zero, ref ps, num, ref pDataOut))
         {
             throw new Exception("Encryption failed. " + GetErrorMessage(Marshal.GetLastWin32Error()));
         }
     }
     catch (Exception exception3)
     {
         throw new Exception("Exception encrypting. " + exception3.Message);
     }
     byte[] destination = new byte[pDataOut.cbData];
     Marshal.Copy(pDataOut.pbData, destination, 0, pDataOut.cbData);
     return destination;
 }
开发者ID:jokingzhou,项目名称:AnJi-DevZoneGIS,代码行数:64,代码来源:DataProtector.cs


示例12: InitPrompt

 private static void InitPrompt(ref CRYPTPROTECT_PROMPTSTRUCT ps)
 {
     ps.cbSize = Marshal.SizeOf(typeof(CRYPTPROTECT_PROMPTSTRUCT));
     ps.dwPromptFlags = 0;
     ps.hwndApp = DataProtection.NullPtr;
     ps.szPrompt = (string)null;
 }
开发者ID:kingpin2k,项目名称:MCS,代码行数:7,代码来源:DataProtection.cs


示例13: Encrypt

 public static byte[] Encrypt(DataProtection.KeyType keyType, byte[] plainTextBytes, byte[] entropyBytes, string description)
 {
     if (plainTextBytes == null)
         plainTextBytes = new byte[0];
     if (entropyBytes == null)
         entropyBytes = new byte[0];
     if (description == null)
         description = string.Empty;
     DATA_BLOB dataBlob1 = new DATA_BLOB();
     DATA_BLOB pCipherText = new DATA_BLOB();
     DATA_BLOB dataBlob2 = new DATA_BLOB();
     CRYPTPROTECT_PROMPTSTRUCT cryptprotectPromptstruct = new CRYPTPROTECT_PROMPTSTRUCT();
     DataProtection.InitPrompt(ref cryptprotectPromptstruct);
     try
     {
         try
         {
             DataProtection.InitBLOB(plainTextBytes, ref dataBlob1);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize plaintext BLOB.", ex);
         }
         try
         {
             DataProtection.InitBLOB(entropyBytes, ref dataBlob2);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize entropy BLOB.", ex);
         }
         int dwFlags = 1;
         if (keyType == DataProtection.KeyType.MachineKey)
             dwFlags |= 4;
         if (!Advent.Common.Interop.NativeMethods.CryptProtectData(ref dataBlob1, description, ref dataBlob2, IntPtr.Zero, ref cryptprotectPromptstruct, dwFlags, ref pCipherText))
             throw new Exception("CryptProtectData failed.", (Exception)new Win32Exception(Marshal.GetLastWin32Error()));
         byte[] destination = new byte[pCipherText.cbData];
         Marshal.Copy(pCipherText.pbData, destination, 0, pCipherText.cbData);
         return destination;
     }
     catch (Exception ex)
     {
         throw new Exception("DPAPI was unable to encrypt data.", ex);
     }
     finally
     {
         if (dataBlob1.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(dataBlob1.pbData);
         if (pCipherText.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(pCipherText.pbData);
         if (dataBlob2.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(dataBlob2.pbData);
     }
 }
开发者ID:kingpin2k,项目名称:MCS,代码行数:54,代码来源:DataProtection.cs


示例14: InitPrompt

 private static void InitPrompt(ref CRYPTPROTECT_PROMPTSTRUCT ps)
 {
     ps.cbSize = Marshal.SizeOf(
                               typeof(CRYPTPROTECT_PROMPTSTRUCT));
     ps.dwPromptFlags = 0;
     ps.hwndApp = ((IntPtr)((int)(0)));
     ps.szPrompt = null;
 }
开发者ID:jimbojetset,项目名称:GetChromePasswords,代码行数:8,代码来源:DPAPI.cs


示例15: InitPrompt

 /// <summary>
 /// Initializes empty prompt structure.
 /// </summary>
 /// <param name="ps">
 /// Prompt parameter (which we do not actually need).
 /// </param>
 private static void InitPrompt(ref CRYPTPROTECT_PROMPTSTRUCT ps)
 {
     ps.cbSize       = Marshal.SizeOf(ps);
     ps.dwPromptFlags= 0;
     ps.hwndApp      = NullPtr;
     ps.szPrompt     = null;
 }
开发者ID:trendSWM,项目名称:rdesktopce_rdp5,代码行数:13,代码来源:dpapi.cs


示例16: Decrypt

 public byte[] Decrypt(byte[] cipherText, byte[] optionalEntropy)
 {
     DATA_BLOB pDataOut = new DATA_BLOB();
     DATA_BLOB pDataIn = new DATA_BLOB();
     CRYPTPROTECT_PROMPTSTRUCT ps = new CRYPTPROTECT_PROMPTSTRUCT();
     this.InitPromptstruct(ref ps);
     try
     {
         int num2;
         try
         {
             int length = cipherText.Length;
             pDataIn.pbData = Marshal.AllocHGlobal(length);
             if (IntPtr.Zero == pDataIn.pbData)
             {
                 throw new Exception("Unable to allocate cipherText buffer.");
             }
             pDataIn.cbData = length;
             Marshal.Copy(cipherText, 0, pDataIn.pbData, pDataIn.cbData);
         }
         catch (Exception exception)
         {
             throw new Exception("Exception marshalling data. " + exception.Message);
         }
         DATA_BLOB pOptionalEntropy = new DATA_BLOB();
         if (Store.Machine == this.store)
         {
             num2 = 5;
             if (optionalEntropy == null)
             {
                 optionalEntropy = new byte[0];
             }
             try
             {
                 int cb = optionalEntropy.Length;
                 pOptionalEntropy.pbData = Marshal.AllocHGlobal(cb);
                 if (IntPtr.Zero == pOptionalEntropy.pbData)
                 {
                     throw new Exception("Unable to allocate entropy buffer.");
                 }
                 pOptionalEntropy.cbData = cb;
                 Marshal.Copy(optionalEntropy, 0, pOptionalEntropy.pbData, cb);
                 goto Label_0113;
             }
             catch (Exception exception2)
             {
                 throw new Exception("Exception entropy marshalling data. " + exception2.Message);
             }
         }
         num2 = 1;
     Label_0113:
         if (!CryptUnprotectData(ref pDataIn, null, ref pOptionalEntropy, IntPtr.Zero, ref ps, num2, ref pDataOut))
         {
             throw new Exception("Decryption failed. " + GetErrorMessage(Marshal.GetLastWin32Error()));
         }
         if (IntPtr.Zero != pDataIn.pbData)
         {
             Marshal.FreeHGlobal(pDataIn.pbData);
         }
         if (IntPtr.Zero != pOptionalEntropy.pbData)
         {
             Marshal.FreeHGlobal(pOptionalEntropy.pbData);
         }
     }
     catch (Exception exception3)
     {
         throw new Exception("Exception decrypting. " + exception3.Message);
     }
     byte[] destination = new byte[pDataOut.cbData];
     Marshal.Copy(pDataOut.pbData, destination, 0, pDataOut.cbData);
     return destination;
 }
开发者ID:jokingzhou,项目名称:AnJi-DevZoneGIS,代码行数:72,代码来源:DataProtector.cs


示例17: Encrypt

    public static byte[] Encrypt(KeyType keyType, byte[] plainTextBytes, byte[] entropyBytes, string description)
    {
        // Make sure that parameters are valid.
        if (plainTextBytes == null) plainTextBytes = new byte[0];
        if (entropyBytes == null) entropyBytes = new byte[0];
        if (description == null) description = String.Empty;

        // Create BLOBs to hold data.
        DATA_BLOB plainTextBlob = new DATA_BLOB();
        DATA_BLOB cipherTextBlob = new DATA_BLOB();
        DATA_BLOB entropyBlob = new DATA_BLOB();

        // We only need prompt structure because it is a required
        // parameter.
        CRYPTPROTECT_PROMPTSTRUCT prompt =
                                  new CRYPTPROTECT_PROMPTSTRUCT();
        InitPrompt(ref prompt);

        try
        {
            // Convert plaintext bytes into a BLOB structure.
            try
            {
                InitBLOB(plainTextBytes, ref plainTextBlob);
            }
            catch (Exception ex)
            {
                throw new Exception(
                    "Cannot initialize plaintext BLOB.", ex);
            }

            // Convert entropy bytes into a BLOB structure.
            try
            {
                InitBLOB(entropyBytes, ref entropyBlob);
            }
            catch (Exception ex)
            {
                throw new Exception(
                    "Cannot initialize entropy BLOB.", ex);
            }

            // Disable any types of UI.
            int flags = CRYPTPROTECT_UI_FORBIDDEN;

            // When using machine-specific key, set up machine flag.
            if (keyType == KeyType.MachineKey)
                flags |= CRYPTPROTECT_LOCAL_MACHINE;

            // Call DPAPI to encrypt data.
            bool success = CryptProtectData(ref plainTextBlob,
                                                description,
                                            ref entropyBlob,
                                                IntPtr.Zero,
                                            ref prompt,
                                                flags,
                                            ref cipherTextBlob);
            // Check the result.
            if (!success)
            {
                // If operation failed, retrieve last Win32 error.
                int errCode = Marshal.GetLastWin32Error();

                // Win32Exception will contain error message corresponding
                // to the Windows error code.
                throw new Exception(
                    "CryptProtectData failed.", new Win32Exception(errCode));
            }

            // Allocate memory to hold ciphertext.
            byte[] cipherTextBytes = new byte[cipherTextBlob.cbData];

            // Copy ciphertext from the BLOB to a byte array.
            Marshal.Copy(cipherTextBlob.pbData,
                            cipherTextBytes,
                            0,
                            cipherTextBlob.cbData);

            // Return the result.
            return cipherTextBytes;
        }
        catch (Exception ex)
        {
            throw new Exception("DPAPI was unable to encrypt data.", ex);
        }
        // Free all memory allocated for BLOBs.
        finally
        {
            if (plainTextBlob.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(plainTextBlob.pbData);

            if (cipherTextBlob.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(cipherTextBlob.pbData);

            if (entropyBlob.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(entropyBlob.pbData);
        }
    }
开发者ID:Xiryl,项目名称:ChromeRec,代码行数:98,代码来源:DPAPI.cs


示例18: Encrypt

            public byte[] Encrypt(byte[] plainText, byte[] optionalEntropy)
            {
                bool retVal = false;

                DATA_BLOB plainTextBlob = new DATA_BLOB();
                DATA_BLOB cipherTextBlob = new DATA_BLOB();
                DATA_BLOB entropyBlob = new DATA_BLOB();

                CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
                InitPromptstruct(ref prompt);

                int dwFlags;
                try
                {
                    try
                    {
                        int bytesSize = plainText.Length;
                        plainTextBlob.pbData = Marshal.AllocHGlobal(bytesSize);
                        if (IntPtr.Zero == plainTextBlob.pbData)
                        {
                            throw new Exception("Unable to allocate plaintext buffer.");
                        }
                        plainTextBlob.cbData = bytesSize;
                        Marshal.Copy(plainText, 0, plainTextBlob.pbData, bytesSize);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Exception marshalling data. " + ex.Message);
                    }
                    if (Store.Machine == store)
                    {
                        //Using the machine store, should be providing entropy.
                        dwFlags = CRYPTPROTECT_LOCAL_MACHINE | CRYPTPROTECT_UI_FORBIDDEN;
                        //Check to see if the entropy is null
                        if (null == optionalEntropy)
                        {
                            //Allocate something
                            optionalEntropy = new byte[0];
                        }
                        try
                        {
                            int bytesSize = optionalEntropy.Length;
                            entropyBlob.pbData = Marshal.AllocHGlobal(optionalEntropy.Length);
                            if (IntPtr.Zero == entropyBlob.pbData)
                            {
                                throw new Exception("Unable to allocate entropy data buffer.");
                            }
                            Marshal.Copy(optionalEntropy, 0, entropyBlob.pbData, bytesSize);
                            entropyBlob.cbData = bytesSize;
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Exception entropy marshalling data. " + ex.Message);
                        }
                    }
                    else
                    {
                        //Using the user store
                        dwFlags = CRYPTPROTECT_UI_FORBIDDEN;
                    }
                    retVal = CryptProtectData(ref plainTextBlob, "", ref entropyBlob,
                        IntPtr.Zero, ref prompt, dwFlags, ref cipherTextBlob);
                    if (false == retVal)
                    {
                        throw new Exception("Encryption failed. " + GetErrorMessage(Marshal.GetLastWin32Error()));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Exception encrypting. " + ex.Message);
                }
                byte[] cipherText = new byte[cipherTextBlob.cbData];
                Marshal.Copy(cipherTextBlob.pbData, cipherText, 0, cipherTextBlob.cbData);
                return cipherText;

            }
开发者ID:javithalion,项目名称:NCache,代码行数:76,代码来源:Protector.cs


示例19: Decrypt

    public static byte[] Decrypt(byte[] cipherTextBytes, byte[] entropyBytes, out string description)
    {
        // Create BLOBs to hold data.
        DATA_BLOB plainTextBlob = new DATA_BLOB();
        DATA_BLOB cipherTextBlob = new DATA_BLOB();
        DATA_BLOB entropyBlob = new DATA_BLOB();

        // We only need prompt structure because it is a required
        // parameter.
        CRYPTPROTECT_PROMPTSTRUCT prompt =
                                  new CRYPTPROTECT_PROMPTSTRUCT();
        InitPrompt(ref prompt);

        // Initialize description string.
        description = String.Empty;

        try
        {
            // Convert ciphertext bytes into a BLOB structure.
            try
            {
                InitBLOB(cipherTextBytes, ref cipherTextBlob);
            }
            catch (Exception ex)
            {
                throw new Exception(
                    "Cannot initialize ciphertext BLOB.", ex);
            }

            // Convert entropy bytes into a BLOB structure.
            try
            {
                InitBLOB(entropyBytes, ref entropyBlob);
            }
            catch (Exception ex)
            {
                throw new Exception(
                    "Cannot initialize entropy BLOB.", ex);
            }

            // Disable any types of UI. CryptUnprotectData does not
            // mention CRYPTPROTECT_LOCAL_MACHINE flag in the list of
            // supported flags so we will not set it up.
            int flags = CRYPTPROTECT_UI_FORBIDDEN;

            // Call DPAPI to decrypt data.
            bool success = CryptUnprotectData(ref cipherTextBlob,
                                              ref description,
                                              ref entropyBlob,
                                                  IntPtr.Zero,
                                              ref prompt,
                                                  flags,
                                              ref plainTextBlob);

            // Check the result.
            if (!success)
            {
                // If operation failed, retrieve last Win32 error.
                int errCode = Marshal.GetLastWin32Error();

                // Win32Exception will contain error message corresponding
                // to the Windows error code.
                throw new Exception(
                    "CryptUnprotectData failed.", new Win32Exception(errCode));
            }

            // Allocate memory to hold plaintext.
            byte[] plainTextBytes = new byte[plainTextBlob.cbData];

            // Copy ciphertext from the BLOB to a byte array.
            Marshal.Copy(plainTextBlob.pbData,
                         plainTextBytes,
                         0,
                         plainTextBlob.cbData);

            // Return the result.
            return plainTextBytes;
        }
        catch (Exception ex)
        {
            throw new Exception("DPAPI was unable to decrypt data.", ex);
        }
        // Free all memory allocated for BLOBs.
        finally
        {
            if (plainTextBlob.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(plainTextBlob.pbData);

            if (cipherTextBlob.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(cipherTextBlob.pbData);

            if (entropyBlob.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(entropyBlob.pbData);
        }
    }
开发者ID:Xiryl,项目名称:ChromeRec,代码行数:95,代码来源:DPAPI.cs


示例20: CryptProtectData

 public static extern bool CryptProtectData(ref CRYPTOAPI_BLOB pDataIn, string szDataDescr,
                                        ref CRYPTOAPI_BLOB pOptionalEntropy, IntPtr pvReserved,
                                        ref CRYPTPROTECT_PROMPTSTRUCT pPromptStruct, uint dwFlags,
                                        ref CRYPTOAPI_BLOB pDataOut);
开发者ID:bmadarasz,项目名称:ndihelpdesk,代码行数:4,代码来源:Cryptography.cs



注:本文中的CRYPTPROTECT_PROMPTSTRUCT类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# CSMTestEnvironmentFactory类代码示例发布时间:2022-05-24
下一篇:
C# CRSpline类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap