PowerShell 7.x使用Unprotect-PGP遇BouncyCastle版本冲突求助
PowerShell 7.x Unprotect-PGP 报错关联 Protect-PGP 的版本冲突问题解决思路
问题场景
执行Unprotect-PGP解密文件时,报错信息却指向Protect-PGP,提示无法加载BouncyCastle.Crypto, Version=1.8.9.0,但当前PowerShell会话中加载的是1.8.8.0版本,核心问题为BouncyCastle程序集版本不匹配,且存在PGP模块内部调用逻辑的异常关联。
解决步骤
确认PGP模块依赖版本
找到你使用的PGP模块(如PSPGP)的安装目录,查看模块的.psd1配置文件,检查RequiredAssemblies字段指定的BouncyCastle版本;或者直接查看模块目录下的BouncyCastle.Crypto.dll属性,确认模块要求的具体版本。替换为匹配的BouncyCastle版本
- 重启PowerShell,避免旧版本程序集被锁定无法卸载。
- 下载1.8.9.0版本的
BouncyCastle.Crypto.dll(可通过NuGet官方仓库获取对应版本)。 - 在调用
Unprotect-PGP前手动加载正确版本:Add-Type -Path "G:\TEMP\BouncyCastle.Crypto.1.8.9.0\BouncyCastle.Crypto.dll" # 验证加载版本 $assemblies = [System.AppDomain]::CurrentDomain.GetAssemblies() $bouncyCastle = $assemblies | Where-Object { $_.ManifestModule.Name -eq "BouncyCastle.Crypto.dll" } $bouncyCastle.GetName().Version
修复模块程序集绑定
如果模块硬编码了1.8.9.0版本,但系统默认加载旧版本,可添加程序集绑定重定向:
在PGP模块的安装目录下创建[模块名称].dll.config文件,内容如下:<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="BouncyCastle.Crypto" publicKeyToken="0e99375e54769942" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.8.9.0" newVersion="1.8.9.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>更新或重装PGP模块
模块内部可能存在逻辑错误(比如Unprotect-PGP错误调用了Protect-PGP的加密逻辑),尝试更新到最新版本:Update-Module -Name PSPGP -Force # 替换为你实际使用的模块名若更新无效,卸载后重装:
Uninstall-Module -Name PSPGP -Force Install-Module -Name PSPGP -Force -AllowClobber
额外排查点
- 检查是否有其他PowerShell模块或脚本提前加载了旧版本的BouncyCastle,导致会话中版本冲突。
- 尝试在干净的PowerShell会话(无其他模块加载)中执行解密命令,排除环境干扰。
内容的提问来源于stack exchange,提问作者Karel ten Hoope
相关产品推荐
相关产品推荐

