private static void InitPrompt(ref DPAPI.CRYPTPROTECT_PROMPTSTRUCT ps) { ps.cbSize = Marshal.SizeOf(typeof(DPAPI.CRYPTPROTECT_PROMPTSTRUCT)); ps.dwPromptFlags = 0; ps.hwndApp = DPAPI.NullPtr; ps.szPrompt = null; }
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; if (!DPAPI.CryptUnprotectData(ref dATA_BLOB2, ref description, ref dATA_BLOB3, IntPtr.Zero, ref cRYPTPROTECT_PROMPTSTRUCT, dwFlags, ref dATA_BLOB)) { Marshal.GetLastWin32Error(); } 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 { 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); }
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); }
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);