下面列出了java.security.interfaces.ECPrivateKey#getS ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tests key generation for given parameters. The test can be skipped if the curve is not a
* standard curve.
*/
void testKeyGeneration(ECParameterSpec ecParams, boolean isStandard) throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
KeyPair keyPair;
try {
keyGen.initialize(ecParams);
keyPair = keyGen.generateKeyPair();
} catch (InvalidAlgorithmParameterException ex) {
if (!isStandard) {
return;
}
throw ex;
}
ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
EcUtil.checkPublicKey(pub);
BigInteger s = priv.getS();
// Check the length of s. Could fail with probability 2^{-32}.
assertTrue(s.bitLength() >= EcUtil.fieldSizeInBits(ecParams.getCurve()) - 32);
// TODO(bleichen): correct curve?
// TODO(bleichen): use RandomUtil
}
protected static byte[] decrypt(final byte[] ciphertext, final PrivateKey privateKey)
throws BadPaddingException, IllegalBlockSizeException {
if (privateKey instanceof ECPrivateKey) {
final ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
final BigInteger privateKeyInt = ecPrivateKey.getS();
return decrypt(ciphertext, privateKeyInt);
} else {
throw new IllegalArgumentException("Key type must be ECPublicKey!");
}
}
/** Extract the k that was used to sign the signature. */
BigInteger extractK(byte[] signature, BigInteger h, ECPrivateKey priv) throws Exception {
BigInteger x = priv.getS();
BigInteger n = priv.getParams().getOrder();
BigInteger r = extractR(signature);
BigInteger s = extractS(signature);
BigInteger k = x.multiply(r).add(h).multiply(s.modInverse(n)).mod(n);
return k;
}
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();
}
public static EcPrivateKeyDef from(ECPrivateKey key) {
return new EcPrivateKeyDef(EcDomainDef.from(key.getParams()), key.getS(), key);
}
static byte[] generateECBlob(Key k) {
int keyBitLength = KeyUtil.getKeySize(k);
int keyLen = (keyBitLength + 7) / 8;
boolean isPrivate = k instanceof ECPrivateKey;
byte[] keyBlob = new byte[8 + keyLen * (isPrivate ? 3 : 2)];
keyBlob[0] = 'E';
keyBlob[1] = 'C';
keyBlob[2] = 'S';
if (isPrivate) {
keyBlob[3] = (byte) (keyBitLength == 256 ? '2'
: (keyBitLength == 384 ? '4' : '6'));
} else {
keyBlob[3] = (byte) (keyBitLength == 256 ? '1'
: (keyBitLength == 384 ? '3' : '5'));
}
BigInteger x;
BigInteger y;
// Fill the array in reverse order (s -> y -> x -> len) in case
// one BigInteger encoding has an extra 0 at the beginning
if (isPrivate) {
// We can keep X and Y zero and it still works
ECPrivateKey prk = (ECPrivateKey)k;
BigInteger s = prk.getS();
byte[] bs = s.toByteArray();
System.arraycopy(
bs, 0,
keyBlob, 8 + keyLen + keyLen + keyLen - bs.length,
bs.length);
} else {
ECPublicKey puk = (ECPublicKey)k;
x = puk.getW().getAffineX();
y = puk.getW().getAffineY();
byte[] by = y.toByteArray();
System.arraycopy(by, 0, keyBlob, 8 + keyLen + keyLen - by.length,
by.length);
byte[] bx = x.toByteArray();
System.arraycopy(bx, 0, keyBlob, 8 + keyLen - bx.length, bx.length);
}
keyBlob[4] = (byte) keyLen;
keyBlob[5] = keyBlob[6] = keyBlob[7] = 0;
return keyBlob;
}
/**
* Checks if the private key is a valid key (according to requirement [V2G2-823]) for the received contract
* certificate before saving it to the keystore.
* @param privateKey The private key corresponding to the contract certificate
* @param contractCertChain The received contract certificate chain
* @return True, if the private key is a valid key, false otherwise.
*/
private static boolean isPrivateKeyValid(ECPrivateKey privateKey, CertificateChainType contractCertChain) {
AlgorithmParameters parameters;
try {
parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp256r1"));
ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);
// Now we need to check if the private key is correct (see requirement [V2G2-823])
BigInteger order = ecParameterSpec.getOrder();
ECPoint basePoint = ecParameterSpec.getGenerator();
BigInteger privateKeyValue = privateKey.getS();
X509Certificate contractCert = getCertificate(contractCertChain.getCertificate());
ECPublicKey publicKey = (ECPublicKey) contractCert.getPublicKey();
// 1. check
if (privateKeyValue.compareTo(order) != -1) {
getLogger().error("Validation of private key failed: its value is not strictly smaller than the "
+ "order of the base point");
return false;
}
// 2. check
/*
* TODO:
* No idea how to check for
* "multiplication of the base point with this value must generate a key matching the public key of
* the contract certificate"
* "this value" = value of private key
* -> some more expert knowledge on the arithmetic of elliptic curves is needed to tackle this!
*/
} catch (NoSuchAlgorithmException | InvalidParameterSpecException e) {
getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get private key from raw bytes", e);
return false;
}
return true;
}
static byte[] generateECBlob(Key k) {
int keyBitLength = KeyUtil.getKeySize(k);
int keyLen = (keyBitLength + 7) / 8;
boolean isPrivate = k instanceof ECPrivateKey;
byte[] keyBlob = new byte[8 + keyLen * (isPrivate ? 3 : 2)];
keyBlob[0] = 'E';
keyBlob[1] = 'C';
keyBlob[2] = 'S';
if (isPrivate) {
keyBlob[3] = (byte) (keyBitLength == 256 ? '2'
: (keyBitLength == 384 ? '4' : '6'));
} else {
keyBlob[3] = (byte) (keyBitLength == 256 ? '1'
: (keyBitLength == 384 ? '3' : '5'));
}
BigInteger x;
BigInteger y;
// Fill the array in reverse order (s -> y -> x -> len) in case
// one BigInteger encoding has an extra 0 at the beginning
if (isPrivate) {
// We can keep X and Y zero and it still works
ECPrivateKey prk = (ECPrivateKey)k;
BigInteger s = prk.getS();
byte[] bs = s.toByteArray();
System.arraycopy(
bs, 0,
keyBlob, 8 + keyLen + keyLen + keyLen - bs.length,
bs.length);
} else {
ECPublicKey puk = (ECPublicKey)k;
x = puk.getW().getAffineX();
y = puk.getW().getAffineY();
byte[] by = y.toByteArray();
System.arraycopy(by, 0, keyBlob, 8 + keyLen + keyLen - by.length,
by.length);
byte[] bx = x.toByteArray();
System.arraycopy(bx, 0, keyBlob, 8 + keyLen - bx.length, bx.length);
}
keyBlob[4] = (byte) keyLen;
keyBlob[5] = keyBlob[6] = keyBlob[7] = 0;
return keyBlob;
}