public static ECKeyPair create(KeyPair keyPair) { BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); BigInteger privateKeyValue = privateKey.getD(); // Ethereum does not use encoded public keys like bitcoin - see // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details // Additionally, as the first bit is a constant prefix (0x04) we ignore this value byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); return new ECKeyPair(privateKeyValue, publicKeyValue); }
/** * 只获取私钥里的d,32字节 * * @param privateKey * @return */ public static byte[] getRawPrivateKey(BCECPrivateKey privateKey) { return fixToCurveLengthBytes(privateKey.getD().toByteArray()); }
/** * Returns a 32 byte array containing the private key, or null if the key is encrypted or public only * * @return - */ @Nullable public byte[] getPrivKeyBytes() { if (privKey == null) { return null; } else if (privKey instanceof BCECPrivateKey) { return bigIntegerToBytes(((BCECPrivateKey) privKey).getD(), 32); } else { return null; } }
public byte[] serializePrivateKey() throws Exception { switch (this.keyType) { case ECDSA: case SM2: BCECPrivateKey pri = (BCECPrivateKey) this.privateKey; String curveName = Curve.valueOf(pri.getParameters().getCurve()).toString(); byte[] d = new byte[32]; if (pri.getD().toByteArray().length == 33) { System.arraycopy(pri.getD().toByteArray(), 1, d, 0, 32); } else if (pri.getD().toByteArray().length == 31){ d[0] = 0; System.arraycopy(pri.getD().toByteArray(), 0, d, 1, 31); } else { return pri.getD().toByteArray(); } return d; default: // should not reach here throw new Exception(ErrorCode.UnknownKeyType); } }
public byte[] serializePrivateKey() throws Exception { switch (this.keyType) { case ECDSA: case SM2: BCECPrivateKey pri = (BCECPrivateKey) this.privateKey; String curveName = Curve.valueOf(pri.getParameters().getCurve()).toString(); byte[] d = new byte[32]; if (pri.getD().toByteArray().length == 33) { System.arraycopy(pri.getD().toByteArray(), 1, d, 0, 32); } else if (pri.getD().toByteArray().length == 31){ d[0] = 0; System.arraycopy(pri.getD().toByteArray(), 0, d, 1, 31); } else { return pri.getD().toByteArray(); } return d; default: // should not reach here throw new Exception(ErrorCode.UnknownKeyType); } }
public static ECKeyPair create(KeyPair keyPair) { BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); BigInteger privateKeyValue = privateKey.getD(); // Ethereum does not use encoded public keys like bitcoin - see // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details // Additionally, as the first bit is a constant prefix (0x04) we ignore this value byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); return new ECKeyPair(privateKeyValue, publicKeyValue); }
/** * Produce a string rendering of the ECKey INCLUDING the private key. * Unless you absolutely need the private key it is better for security reasons to just use toString(). * * * @return - */ public String toStringWithPrivate() { StringBuilder b = new StringBuilder(); b.append(toString()); if (privKey != null && privKey instanceof BCECPrivateKey) { b.append(" priv:").append(Hex.toHexString(((BCECPrivateKey) privKey).getD().toByteArray())); } return b.toString(); }
public static ECKeyPair create(KeyPair keyPair) { BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); BigInteger privateKeyValue = privateKey.getD(); // Ethereum does not use encoded public keys like bitcoin - see // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details // Additionally, as the first bit is a constant prefix (0x04) we ignore this value byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); return new ECKeyPair(privateKeyValue, publicKeyValue); }
public static ECKeyPair create(KeyPair keyPair) { BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); BigInteger privateKeyValue = privateKey.getD(); // Moac does not use encoded public keys like bitcoin - see // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details // Additionally, as the first bit is a constant prefix (0x04) we ignore this value byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); return new ECKeyPair(privateKeyValue, publicKeyValue); }
public static ECKeyPair create(KeyPair keyPair) { BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); BigInteger privateKeyValue = privateKey.getD(); // Ethereum does not use encoded public keys like bitcoin - see // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details // Additionally, as the first bit is a constant prefix (0x04) we ignore this value byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); return new ECKeyPair(privateKeyValue, publicKeyValue); }
public static ECKeyPair create(KeyPair keyPair) { BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); BigInteger privateKeyValue = privateKey.getD(); // Ethereum does not use encoded public keys like bitcoin - see // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details // Additionally, as the first bit is a constant prefix (0x04) we ignore this value byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length)); return new ECKeyPair(privateKeyValue, publicKeyValue); }
public int hashCode() { return getD().hashCode() ^ engineGetSpec().hashCode(); }
public static ECKeyPair create(KeyPair keyPair) { BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); BigInteger privateKeyValue = privateKey.getD(); byte[] publicKeyBytes = publicKey.getQ().getEncoded(true); BigInteger publicKeyValue = new BigInteger(1, publicKeyBytes); return new ECKeyPair(privateKeyValue, publicKeyValue); }
/** * Gets the private key in the form of an integer field element. The public key is derived by performing EC * point addition this number of times (i.e. point multiplying). * * * @return - * * @throws java.lang.IllegalStateException if the private key bytes are not available. */ public BigInteger getPrivKey() { if (privKey == null) { throw new MissingPrivateKeyException(); } else if (privKey instanceof BCECPrivateKey) { return ((BCECPrivateKey) privKey).getD(); } else { throw new MissingPrivateKeyException(); } }
public static PublicKey extractPublicKey(PrivateKey privateKey) { String algorithm = privateKey.getAlgorithm(); try { if (algorithm.equals(RSA.getAlgorithmName())) { KeyFactory keyFactory = KeyFactory.getInstance(RSA.getAlgorithmName(), BouncyCastleProviderHolder.getInstance()); RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) privateKey; RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent()); return keyFactory.generatePublic(keySpec); } else if (algorithm.equals(EC.getAlgorithmName())) { KeyFactory keyFactory = KeyFactory.getInstance(EC.getAlgorithmName(), BouncyCastleProviderHolder.getInstance()); BCECPrivateKey ecPrivateKey = (BCECPrivateKey) privateKey; ECParameterSpec ecParameterSpec = ecPrivateKey.getParameters(); ECPoint ecPoint = new FixedPointCombMultiplier().multiply(ecParameterSpec.getG(), ecPrivateKey.getD()); ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPoint, ecParameterSpec); return keyFactory.generatePublic(keySpec); } else { throw new IllegalArgumentException("Unexpected key algorithm: " + algorithm); } } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
public static byte[] Decrypt(byte[] prikey, byte[] IV, byte[] key_cxt, byte[] ciphertext, int keylen) { try { com.github.ontio.account.Account account = new com.github.ontio.account.Account(prikey, signatureScheme); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) curveParaSpec[0]); ECDomainParameters ecDomain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); AsymmetricCipherKeyPair keys = new AsymmetricCipherKeyPair( null, new ECPrivateKeyParameters(((BCECPrivateKey) account.getPrivateKey()).getD(), ecDomain)); byte[] out = key_cxt; ECIESKeyEncapsulation kem = new ECIESKeyEncapsulation(new KDF2BytesGenerator(defaultDigest), new SecureRandom()); KeyParameter key1; kem.init(keys.getPrivate()); key1 = (KeyParameter) kem.decrypt(out, keylen); byte[] plaintext; try { Cipher dec = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); Key key = new SecretKeySpec(key1.getKey(), "AES"); dec.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV)); plaintext = dec.doFinal(ciphertext); } catch (Exception e) { throw new Exception("AES failed initialisation - " + e.toString(), e); } return plaintext; } catch (Exception e) { e.printStackTrace(); } return null; }
public static byte[] Decrypt(byte[] prikey, byte[] IV, byte[] key_cxt, byte[] ciphertext, int keylen) { try { com.github.ontio.account.Account account = new com.github.ontio.account.Account(prikey, signatureScheme); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) curveParaSpec[0]); ECDomainParameters ecDomain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); AsymmetricCipherKeyPair keys = new AsymmetricCipherKeyPair( null, new ECPrivateKeyParameters(((BCECPrivateKey) account.getPrivateKey()).getD(), ecDomain)); byte[] out = key_cxt; ECIESKeyEncapsulation kem = new ECIESKeyEncapsulation(new KDF2BytesGenerator(defaultDigest), new SecureRandom()); KeyParameter key1; kem.init(keys.getPrivate()); key1 = (KeyParameter) kem.decrypt(out, keylen); byte[] plaintext; try { Cipher dec = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); Key key = new SecretKeySpec(key1.getKey(), "AES"); dec.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV)); plaintext = dec.doFinal(ciphertext); } catch (Exception e) { throw new Exception("AES failed initialisation - " + e.toString(), e); } return plaintext; } catch (Exception e) { e.printStackTrace(); } return null; }
public static ECPrivateKeyParameters convertPrivateKeyToParameters(BCECPrivateKey ecPriKey) { ECParameterSpec parameterSpec = ecPriKey.getParameters(); ECDomainParameters domainParameters = new ECDomainParameters(parameterSpec.getCurve(), parameterSpec.getG(), parameterSpec.getN(), parameterSpec.getH()); return new ECPrivateKeyParameters(ecPriKey.getD(), domainParameters); }
public BigInteger keyAgreement(ECPoint otherParty) { if (privKey == null) { throw new MissingPrivateKeyException(); } else if (privKey instanceof BCECPrivateKey) { final ECDHBasicAgreement agreement = new ECDHBasicAgreement(); agreement.init(new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE)); return agreement.calculateAgreement(new ECPublicKeyParameters(otherParty, CURVE)); } else { try { final KeyAgreement agreement = ECKeyAgreement.getInstance(this.provider); agreement.init(this.privKey); agreement.doPhase( ECKeyFactory.getInstance(this.provider) .generatePublic(new ECPublicKeySpec(otherParty, CURVE_SPEC)), /* lastPhase */ true); return new BigInteger(1, agreement.generateSecret()); } catch (IllegalStateException | InvalidKeyException | InvalidKeySpecException ex) { throw new RuntimeException("ECDH key agreement failure", ex); } } }