我正在尝试使用 javaDeflaterOutputStream和InflaterOutputStream类来压缩字节数组,但两者似乎都无法正常工作。我认为我错误地实施了它们。
public static byte[] compress(byte[] in) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DeflaterOutputStream defl = new DeflaterOutputStream(out);
defl.write(in);
defl.flush();
defl.close();
return out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
System.exit(150);
return null;
}
}
public static byte[] decompress(byte[] in) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InflaterOutputStream infl = new InflaterOutputStream(out);
infl.write(in);
infl.flush();
infl.close();
return out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
System.exit(150);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我用来压缩和解压缩字节数组的两种方法。我在网上看到的大多数实现都使用固定大小的缓冲区数组来进行解压缩部分,但如果可能的话,我希望避免这种情况,因为如果我想要任何缓冲区数组,我需要使该缓冲区数组的大小为 1显着压缩。
如果有人可以向我解释我做错了什么,我将不胜感激。另外,为了解释为什么我知道这些方法无法正常工作:它输出的“压缩”字节数组始终大于未压缩的字节数组,无论我尝试提供什么大小的字节数组。
我试图从私钥生成公共 ECDSA 密钥,但我没有在互联网上找到太多关于如何执行此操作的帮助。几乎所有东西都是为了从公钥规范生成公钥,我不知道如何获得它。到目前为止,这是我整理的内容:
public void setPublic() throws GeneralSecurityException {
ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1");
KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC");
ECCurve curve = params.getCurve();
java.security.spec.EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, params.getSeed());
java.security.spec.ECPoint point = ECPointUtil.decodePoint(ellipticCurve, this.privateKey.getEncoded());
java.security.spec.ECParameterSpec params2=EC5Util.convertSpec(ellipticCurve, params);
java.security.spec.ECPublicKeySpec keySpec = new java.security.spec.ECPublicKeySpec(point,params2);
this.publicKey = fact.generatePublic(keySpec);
}
Run Code Online (Sandbox Code Playgroud)
但是,在运行时,我收到以下错误:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid point encoding 0x30
at org.bouncycastle.math.ec.ECCurve.decodePoint(Unknown Source)
at org.bouncycastle.jce.ECPointUtil.decodePoint(Unknown Source)
at Wallet.Wallet.setPublic(Wallet.java:125)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?有没有更好/更简单的方法来做到这一点?
编辑:我已经设法编译了一些代码,但它不能正常工作:
public void setPublic() throws GeneralSecurityException {
BigInteger privKey = new BigInteger(getHex(privateKey.getEncoded()),16);
X9ECParameters ecp = SECNamedCurves.getByName("secp256k1");
ECPoint …Run Code Online (Sandbox Code Playgroud) Java的垃圾收集究竟如何处理线程?
为了澄清,我有一个点对点网络,我用Java编写,如果一个对等体被确定为恶意行为或被路由表拒绝,我删除对Peer对象的所有引用,这扩展了一个线程.
我是否正确地假设即使我删除了对该实例的所有引用,该实例的线程,因此该实例也不会被垃圾收集器删除?