Воспроизведите расшифровку Microsoft.AspNetCore.DataProtection с помощью инфраструктуры. net 4.8 - PullRequest
0 голосов
/ 12 февраля 2020

Я просто хотел бы воспроизвести часть расшифровки, которая находится в «Microsoft.AspNetCore.DataProtection» в. net 4.8.

Это возможность делиться секретами с вебапами, которые находятся в. net 4.8

для образца в .netCore

UTF8Encoding _encodeur = new UTF8Encoding(false, true);
var services = new ServiceCollection();
services.AddDataProtection()
    .PersistKeysToFileSystem(new DirectoryInfo("c:\\temp\\secu"))
    .SetApplicationName("INTRANET");
var providerService = services.BuildServiceProvider();
var providerProtection = providerService.GetDataProtectionProvider();

var protector = providerProtection.CreateProtector("INTRANET");

var plaintext = "mon texte";
// PROTECT
var plaintextBytes = _encodeur.GetBytes(plaintext);
var protectBytes = protector.Protect(plaintextBytes);

// UNPROTECT
var unprotectBytes = protector.Unprotect(protectBytes);
var unprotectPlaintext = _encodeur.GetString(unprotectBytes);

Файл содержит masterKey

<?xml version="1.0" encoding="utf-8"?>
<key id="cd977748-5482-42a4-85e7-fb6138ef6b10" version="1">
  <creationDate>2020-02-12T10:04:08.28082Z</creationDate>
  <activationDate>2020-02-12T10:04:01.9592602Z</activationDate>
  <expirationDate>2020-05-12T10:04:01.9592602Z</expirationDate>
  <descriptor deserializerType="Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null">
    <descriptor>
      <encryption algorithm="AES_256_CBC" />
      <validation algorithm="HMACSHA256" />
      <masterKey p4:requiresEncryption="true" xmlns:p4="http://schemas.asp.net/2015/03/dataProtection">
        <!-- Warning: the key below is in an unencrypted form. -->
        <value>pkxdnaumouftFkYEZczl40Ah5stOxhRFoQWFNmHoGHwAxRcpgbnfNRvdWFhInrpG6l+obewxh0gT/4tkDBtxDQ==</value>
      </masterKey>
    </descriptor>
  </descriptor>
</key>

Но в. net 4.8, если я последую этому примеру Как расшифровать зашифрованную строку AES-256-CB C

Как создать производный ключ ivString и keyString с помощью моего мастер-ключа pkxdnaumouftFkYEZczl40Ah5stOxhRFoQWFNmHoGHwAxRcpgbnfn * * 4x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6hwhgvx0x6x6hwhgvx0x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6x6hxxvx6x6hxxvx6x6x6x6x6x6hxp0

1 Ответ

0 голосов
/ 13 февраля 2020

Я нашел, как это сделать, нужно импортировать пакет Microsoft.Owin.Security.Interop

Пакеты. net 4.X

Microsoft .Owin.Security.Interop

Пакеты .netcore

Microsoft.AspNetCore.DataProtection

Microsoft.AspNetCore.TestHost только для тестирования в UnitTest или консоль

вот код, он одинаков для. net Classi c и .netCore

var services = new ServiceCollection();
services.AddDataProtection()
    .PersistKeysToSqlServer("INTRANET");// with custom datastore
// .PersistKeysToFileSystem(new DirectoryInfo("c:\\temp\\secu"));

var providerService = services.BuildServiceProvider();
var providerProtection = providerService.GetDataProtectionProvider();

var dataProtector = providerProtection.CreateProtector("INTRANET");
//var ticketFormat = new AspNetTicketDataFormat(new DataProtectorShim(dataProtector));

var plaintext = "mon texte";
var protectText = dataProtector.Protect(plaintext);
var unprotect = dataProtector.Unprotect(protectText);
Debug.Assert(plaintext == unprotect);
...