2015-11-04 90 views

回答

0

看看下面的代碼,它是c#但在java中它是類似的。在這個例子中私鑰是由base64編碼的字符串給出的,並且還給出了base64編碼的字符串。註釋的keyParameters正在工作,所以如果你想擁有關鍵和曲線,請使用這一個。

private static readonly Org.BouncyCastle.Asn1.X9.X9ECParameters curve = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256r1"); 
private static readonly Org.BouncyCastle.Crypto.Parameters.ECDomainParameters domain = new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H); 
public string GetPublicKey(string privKey) 
{ 
     Org.BouncyCastle.Math.BigInteger d = new Org.BouncyCastle.Math.BigInteger(Convert.FromBase64String(privKey)); 
     //var privKeyParameters = new Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters(d, domain); 
     Org.BouncyCastle.Math.EC.ECPoint q = domain.G.Multiply(d); 
     //var pubKeyParameters = new Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters(q, domain); 
     return Convert.ToBase64String(q.GetEncoded()); 
}