【问题标题】:How does Visual Studio encrypt passwords in a .pubxml.user file?Visual Studio 如何加密 .pubxml.user 文件中的密码?
【发布时间】:2023-03-08 03:58:01
【问题描述】:

当您告诉 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>

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

【问题讨论】:

  • 基于开头AQAAANC的纯猜测:开头是AQAAANCMnd8BFdERjHoAwE/Cl+(这是0x01000000D08C9DDF0115D1118C7A00C04FC297EB的Base64编码)?那么它们可能是 DPAPI 加密的。在对数据进行 Base64 解码后,应该可以使用 C# 类 ProtectedData 进行解密,更准确地说是使用方法 ProtectedData.Unprotect(请参阅链接文档以获取示例)。如果s_aditionalEntropy 没有已知值,我会尝试null
  • @Topaco 请给出这个答案,我可以奖励赏金。

标签: visual-studio encryption passwords visual-studio-2019 webdeploy


【解决方案1】:

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

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

如果加密后的数据是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); 

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

这是一个获取解码数据的 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));
        }
    }
}

【讨论】:

    猜你喜欢
    • 2017-05-04
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    • 2021-11-13
    • 1970-01-01
    • 2017-09-10
    • 2013-04-16
    相关资源
    最近更新 更多