使用 jar 文件中的代码时出现问题

发布于 2024-11-01 07:35:09 字数 2097 浏览 0 评论 0原文

我运行以下代码:

public class Sign {

private static final PrivateKey         priv    = Util.loadPrivate();
private static final PublicKey          pub     = Util.loadPublic();
private static final HexBinaryAdapter   adp     = new HexBinaryAdapter();

public static String sign(String in) {
    try {
        Signature sign = Signature.getInstance(Util.ALG);
        sign.initSign(priv);
        sign.update(in.getBytes());
        return adp.marshal(sign.sign());

    } catch (Exception e) {e.printStackTrace();}
    return null;
}

public static boolean verify(String data, String sign) {
    try {
        Signature verify = Signature.getInstance(Util.ALG);
        verify.initVerify(pub);
        verify.update(data.getBytes());
        return verify.verify(adp.unmarshal(sign));
    } catch (Exception e) {e.printStackTrace();}
    return false;
}

}

并且 main 函数如下所示:

    public static void main(String[] args) {
    String in = "lala";
    String sign = Sign.sign(in);
    System.out.println(sign);
    System.out.println(Sign.verify(in, sign));
}

当我从 Eclipse 中运行它时一切顺利(输出为“true”),但是当我将其打包到 jar 中(没有 main 函数)并运行时那么输出就是 false。

这就是我加载密钥的方式:

    public static PrivateKey loadPrivate() {
    try {
        URLConnection con = Util.class.getResource("private.key").openConnection();
        byte[] bs = new byte[con.getContentLength()];
        con.getInputStream().read(bs);
        return KeyFactory.getInstance(ALG).generatePrivate(new PKCS8EncodedKeySpec(bs));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public static PublicKey loadPublic() {
    try {
        URLConnection con = Util.class.getResource("public.key").openConnection();
        byte[] bs = new byte[con.getContentLength()];
        con.getInputStream().read(bs);
        return KeyFactory.getInstance(ALG).generatePublic(new X509EncodedKeySpec(bs));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

我检查并加载密钥工作正常。

有什么想法吗?

I run the following code:

public class Sign {

private static final PrivateKey         priv    = Util.loadPrivate();
private static final PublicKey          pub     = Util.loadPublic();
private static final HexBinaryAdapter   adp     = new HexBinaryAdapter();

public static String sign(String in) {
    try {
        Signature sign = Signature.getInstance(Util.ALG);
        sign.initSign(priv);
        sign.update(in.getBytes());
        return adp.marshal(sign.sign());

    } catch (Exception e) {e.printStackTrace();}
    return null;
}

public static boolean verify(String data, String sign) {
    try {
        Signature verify = Signature.getInstance(Util.ALG);
        verify.initVerify(pub);
        verify.update(data.getBytes());
        return verify.verify(adp.unmarshal(sign));
    } catch (Exception e) {e.printStackTrace();}
    return false;
}

}

and the main function looks like this:

    public static void main(String[] args) {
    String in = "lala";
    String sign = Sign.sign(in);
    System.out.println(sign);
    System.out.println(Sign.verify(in, sign));
}

Everything goes well when I run it from within Eclipse (the output is "true"), but when I pack it into a jar (without the main function) and run it then the output is false.

This is how I load the keys:

    public static PrivateKey loadPrivate() {
    try {
        URLConnection con = Util.class.getResource("private.key").openConnection();
        byte[] bs = new byte[con.getContentLength()];
        con.getInputStream().read(bs);
        return KeyFactory.getInstance(ALG).generatePrivate(new PKCS8EncodedKeySpec(bs));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public static PublicKey loadPublic() {
    try {
        URLConnection con = Util.class.getResource("public.key").openConnection();
        byte[] bs = new byte[con.getContentLength()];
        con.getInputStream().read(bs);
        return KeyFactory.getInstance(ALG).generatePublic(new X509EncodedKeySpec(bs));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

I checked and loading the keys works fine.

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2024-11-08 07:35:09

就像这样运行:

java Main -classpath=/path/to/libraryk.jar

Just run like this:

java Main -classpath=/path/to/libraryk.jar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文