public JCEECPrivateKey( String algorithm, org.bouncycastle.jce.spec.ECPrivateKeySpec spec) { this.algorithm = algorithm; this.d = spec.getD(); if (spec.getParams() != null) // can be null if implicitlyCA { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve; ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { this.ecSpec = null; } }
public BCDSTU4145PrivateKey( org.bouncycastle.jce.spec.ECPrivateKeySpec spec) { this.d = spec.getD(); if (spec.getParams() != null) // can be null if implicitlyCA { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve; ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { this.ecSpec = null; } }
public BCECPrivateKey( String algorithm, org.bouncycastle.jce.spec.ECPrivateKeySpec spec, ProviderConfiguration configuration) { this.algorithm = algorithm; this.d = spec.getD(); if (spec.getParams() != null) // can be null if implicitlyCA { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve; ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { this.ecSpec = null; } this.configuration = configuration; }
public BCECGOST3410PrivateKey( org.bouncycastle.jce.spec.ECPrivateKeySpec spec) { this.d = spec.getD(); if (spec.getParams() != null) // can be null if implicitlyCA { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve; ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { this.ecSpec = null; } }
/** * Returns the ECPrivateKey instance from its raw bytes. Note that you must provide the "s" value of the * private key, not e.g. the byte array from reading a PKCS#8 key file. * * @param privateKeyBytes The byte array (the "s" value) of the private key * @return The ECPrivateKey instance */ public static ECPrivateKey getPrivateKey(byte[] privateKeyBytes) { try { AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC"); parameters.init(new ECGenParameterSpec("secp256r1")); ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class); ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(new BigInteger(privateKeyBytes), ecParameterSpec); ECPrivateKey privateKey = (ECPrivateKey) KeyFactory.getInstance("EC").generatePrivate(ecPrivateKeySpec); return privateKey; } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException e) { getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get private key from raw bytes", e); return null; } }
private static PrivateKey getECPrivateKey(String curve) throws Exception { String s; ECParameterSpec params; switch (curve) { case "P256": s = EC_P256_S; params = EC_P256_PARAMS; break; case "P384": s = EC_P384_S; params = EC_P384_PARAMS; break; case "P521": s = EC_P521_S; params = EC_P521_PARAMS; break; default: throw new Exception("Unsupported curve: " + curve); } KeyFactory kf = KeyFactory.getInstance("EC"); KeySpec kspec = new ECPrivateKeySpec(new BigInteger(s), params); return kf.generatePrivate(kspec); }
private static synchronized void initChannelIdKey() throws Exception { if (CHANNEL_ID_PRIVATE_KEY != null) { return; } // NIST P-256 aka SECG secp256r1 aka X9.62 prime256v1 OpenSSLECGroupContext openSslSpec = OpenSSLECGroupContext.getCurveByName("prime256v1"); BigInteger s = new BigInteger( "229cdbbf489aea584828a261a23f9ff8b0f66f7ccac98bf2096ab3aee41497c5", 16); CHANNEL_ID_PRIVATE_KEY = new OpenSSLECPrivateKey(new ECPrivateKeySpec(s, openSslSpec.getECParameterSpec())) .getOpenSSLKey(); // Channel ID is the concatenation of the X and Y coordinates of the public key. CHANNEL_ID = new BigInteger( "702b07871fd7955c320b26f15e244e47eed60272124c92b9ebecf0b42f90069b" + "ab53592ebfeb4f167dbf3ce61513afb0e354c479b1c1b69874fa471293494f77", 16).toByteArray(); }
private static ECPrivateKey toJcaPrivateKey(org.bouncycastle.asn1.sec.ECPrivateKey ecPrivateKey) throws GeneralSecurityException { String curveName = null; ASN1ObjectIdentifier curveId = (ASN1ObjectIdentifier) ecPrivateKey.getParameters(); if (curveId.equals(secp224r1_OID)) { curveName = "secp224r1"; } else if (curveId.equals(prime256v1_OID)) { curveName = "prime256v1"; } else if (curveId.equals(secp384r1_OID)) { curveName = "secp384r1"; } else if (curveId.equals(secp521r1_OID)) { curveName = "secp521r1"; } else { throw new IllegalStateException("Unknown curve OID: " + curveId); } ECNamedCurveParameterSpec sp = ECNamedCurveTable.getParameterSpec(curveName); ECParameterSpec params = new ECNamedCurveSpec(sp.getName(), sp.getCurve(), sp.getG(), sp.getN(), sp.getH()); ECPrivateKeySpec pkSpec = new ECPrivateKeySpec(ecPrivateKey.getKey(), params); KeyFactory kf = KeyFactory.getInstance("EC"); ECPrivateKey privateKey = (ECPrivateKey) kf.generatePrivate(pkSpec); return privateKey; }
public JCEECPrivateKey( String algorithm, org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec spec) { this.algorithm = algorithm; this.d = spec.getD(); if (spec.getParams() != null) // can be null if implicitlyCA { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve; ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { this.ecSpec = null; } }
public BCDSTU4145PrivateKey( org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec spec) { this.d = spec.getD(); if (spec.getParams() != null) // can be null if implicitlyCA { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve; ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { this.ecSpec = null; } }
public BCECPrivateKey( String algorithm, org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec spec, ProviderConfiguration configuration) { this.algorithm = algorithm; this.d = spec.getD(); if (spec.getParams() != null) // can be null if implicitlyCA { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve; ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { this.ecSpec = null; } this.configuration = configuration; }
public BCECGOST3410PrivateKey( org.ripple.bouncycastle.jce.spec.ECPrivateKeySpec spec) { this.d = spec.getD(); if (spec.getParams() != null) // can be null if implicitlyCA { ECCurve curve = spec.getParams().getCurve(); EllipticCurve ellipticCurve; ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed()); this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams()); } else { this.ecSpec = null; } }
/** * Test for <code>getParams()</code> method<br> * Assertion: returns associated EC parameters<br> * Test preconditions: <code>ECPrivateKeySpec</code> instance * created using valid parameters<br> * Expected: must return params value which is equal * to the one passed to the constructor; (both must refer * the same object) */ public final void testGetParams() { // Valid (see note below) parameters set EllipticCurve c = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); ECParameterSpec params = new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10); ECPrivateKeySpec ks = new ECPrivateKeySpec(BigInteger.ZERO, params); ECParameterSpec paramsRet = ks.getParams(); assertEquals(params, paramsRet); assertSame(params, paramsRet); }
/** * Test for <code>getS()</code> method<br> * Assertion: returns associated private value<br> * Test preconditions: <code>ECPrivateKeySpec</code> instance * created using valid parameters<br> * Expected: must return s value which is equal * to the one passed to the constructor; (both must refer * the same object) */ public final void testGetS() { // Valid (see note below) parameters set EllipticCurve c = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)), BigInteger.ZERO, BigInteger.valueOf(4L)); ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L)); ECParameterSpec params = new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10); BigInteger s = BigInteger.valueOf(5L); ECPrivateKeySpec ks = new ECPrivateKeySpec(s, params); BigInteger sRet = ks.getS(); assertEquals(s, sRet); assertSame(s, sRet); }
/** * 解密<br> * 用私钥解密 * * @param data * @param key * @return * @throws Exception */ public static byte[] decrypt(byte[] data, String key) throws Exception { // 对密钥解密 byte[] keyBytes = decryptBASE64(key); // 取得私钥 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = ECKeyFactory.INSTANCE; ECPrivateKey priKey = (ECPrivateKey) keyFactory .generatePrivate(pkcs8KeySpec); ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(priKey.getS(), priKey.getParams()); // 对数据解密 // TODO Chipher不支持EC算法 未能实现 Cipher cipher = new NullCipher(); // Cipher.getInstance(ALGORITHM, keyFactory.getProvider()); cipher.init(Cipher.DECRYPT_MODE, priKey, ecPrivateKeySpec.getParams()); return cipher.doFinal(data); }