Reputation: 577
I would just like to reproduce the decryption part which is in "Microsoft.AspNetCore.DataProtection" in .net 4.8.
It is to be able to share secrets with webapi which are in .net 4.8
for sample in .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);
The file contains the 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>
But into .net 4.8 if I follow this sample How to decrypt an AES-256-CBC encrypted string
How create derived key ivString and keyString with my master key pkxdnaumouftFkYEZczl40Ah5stOxhRFoQWFNmHoGHwAxRcpgbnfNRvdWFhInrpG6l+obewxh0gT/4tkDBtxDQ==
public static string Decrypt(string cipherData, string keyString, string ivString)
{
byte[] key = Encoding.UTF8.GetBytes(keyString);
byte[] iv = Encoding.UTF8.GetBytes(ivString);
try
{
using (var rijndaelManaged =
new RijndaelManaged {Key = key, IV = iv, Mode = CipherMode.CBC})
using (var memoryStream =
new MemoryStream(Convert.FromBase64String(cipherData)))
using (var cryptoStream =
new CryptoStream(memoryStream,
rijndaelManaged.CreateDecryptor(key, iv),
CryptoStreamMode.Read))
{
return new StreamReader(cryptoStream).ReadToEnd();
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
// You may want to catch more exceptions here...
}
Upvotes: 1
Views: 1208
Reputation: 577
I found how to do it, you have to import the package Microsoft.Owin.Security.Interop
Packages .net 4.X
Microsoft.Owin.Security.Interop
Packages .netcore
Microsoft.AspNetCore.DataProtection
Microsoft.AspNetCore.TestHost just for testing into UnitTest or console
here is the code, it's the same for .net Classic and .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);
Upvotes: 1