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); }
ecParam.getH(), ecParam.getSeed()); KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BouncyCastleProvider.PROVIDER_NAME); ECPrivateKeySpec keySpec = new ECPrivateKeySpec(((BCECPrivateKey) privateKey).getS(), ecSpec); privateKey = (PrivateKey) keyFactory.generatePrivate(keySpec);
ecParam.getH(), ecParam.getSeed()); KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BouncyCastleProvider.PROVIDER_NAME); ECPublicKeySpec keySpec = new ECPublicKeySpec(((BCECPublicKey) publicKey).getQ(), ecSpec); publicKey = (PublicKey) keyFactory.generatePublic(keySpec);
protected Key engineTranslateKey( Key key) throws InvalidKeyException { if (key instanceof ECPublicKey) { return new BCECPublicKey((ECPublicKey)key, configuration); } else if (key instanceof ECPrivateKey) { return new BCECPrivateKey((ECPrivateKey)key, configuration); } throw new InvalidKeyException("key type unknown"); }
private void writeObject( ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeObject(this.getEncoded()); } }
private void writeObject( ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeObject(this.getEncoded()); } }
@Override protected byte[] engineGetEncoded() throws IOException { return engineGetEncoded("ASN.1"); }
BCECPublicKey( String algorithm, SubjectPublicKeyInfo info, ProviderConfiguration configuration) { this.algorithm = algorithm; this.configuration = configuration; populateFromPubKeyInfo(info); }
@Override protected void engineInit(byte[] bytes) throws IOException { engineInit(bytes, "ASN.1"); }
BCECPrivateKey( String algorithm, PrivateKeyInfo info, ProviderConfiguration configuration) throws IOException { this.algorithm = algorithm; this.configuration = configuration; populateFromPrivKeyInfo(info); }
static AsymmetricKeyParameter generatePublicKeyParameter( PublicKey key) throws InvalidKeyException { return (key instanceof BCECPublicKey) ? ((BCECPublicKey)key).engineGetKeyParameters() : ECUtil.generatePublicKeyParameter(key); }
protected void engineInit( Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { if (params != null && !(params instanceof MQVParameterSpec || params instanceof UserKeyingMaterialSpec || params instanceof DHUParameterSpec)) { throw new InvalidAlgorithmParameterException("No algorithm parameters supported"); } initFromKey(key, params); }
public String toString() { return ECUtil.privateKeyToString("EC", d, engineGetSpec()); }
public int engineUpdate( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) { buffer.write(input, inputOffset, inputLen); return 0; }
/** * Generates a EC key pair. * * @param random Random source required for key generation. * @param bitLength Desired key size in bits. * * @return EC key pair of desired size. */ public static KeyPair generateEC(final SecureRandom random, final int bitLength) { final org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi.EC generator = new org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi.EC(); generator.initialize(bitLength, random); return generator.generateKeyPair(); }
public int engineDoFinal( byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { byte[] buf = engineDoFinal(input, inputOffset, inputLength); System.arraycopy(buf, 0, output, outputOffset, buf.length); return buf.length; }
public void erase() { Arrays.fill(this.buf, (byte)0); reset(); } }
protected void initializeNamedCurve(String curveName, SecureRandom random) throws InvalidAlgorithmParameterException { ECNamedCurveSpec namedCurve = createNamedCurveSpec(curveName); this.ecParams = namedCurve; this.param = createKeyGenParamsJCE(namedCurve, random); } }
public KeyPair generateNewKeyPairInSecp384r1() { KeyPair pair = keyPairGeneratorSecp384r1.generateKeyPair(); return pair; }
/** * 编码压缩EC公钥(基于BouncyCastle)<br> * 见:https://www.cnblogs.com/xinzhao/p/8963724.html * * @param publicKey {@link PublicKey},必须为org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey * @return 压缩得到的X * @since 4.4.4 */ public static byte[] encodeECPublicKey(PublicKey publicKey) { return ((org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) publicKey).getQ().getEncoded(true); }