Beispiel #1
0
 public static string Encrypt(DPAPI.KeyType keyType, string plainText, string entropy, string description)
 {
     if (plainText == null)
     {
         plainText = string.Empty;
     }
     if (entropy == null)
     {
         entropy = string.Empty;
     }
     return(Convert.ToBase64String(DPAPI.Encrypt(keyType, Encoding.UTF8.GetBytes(plainText), Encoding.UTF8.GetBytes(entropy), description)));
 }
Beispiel #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 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;
         if (keyType == DPAPI.KeyType.MachineKey)
         {
             num |= 4;
         }
         if (!DPAPI.CryptProtectData(ref dATA_BLOB, description, ref dATA_BLOB3, IntPtr.Zero, ref cRYPTPROTECT_PROMPTSTRUCT, num, ref dATA_BLOB2))
         {
             Marshal.GetLastWin32Error();
         }
         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
     {
         if (dATA_BLOB.pbData != IntPtr.Zero)
         {
             Marshal.FreeHGlobal(dATA_BLOB.pbData);
         }
         if (dATA_BLOB2.pbData != IntPtr.Zero)
         {
             Marshal.FreeHGlobal(dATA_BLOB2.pbData);
         }
         if (dATA_BLOB3.pbData != IntPtr.Zero)
         {
             Marshal.FreeHGlobal(dATA_BLOB3.pbData);
         }
     }
     return(result);
 }
Beispiel #3
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);
         }
     }
 }
Beispiel #4
0
 public static string Encrypt(DPAPI.KeyType keyType, string plainText, string entropy)
 {
     return(DPAPI.Encrypt(keyType, plainText, entropy, string.Empty));
 }
Beispiel #5
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);
    }