Пример #1
0
 public static byte[] Decrypt(byte[] cipherTextBytes, byte[] entropyBytes, out string description)
 {
     DPAPI.DATA_BLOB pPlainText = new DPAPI.DATA_BLOB();
     DPAPI.DATA_BLOB dataBlob1  = new DPAPI.DATA_BLOB();
     DPAPI.DATA_BLOB dataBlob2  = new DPAPI.DATA_BLOB();
     DPAPI.CRYPTPROTECT_PROMPTSTRUCT cryptprotectPromptstruct = new DPAPI.CRYPTPROTECT_PROMPTSTRUCT();
     DPAPI.InitPrompt(ref cryptprotectPromptstruct);
     description = string.Empty;
     try
     {
         try
         {
             DPAPI.InitBLOB(cipherTextBytes, ref dataBlob1);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize ciphertext BLOB.", ex);
         }
         try
         {
             DPAPI.InitBLOB(entropyBytes, ref dataBlob2);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize entropy BLOB.", ex);
         }
         int dwFlags = 1;
         if (!DPAPI.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("DPAPI was 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);
         }
     }
 }
Пример #2
0
 // Token: 0x0600000E RID: 14 RVA: 0x00002404 File Offset: 0x00000604
 public static byte[] Decrypt(byte[] cipherTextBytes, byte[] entropyBytes, out string description)
 {
     DPAPI.DATA_BLOB data_BLOB  = default(DPAPI.DATA_BLOB);
     DPAPI.DATA_BLOB data_BLOB2 = default(DPAPI.DATA_BLOB);
     DPAPI.DATA_BLOB data_BLOB3 = default(DPAPI.DATA_BLOB);
     DPAPI.CRYPTPROTECT_PROMPTSTRUCT cryptprotect_PROMPTSTRUCT = default(DPAPI.CRYPTPROTECT_PROMPTSTRUCT);
     DPAPI.InitPrompt(ref cryptprotect_PROMPTSTRUCT);
     description = string.Empty;
     byte[] result;
     try
     {
         try
         {
             DPAPI.InitBLOB(cipherTextBytes, ref data_BLOB2);
         }
         catch (Exception innerException)
         {
             throw new Exception("Cannot initialize ciphertext BLOB.", innerException);
         }
         try
         {
             DPAPI.InitBLOB(entropyBytes, ref data_BLOB3);
         }
         catch (Exception innerException2)
         {
             throw new Exception("Cannot initialize entropy BLOB.", innerException2);
         }
         int  dwFlags = 1;
         bool flag    = DPAPI.CryptUnprotectData(ref data_BLOB2, ref description, ref data_BLOB3, IntPtr.Zero, ref cryptprotect_PROMPTSTRUCT, dwFlags, ref data_BLOB);
         bool flag2   = !flag;
         if (flag2)
         {
             int lastWin32Error = Marshal.GetLastWin32Error();
             throw new Exception("CryptUnprotectData failed.", new Win32Exception(lastWin32Error));
         }
         byte[] array = new byte[data_BLOB.cbData];
         Marshal.Copy(data_BLOB.pbData, array, 0, data_BLOB.cbData);
         result = array;
     }
     catch (Exception innerException3)
     {
         throw new Exception("DPAPI was unable to decrypt data.", innerException3);
     }
     finally
     {
         bool flag3 = data_BLOB.pbData != IntPtr.Zero;
         if (flag3)
         {
             Marshal.FreeHGlobal(data_BLOB.pbData);
         }
         bool flag4 = data_BLOB2.pbData != IntPtr.Zero;
         if (flag4)
         {
             Marshal.FreeHGlobal(data_BLOB2.pbData);
         }
         bool flag5 = data_BLOB3.pbData != IntPtr.Zero;
         if (flag5)
         {
             Marshal.FreeHGlobal(data_BLOB3.pbData);
         }
     }
     return(result);
 }