Пример #1
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);
    }
Пример #2
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);
         }
     }
 }
Пример #3
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);
    }