下面列出了java.security.interfaces.ECPrivateKey#getParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get the EC curve parameters used by the secp256k1 keys used for HD seeds
*/
public static ECParameterSpec getECHDSpec()
{
try
{
ECGenParameterSpec spec = new ECGenParameterSpec("secp256k1");
KeyPairGenerator key_gen = KeyPairGenerator.getInstance("ECDSA", Globals.getCryptoProviderName());
key_gen.initialize(spec);
KeyPair pair = key_gen.genKeyPair();
ECPrivateKey priv = (ECPrivateKey)pair.getPrivate();
return priv.getParams();
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
public PublicKey getPublicKey()
throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchProviderException {
ECPrivateKey privateKey = (ECPrivateKey) getPrivateKey();
ECParameterSpec params = privateKey.getParams();
org.bouncycastle.jce.spec.ECParameterSpec bcSpec =
org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec(params, false);
org.bouncycastle.math.ec.ECPoint q = bcSpec.getG().multiply(privateKey.getS());
org.bouncycastle.math.ec.ECPoint bcW = bcSpec.getCurve().decodePoint(q.getEncoded(false));
ECPoint w =
new ECPoint(
bcW.getAffineXCoord().toBigInteger(), bcW.getAffineYCoord().toBigInteger());
ECPublicKeySpec keySpec = new ECPublicKeySpec(w, tryFindNamedCurveSpec(params));
return (PublicKey)
KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME)
.generatePublic(keySpec);
}
public PublicKey getPublicKey()
throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException {
ECPrivateKey privateKey = (ECPrivateKey) getPrivateKey();
ECParameterSpec params = privateKey.getParams();
org.bouncycastle.jce.spec.ECParameterSpec bcSpec =
org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertSpec(params, false);
org.bouncycastle.math.ec.ECPoint q = bcSpec.getG().multiply(privateKey.getS());
org.bouncycastle.math.ec.ECPoint bcW = bcSpec.getCurve().decodePoint(q.getEncoded(false));
ECPoint w =
new ECPoint(
bcW.getAffineXCoord().toBigInteger(), bcW.getAffineYCoord().toBigInteger());
ECPublicKeySpec keySpec = new ECPublicKeySpec(w, tryFindNamedCurveSpec(params));
return (PublicKey)
KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME)
.generatePublic(keySpec);
}
/**
*
*/
private void export() throws NoSuchAlgorithmException, IOException {
ProviderECLibrary lib = cfg.selected;
KeyPairGeneratorIdent ident = null;
String algo = cli.getOptionValue("export.type", "EC");
for (KeyPairGeneratorIdent kpIdent : lib.getKPGs()) {
if (kpIdent.contains(algo)) {
ident = kpIdent;
break;
}
}
if (ident == null) {
throw new NoSuchAlgorithmException(algo);
}
KeyPairGenerator kpg = ident.getInstance(lib.getProvider());
if (cli.hasOption("export.bits")) {
int bits = Integer.parseInt(cli.getOptionValue("export.bits"));
kpg.initialize(bits);
}
KeyPair kp = kpg.genKeyPair();
ECPrivateKey privateKey = (ECPrivateKey) kp.getPrivate();
ECParameterSpec params = privateKey.getParams();
System.out.println(params);
EC_Curve curve = EC_Curve.fromSpec(params);
curve.writeCSV(System.out);
}
@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
if (!(privateKey instanceof ECPrivateKey)) {
throw new InvalidKeyException
("Key must be an instance of ECPrivateKey");
}
signKey = (ECPrivateKey) privateKey;
params = signKey.getParams();
buffer.reset();
}
@Override
protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException {
if (!(key instanceof ECPrivateKey)) {
throw new InvalidKeyException
("Key must be instance of ECPrivateKey");
}
privateKey = (ECPrivateKey) key;
this.params = privateKey.getParams();
}
public JCEECPrivateKey(
ECPrivateKey key)
{
this.d = key.getS();
this.algorithm = key.getAlgorithm();
this.ecSpec = key.getParams();
}
public BCDSTU4145PrivateKey(
ECPrivateKey key)
{
this.d = key.getS();
this.algorithm = key.getAlgorithm();
this.ecSpec = key.getParams();
}
public BCECPrivateKey(
ECPrivateKey key,
ProviderConfiguration configuration)
{
this.d = key.getS();
this.algorithm = key.getAlgorithm();
this.ecSpec = key.getParams();
this.configuration = configuration;
}
public BCECGOST3410PrivateKey(
ECPrivateKey key)
{
this.d = key.getS();
this.algorithm = key.getAlgorithm();
this.ecSpec = key.getParams();
}
public JCEECPrivateKey(
ECPrivateKey key)
{
this.d = key.getS();
this.algorithm = key.getAlgorithm();
this.ecSpec = key.getParams();
}
public BCDSTU4145PrivateKey(
ECPrivateKey key)
{
this.d = key.getS();
this.algorithm = key.getAlgorithm();
this.ecSpec = key.getParams();
}
public BCECPrivateKey(
ECPrivateKey key,
ProviderConfiguration configuration)
{
this.d = key.getS();
this.algorithm = key.getAlgorithm();
this.ecSpec = key.getParams();
this.configuration = configuration;
}
public BCECGOST3410PrivateKey(
ECPrivateKey key)
{
this.d = key.getS();
this.algorithm = key.getAlgorithm();
this.ecSpec = key.getParams();
}