PowerShell - System.Security.Cryptography.AesGcm not found despite required .NET version installed correctly

James Vega 41 Reputation points
2021-06-21T11:23:18.73+00:00

Hello,

I need to use AesGcm class in PowerShell 5.1.19041.906 in Windows 10 (Version 20H2 (OS Build 19042.985) and by following the Microsoft documentation https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.aesgcm?view=net-5.0 it requires that one of the following .NET software versions are installed:

Product Versions

.NET 5.0 6.0 Preview 3

.NET Core 3.0 3.1

.NET Standard 2.1

I installed Microsoft .NET Runtime - 5.0.7 (x64) from https://dotnet.microsoft.com/download/dotnet/5.0 but when I try to search the functions related AesGcm, I cannot find anything despite I call "Add-Type -Assembly System.Security". I also tried to install the other .NET products and versions reported but nothing.

The only things related to Aes I have are:

System.Security.Cryptography.Aes
System.Security.Cryptography.AesCng
System.Security.Cryptography.AesCryptoServiceProvider
System.Security.Cryptography.AesManaged

The piece of code I need to use is the following:

Add-Type -Assembly System.Security  

$encrypted_value = ".. let's guess some encrypted value .."  
$cipherStream = [System.IO.MemoryStream]::new($encrypted_value)  
$cipherReader = [System.IO.BinaryReader]::new($cipherStream)  
$nonSecretPayload = $cipherReader.ReadBytes(3) # Magic number 3  
$nonce = $cipherReader.ReadBytes([System.Security.Cryptography.AesGcm]::NonceByteSizes.MinSize)  

I get the following error:

Unable to find type [System.Security.Cryptography.AesGcm].
At line:1 char:34

  • ... $cipherReader.ReadBytes([System.Security.Cryptography.AesGcm]::NonceB ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: (System.Security.Cryptography.AesGcm:TypeName) [], RuntimeException
  • FullyQualifiedErrorId : TypeNotFound

How can I solve? Which .NET Product and version I need to install? Should I change manually the assembly references PowerShell must refer to? If so, how can I do?

Thanks for help!

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2021-06-21T14:41:56.993+00:00

    Powershell 5.x is based upon .NET Framework. The functionality you are looking for only works with .NET Core/5 apps. Therefore you won't be able to use it in PS 5.x

    Install PS Core 7.x and you will then have access to the new functionality. If this isn't possible then you'll have to find an alternative implementation as the .NET Core assembly won't load in .NET Framework PS.

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-06-22T02:05:30.17+00:00

    Hi,

    Window PowerShell 5.1 is built on .NET Framework. As the AesGcm Class applies to .NET Core 3.0, 3.1 you could use PowerShell 7 that is built on .NET core 3.1.

    https://learn.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-70

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.