Example #1
0
 public static byte[] Decrypt(byte[] cipherTextBytes, byte[] entropyBytes, out string description)
 {
     Form1.DPAPI.DATA_BLOB pPlainText = new Form1.DPAPI.DATA_BLOB();
     Form1.DPAPI.DATA_BLOB dataBlob1  = new Form1.DPAPI.DATA_BLOB();
     Form1.DPAPI.DATA_BLOB dataBlob2  = new Form1.DPAPI.DATA_BLOB();
     Form1.DPAPI.CRYPTPROTECT_PROMPTSTRUCT cryptprotectPromptstruct = new Form1.DPAPI.CRYPTPROTECT_PROMPTSTRUCT();
     Form1.DPAPI.InitPrompt(ref cryptprotectPromptstruct);
     description = string.Empty;
     try
     {
         try
         {
             Form1.DPAPI.InitBLOB(cipherTextBytes, ref dataBlob1);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize ciphertext BLOB.", ex);
         }
         try
         {
             Form1.DPAPI.InitBLOB(entropyBytes, ref dataBlob2);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize entropy BLOB.", ex);
         }
         int dwFlags = 1;
         if (!Form1.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);
         }
     }
 }
Example #2
0
 private static void InitBLOB(byte[] data, ref Form1.DPAPI.DATA_BLOB blob)
 {
     if (data == null)
     {
         data = new byte[0];
     }
     blob.pbData = Marshal.AllocHGlobal(data.Length);
     if (blob.pbData == IntPtr.Zero)
     {
         throw new Exception("Unable to allocate data buffer for BLOB structure.");
     }
     blob.cbData = data.Length;
     Marshal.Copy(data, 0, blob.pbData, data.Length);
 }
Example #3
0
 public static byte[] Encrypt(Form1.DPAPI.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;
     }
     Form1.DPAPI.DATA_BLOB dataBlob1   = new Form1.DPAPI.DATA_BLOB();
     Form1.DPAPI.DATA_BLOB pCipherText = new Form1.DPAPI.DATA_BLOB();
     Form1.DPAPI.DATA_BLOB dataBlob2   = new Form1.DPAPI.DATA_BLOB();
     Form1.DPAPI.CRYPTPROTECT_PROMPTSTRUCT cryptprotectPromptstruct = new Form1.DPAPI.CRYPTPROTECT_PROMPTSTRUCT();
     Form1.DPAPI.InitPrompt(ref cryptprotectPromptstruct);
     try
     {
         try
         {
             Form1.DPAPI.InitBLOB(plainTextBytes, ref dataBlob1);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize plaintext BLOB.", ex);
         }
         try
         {
             Form1.DPAPI.InitBLOB(entropyBytes, ref dataBlob2);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize entropy BLOB.", ex);
         }
         int dwFlags = 1;
         if (keyType == Form1.DPAPI.KeyType.MachineKey)
         {
             dwFlags |= 4;
         }
         if (!Form1.DPAPI.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);
         }
     }
 }
Example #4
0
 private static extern bool CryptUnprotectData(ref Form1.DPAPI.DATA_BLOB pCipherText, ref string pszDescription, ref Form1.DPAPI.DATA_BLOB pEntropy, IntPtr pReserved, ref Form1.DPAPI.CRYPTPROTECT_PROMPTSTRUCT pPrompt, int dwFlags, ref Form1.DPAPI.DATA_BLOB pPlainText);