如何将 bouncycastle 实现到一个罐子里?

How to implement bouncycastle into one-jar?

我在项目中使用 SSHJ 库。 SSHJ 库使用了 bouncycastle 加密。

在 eclipse 中一切都很好,但是在我使用一个 jar 将所有内容打包在一个 jar 包中之后,我遇到了 bouncycastle 库的问题。 bcprov-jdk15on-1.51.jar 包含在 /lib 中的 JAR 包中,所有其他库以及例如。 sshj.jar.

在日志中我有这些:

Security Provider class 'org.bouncycastle.jce.provider.BouncyCastleProvider' not found

还有这个

WARNING: Disabling high-strength ciphers: cipher strengths apparently limited by JCE policy

SFTP 连接的功能是:

net.schmizz.sshj.transport.TransportException: Unable to reach a settlement: [diffie-hellman-group1-sha1, diffie-hellman-group-exchange-sha1] and [diffie-hellman-group-exchange-sha256]

我尝试过的:

这就是所有工作的方式"as expected":

那么,显然 BC 库存在一些类路径问题?我在某处读到一些关于已签名的安全提供程序库在实施时遇到问题的内容,但不太了解那个......也许这也是这里的原因?

知道这个问题是如何解决的吗?感谢任何有关此问题的帮助,谢谢!

编辑: 我的 build.xml 已实施建议的代码签名:

    <target name="package_x" depends="package_y">
    <!-- Create manifest file for x -->
    <delete file="MANIFEST.MF"/>
    <manifest file="MANIFEST.MF">
        <attribute name="Main-Class" value="com.simontuffs.onejar.Boot"/>
        <attribute name="One-Jar-Main-Class" value="com.some.main.class.name"/>
        <attribute name="Class-Path" value="some_other_libs lib/bcprov-jdk15on.jar ." />
    </manifest>

    <!-- Copy properties file -->
    <copy todir="${module.dist.dir}">
        <fileset dir="${module.x.build.dir}/classes">
            <include name="**/*.properties"/>
        </fileset>
    </copy>

        <signjar destDir="${basedir}/distribute/lib/" 
                alias="server" keystore="${module.x.src.dir}/keystore/myCSC.jks"
                storepass="pass"
                preservelastmodified="true">
            <path>
                <fileset dir="${basedir}/distribute/lib/" includes="bcprov-jdk15on.jar" />
            </path>
            <flattenmapper />
        </signjar>

    <!-- Construct the One-JAR file -->
    <echo message="Creating a ONE-jar package of the x files..." />

    <one-jar destfile="${module.dist.dir}/${module.x.package}" manifest="MANIFEST.MF">
        <main>
            <fileset dir="${module.x.build.dir}/classes/">
                <exclude name="x-config.properties"/>
            </fileset>
        </main>

        <lib>               
            <fileset dir="${basedir}/distribute/lib/" />
            <fileset dir="${module.common.dist.dir}" />

        </lib>
    </one-jar>

        <signjar destDir="${module.dist.dir}" 
                alias="server" keystore="${module.agent.src.dir}/keystore/myCSC.jks"
                storepass="pass"
                preservelastmodified="true">
            <path>
                <fileset dir="${module.dist.dir}" includes="**/*.jar" />
            </path>
            <flattenmapper />
        </signjar>

</target>

需要对 implement a cryptographic provider jar 进行签名

If your provider is supplying encryption algorithms through the Cipher KeyAgreement, KeyGenerator, Mac, or SecretKeyFactory classes, you will need to sign your JAR file so that the JCA can authenticate the code at runtime.

Bouncycastle 罐子已签名。你把所有 类 重新打包到一个 jar 里,但是你没有说你已经签名了,所以我猜你没有做。 SSHJ 可能正在使用某种加密算法,无法初始化 bouncycastle

选项:

  • 使用代码签名证书签署您的代码

  • 也使用您的应用部署 bcprov-jdk15on-1。51.jar

回答我自己的问题:

两种解决方案:

  1. 将库添加到 Java 的 lib/ext
    • 这对我来说确实不是解决方案,但可能对其他人有用。
  2. 使用 JDotSoft JarClassLoader
    • 使用简单,似乎支持添加 JCE 提供程序,例如 Bouncycastle。
    • 通过 Ant 构建将 BC-jar 添加到主 JAR 中:<zipfileset dir="/build/libsToInclude/" includes="*.jar" prefix="lib/"/> 类加载器完成剩下的工作。