Visual Studio 如何加密 .pubxml.user 文件中的密码?

Jez*_*Jez 7 encryption passwords visual-studio webdeploy visual-studio-2019

当您告诉 Visual Studio 保存发布配置文件的密码时,它会.pubxml.user在您的发布文件旁边创建一个文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <TimeStampOfAssociatedLegacyPublishXmlFile />
    <EncryptedPassword>AQAAANC[...]</EncryptedPassword>
  </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

Visual Studio 实际上如何加密元素中的密码EncryptedPassword?我想解密它,因为我忘记了密码......现在它只是加密存储在这个文件中!

Top*_*aco 10

数据经过DPAPI加密。DPAPI 加密数据以字节序列0x01000000D08C9DDF0115D1118C7A00C04FC297EB或 Base64 编码的十六进制开头AQAAANCMnd8BFdERjHoAwE/Cl+此处

对于使用 C# 解密,可以使用类ProtectedData或更准确地说是静态方法。ProtectedData.Unprotect如果熵值未知s_aditionalEntropynull则应尝试。有关此参数的更多信息可以在此处找到。

如果加密数据是Base64编码的,则在解密之前必须先进行Base64解码:

using System.Security.Cryptography;

...
String encryptedDataB64 = "AQAAANCMnd8BFdERjHoAwE/Cl+...";
byte[] encryptedData = Convert.FromBase64String(encryptedDataB64); 

byte[] s_aditionalEntropy = null;
byte[] data = ProtectedData.Unprotect(encryptedData, s_aditionalEntropy, DataProtectionScope.CurrentUser); 
Run Code Online (Sandbox Code Playgroud)

可以在链接文档中找到更详细的示例。解密不限于.NET,还可以使用其他语言,只要存在相应的 DPAPI 包装器,例如在带有 Java DPAPI 的 Python 中win32crypt.CryptUnprotectData或在带有Java DPAPI的 Java 中。

下面是一个获取解码数据的 Unicode 字符串表示形式的控制台程序:

using System;
using System.Security.Cryptography;

namespace ConsolePassDecrypter {
    class Program {
        static void Main(string[] args) {
            string encryptedPass = "AQAAANC[...]";
            var decryptedPassBytes = ProtectedData.Unprotect(Convert.FromBase64String(encryptedPass), null, DataProtectionScope.LocalMachine);
            Console.WriteLine("Decrypted pass: " + System.Text.Encoding.Unicode.GetString(decryptedPassBytes));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)