You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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版本

    1. 重启PowerShell,避免旧版本程序集被锁定无法卸载。
    2. 下载1.8.9.0版本的BouncyCastle.Crypto.dll(可通过NuGet官方仓库获取对应版本)。
    3. 在调用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 08:28:34