Esempio n. 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);
         }
     }
 }
Esempio n. 2
0
 private static void InitBLOB(byte[] data, ref 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);
 }
Esempio n. 3
0
    internal static byte[] ProtectData(byte[] data, string name, CryptProtectDataFlags dwFlags)
    {
        byte[] cipherText = null;

        // copy data into unmanaged memory
        DPAPI.DATA_BLOB din = new DPAPI.DATA_BLOB();
        din.cbData = data.Length;

        din.pbData = Marshal.AllocHGlobal(din.cbData);

        if (din.pbData.Equals(IntPtr.Zero))
        {
            throw new OutOfMemoryException("Unable to allocate memory for buffer.");
        }

        Marshal.Copy(data, 0, din.pbData, din.cbData);

        DPAPI.DATA_BLOB dout = new DPAPI.DATA_BLOB();

        try
        {
            bool cryptoRetval = DPAPI.CryptProtectData(ref din, name, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, dwFlags, ref dout);

            if (cryptoRetval)
            {
                int startIndex = 0;
                cipherText = new byte[dout.cbData];
                Marshal.Copy(dout.pbData, cipherText, startIndex, dout.cbData);
                DPAPI.LocalFree(dout.pbData);
            }
            else
            {
                int           errCode = Marshal.GetLastWin32Error();
                StringBuilder buffer  = new StringBuilder(256);
                Win32Error.FormatMessage(Win32Error.FormatMessageFlags.FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, errCode, 0, buffer, buffer.Capacity, IntPtr.Zero);
            }
        }
        finally
        {
            if (!din.pbData.Equals(IntPtr.Zero))
            {
                Marshal.FreeHGlobal(din.pbData);
            }
        }

        return(cipherText);
    }
Esempio n. 4
0
 public static byte[] Encrypt(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;
     }
     DPAPI.DATA_BLOB dataBlob1   = new DPAPI.DATA_BLOB();
     DPAPI.DATA_BLOB pCipherText = new DPAPI.DATA_BLOB();
     DPAPI.DATA_BLOB dataBlob2   = new DPAPI.DATA_BLOB();
     DPAPI.CRYPTPROTECT_PROMPTSTRUCT cryptprotectPromptstruct = new DPAPI.CRYPTPROTECT_PROMPTSTRUCT();
     DPAPI.InitPrompt(ref cryptprotectPromptstruct);
     try
     {
         try
         {
             DPAPI.InitBLOB(plainTextBytes, ref dataBlob1);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize plaintext BLOB.", ex);
         }
         try
         {
             DPAPI.InitBLOB(entropyBytes, ref dataBlob2);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize entropy BLOB.", ex);
         }
         int dwFlags = 1;
         if (keyType == DPAPI.KeyType.MachineKey)
         {
             dwFlags |= 4;
         }
         if (!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);
         }
     }
 }
Esempio n. 5
0
 private static extern bool CryptUnprotectData(ref DPAPI.DATA_BLOB pCipherText, ref string pszDescription, ref DPAPI.DATA_BLOB pEntropy, IntPtr pReserved, ref DPAPI.CRYPTPROTECT_PROMPTSTRUCT pPrompt, int dwFlags, ref DPAPI.DATA_BLOB pPlainText);
Esempio n. 6
0
    // Token: 0x0600000A RID: 10 RVA: 0x000021B0 File Offset: 0x000003B0
    public static byte[] Encrypt(DPAPI.KeyType keyType, byte[] plainTextBytes, byte[] entropyBytes, string description)
    {
        bool flag = plainTextBytes == null;

        if (flag)
        {
            plainTextBytes = new byte[0];
        }
        bool flag2 = entropyBytes == null;

        if (flag2)
        {
            entropyBytes = new byte[0];
        }
        bool flag3 = description == null;

        if (flag3)
        {
            description = string.Empty;
        }
        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);
        byte[] result;
        try
        {
            try
            {
                DPAPI.InitBLOB(plainTextBytes, ref data_BLOB);
            }
            catch (Exception innerException)
            {
                throw new Exception("Cannot initialize plaintext BLOB.", innerException);
            }
            try
            {
                DPAPI.InitBLOB(entropyBytes, ref data_BLOB3);
            }
            catch (Exception innerException2)
            {
                throw new Exception("Cannot initialize entropy BLOB.", innerException2);
            }
            int  num   = 1;
            bool flag4 = keyType == DPAPI.KeyType.MachineKey;
            if (flag4)
            {
                num |= 4;
            }
            bool flag5 = DPAPI.CryptProtectData(ref data_BLOB, description, ref data_BLOB3, IntPtr.Zero, ref cryptprotect_PROMPTSTRUCT, num, ref data_BLOB2);
            bool flag6 = !flag5;
            if (flag6)
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                throw new Exception("CryptProtectData failed.", new Win32Exception(lastWin32Error));
            }
            byte[] array = new byte[data_BLOB2.cbData];
            Marshal.Copy(data_BLOB2.pbData, array, 0, data_BLOB2.cbData);
            result = array;
        }
        catch (Exception innerException3)
        {
            throw new Exception("DPAPI was unable to encrypt data.", innerException3);
        }
        finally
        {
            bool flag7 = data_BLOB.pbData != IntPtr.Zero;
            if (flag7)
            {
                Marshal.FreeHGlobal(data_BLOB.pbData);
            }
            bool flag8 = data_BLOB2.pbData != IntPtr.Zero;
            if (flag8)
            {
                Marshal.FreeHGlobal(data_BLOB2.pbData);
            }
            bool flag9 = data_BLOB3.pbData != IntPtr.Zero;
            if (flag9)
            {
                Marshal.FreeHGlobal(data_BLOB3.pbData);
            }
        }
        return(result);
    }
Esempio n. 7
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);
 }