我实施"密钥对生成"使用secp192r1曲线。但是私钥没有像公钥那样以字符串形式显示。
这是我的代码:
package lam.bk;
import java.security.*;
import java.security.spec.*;
public class ECCKeyGeneration {
public static void main(String[] args) throws Exception {
KeyPairGenerator kpg;
kpg = KeyPairGenerator.getInstance("EC","SunEC");
ECGenParameterSpec ecsp;
ecsp = new ECGenParameterSpec("secp192r1");
kpg.initialize(ecsp);
KeyPair kp = kpg.genKeyPair();
PrivateKey privKey = kp.getPrivate();
PublicKey pubKey = kp.getPublic();
System.out.println(pubKey.toString());
System.out.println(privKey.toString());
}
}
答案 0 :(得分:0)
在获得openssl字符串表示之前,您需要将私钥转换为PEM格式。在java中,您可以通过使用BouncyCastle库PEMWriter
以编程方式执行此操作答案 1 :(得分:0)
下面的代码将为secp192r1曲线输出24字节私钥:
private String getPrivateKeyAsHex(PrivateKey privateKey) {
ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
byte[] privateKeyBytes = new byte[24];
writeToStream(privateKeyBytes, 0, ecPrivateKey.getS(), 24);
return Hex.toHexString(privateKeyBytes);
}
private void writeToStream(byte[] stream, int start, BigInteger value, int size) {
byte[] data = value.toByteArray();
int length = Math.min(size, data.length);
int writeStart = start + size - length;
int readStart = data.length - length;
System.arraycopy(data, readStart, stream, writeStart, length);
}