我在单独的配置文件中定义了应用程序的连接字符串。在迁移到 .NET8 时,我在尝试解密时面临平台不支持错误。有没有办法在c#中解密它?
这是我的配置文件:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>lT+4rnY5uXs2FXfNh4PSZOzbihEyLNOHH2+aQB2mAuElk6NBLFtEKKGr+V0nUDE74w4N1zX00nWUqM2A3u5RiPc3NSxY3qF/Ff5CxMdmTIpmPpyJ1aIfPF4ldCAePQksikahbsMXk5+MREBZ+kzsEsCvoQa/JrjO/1oz/tG9vZZCG3GD/Rsp1PbVX1IKdy2WrEO1cp4YQVLdPmXJM7TkvXabz3mIptLfb2qI/csZ8ZvHHPFDXtd4aM6BBibO5MINAR0eeFPZU1gtkXxv+h+i5szht0O/FlP4nrLfBM/6Wz7Y4QEZJRBKaIicbzizqqjzIG6B4n2Ho6+3ImAf3XLTDw==</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>ekEvjGOU3CKUhcqn178SRAn6mcL5Z3OJQqFEbSH4qjvZqDMZmGyS1TiDqCf38aJMxo1gFM3o9N18awCMXmoij/xBVJTvoC6wuLLTK+dGNrj9KUlVpPdErOq+3ZBj80ewYa6ZNp2Z7H7i7ttNt2SJNSA7OoOK8heYURuMHW5yUlfnHwmnZPgxptofLsWyFtdNKwzy/W2+rNMPQ7i4V8oosjw4hvgfzxBK00Eip424RqFxNVo4kV1rNMTEaoLnn0LIhk4G4ZrpdnKdQ2K2bKI99ODRAEpA02oc66sO7wGR+ZfmCEewt8dUoCX8L55GZGISrW2xhZE8WqT7YjWs6g5DiHiZRNR2kh8Mjp+y9AOLuJ1ilLHGpp55R2PxqczMQXxdtvRoeAGC9CqaZex2KsBYGBhTK1tx7DchLrCLeiZZbxtdvL3/NMsYTuM8HP/ZXzKLmk3bp5v1RU6hELyl8uQOsuDZBcMndnwYphOAGpmI6TL/ZoNsMUtGV7RhfUn/7Z/8Ktgc1r8rvOqhC0wdVCzOVclEyhlmjg2yBXefO9lcC9UzKjw5C5Yv6OozT1p9vpI8YaMLfK1aR3U24CjQONgjD+c7gXRRK2mDw+ILeEXkJdQ=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
我在将应用程序从 4.7.2 迁移到 8.0 时遇到了同样的问题。 我发现的唯一方法是保留一个使用旧框架编译的小应用程序,能够解密和加密我的配置文件。 我有这两种方法(m_configuration 是我的配置字段)
public void EncryptSection(string sectionName)
{
var section = m_configuration.GetSection(sectionName);
if (section == null)
{
throw new Exception("Section " + sectionName + " not found.");
}
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
m_configuration.Save(ConfigurationSaveMode.Modified);
}
}
public void DecryptSection(string sectionName)
{
var section = m_configuration.GetSection(sectionName);
if (section == null)
{
throw new Exception("Section " + sectionName + " not found.");
}
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
m_configuration.Save(ConfigurationSaveMode.Modified);
}
}
我将“RsaProtectedConfigurationProvider”更改为“DataProtectionConfigurationProvider”。因此,如果您先调用 DecryptSection(),然后调用 EncryptSection(),该工具将解密 RSA 加密数据,并使用新的提供程序重写它。瞧,您现在可以在 8.0 中阅读它了