I am trying to decrypt a file in PowerShell "7.x". I am explicitly using the Unprotect-PGP
cmdlet, but the error message I am getting refers to the Protect-PGP
cmdlet. Only thing I can come up with is perhaps a BouncyCastle.Crypto
Version Conflict but am really not sure...
Would really appreciate any help or brilliant ideas (even semi-brilliant will do)!!
Unprotect-PGP -FilePathPrivate "G:\TEMP\secretkey.asc" -Password "XXXXXXXXX" -FolderPath "G:\TEMP\ABM\PROD_Test.csv.pgp" -OutputFolderPath "G:\TEMP\"
WARNING: Protect-PGP - Can't encrypt files because: Exception calling ".ctor" with "2" argument(s): "Could not load file or assembly 'BouncyCastle.Crypto, Version=1.8.9.0, Culture=neutral, PublicKeyToken=0e99375e54769942'. Could not find or load a specific file. (0x80131621)"
PS C:\> # List Bouncy Castle version currently loaded
>> $assemblies = [System.AppDomain]::CurrentDomain.GetAssemblies()
>> $bouncyCastle = $assemblies | Where-Object { $_.ManifestModule.Name -eq "BouncyCastle.Crypto.dll" }
>> $bouncyCastleVersion = $bouncyCastle.GetName().Version
>> $bouncyCastleVersion
Major Minor Build Revision
----- ----- ----- --------
1 8 8 0
Most likely you have another Module using an older version of that assembly causing this dependency issue. You can try restarting PowerShell, if you can, using the -NoProfile
switch: pwsh -NoProfile
and run the cmdlet again and see if it keeps failing. If it doesn't fail then you can assume there is some other Module using an older version of the same assembly causing this problem, possible solutions are to upgrade said Module assuming it is indeed using the newer version of the assembly.
Leaving that aside, you seem to have a typo in your use of input and output file, you're using -FolderPath
and -OutputFolderPath
when the parameters you should be using to decrypt a file are -FilePath
and -OutFilePath
.
Alternatively something that might work assuming the assembly didn't change in its usage is to use the BouncyCastle.Crypto
assembly you already have loaded directly instead of the Unprotect-PGP
, which for reference, in its latest version is using the version 2.0.0
of BouncyCastle.Cryptography
:
try {
$key = [PgpCore.EncryptionKeys]::new(
'G:\TEMP\secretkey.asc',
'thePrivateKeyPasswordGoesHere')
$pgp = [PgpCore.PGP]::new($key)
$pgp.DecryptFile(
'G:\TEMP\ABM\PROD_Test.csv.pgp',
# change destination file path accordingly here:
'G:\TEMP\PROD_Test-decrypted.csv')
}
finally {
${pgp}?.Dispose()
}
Thanks to OP's feedback we could determine that the module causing conflicts in this case was the Az Module using an older version of the assembly: