How to Import a .NET 8.0 DLL with System.Security.Cryptography in PowerShell?

0
7
Asked By CleverJellyfish123 On

I've been working on a C# class that uses DPAPI for data protection, but I'm having trouble when I try to import the compiled DLL into PowerShell. The error I'm getting is:

> Import-Module: Could not load file or assembly 'System.Security.Cryptography.ProtectedData, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

My C# project references `System.Security.Cryptography.ProtectedData` version `10.0.1` and targets `.NET 8.0-windows`. I've tried a few troubleshooting steps, like changing output types, building in different configurations, and importing the DLL directly, but nothing has worked.

I really want a built-in method for securing data via the C# library, primarily to handle credentials securely without messing around with passwords or certificates. Can this work, and what might I be doing wrong? By the way, I'm running PowerShell 7.4.13, which is supposed to target .NET 8.

4 Answers

Answered By SlightlyConfusedCat On

Have you considered using this code in PowerShell instead? It works:

```powershell
[Text.Encoding]::ASCII.GetString(
[Security.Cryptography.ProtectedData]::Unprotect(
[Convert]::FromBase64String(
$encryptedData
),
$null,
'CurrentUser'
)
)
```
However, I understand you want to keep everything in your C# library for easier access to objects.

Answered By CuriousCoder2022 On

I saw you mentioned importing a .NET 8.0 library. Make sure your PowerShell is indeed running on the right version—versions before 7.4 won't support it. You should be fine, but it's a good thing to double-check.

Answered By LostInTheCode On

Does PowerShell load the required assembly by default? It might be that there's a version conflict. If you're just looking to store strings securely, consider creating a [SecureString] within PowerShell. You can export it as XML with `Export-Clixml` and re-import it later. If it's credential data, you might want to nest it in a [PSCredential] to keep it easy and secure.

Answered By TechieTurtle99 On

You might want to ensure that your C# project is targeting the same .NET version that PowerShell is using. Just double-check that you're aligned with .NET 8, especially since you're using PowerShell 7.4.13.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.