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); ECPublicKeySpec keySpec = new ECPublicKeySpec(((BCECPublicKey) publicKey).getQ(), ecSpec); publicKey = (PublicKey) keyFactory.generatePublic(keySpec);
/** * 编码压缩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); }
/** * 只获取公钥里的XY分量,64字节 * * @param publicKey * @return */ public static byte[] getRawPublicKey(BCECPublicKey publicKey) { byte[] src65 = publicKey.getQ().getEncoded(false); byte[] rawXY = new byte[CURVE_LEN * 2];//SM2的话这里应该是64字节 System.arraycopy(src65, 1, rawXY, 0, rawXY.length); return rawXY; }
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 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 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); }
/** * Generate a new keypair using the given Java Security Provider. * * All private key operations will use the provider. */ public ECKey(Provider provider, SecureRandom secureRandom) { this.provider = provider; final KeyPairGenerator keyPairGen = ECKeyPairGenerator.getInstance(provider, secureRandom); final KeyPair keyPair = keyPairGen.generateKeyPair(); this.privKey = keyPair.getPrivate(); final PublicKey pubKey = keyPair.getPublic(); if (pubKey instanceof BCECPublicKey) { pub = ((BCECPublicKey) pubKey).getQ(); } else if (pubKey instanceof ECPublicKey) { pub = extractPublicKey((ECPublicKey) pubKey); } else { throw new AssertionError( "Expected Provider " + provider.getName() + " to produce a subtype of ECPublicKey, found " + pubKey.getClass()); } }
/** * UAF_ALG_KEY_ECC_X962_RAW 0x100 Raw ANSI X9.62 formatted Elliptic Curve * public key [SEC1]. * * I.e. [0x04, X (32 bytes), Y (32 bytes)]. Where the byte 0x04 denotes the * uncompressed point compression method. * * @param pub * - Public Key * @return bytes * @throws IOException */ public static byte[] getKeyAsRawBytes(BCECPublicKey pub) throws IOException { byte[] raw; ByteArrayOutputStream bos = new ByteArrayOutputStream(65); bos.write(0x04); bos.write(pub.getQ().getXCoord().getEncoded()); bos.write(pub.getQ().getYCoord().getEncoded()); raw = bos.toByteArray(); logger.info("Raw key length:" + raw.length); return raw; }
/** * UAF_ALG_KEY_ECC_X962_RAW 0x100 Raw ANSI X9.62 formatted Elliptic Curve * public key [SEC1]. * * I.e. [0x04, X (32 bytes), Y (32 bytes)]. Where the byte 0x04 denotes the * uncompressed point compression method. * * @param pub * - Public Key * @return bytes * @throws IOException */ public static byte[] getKeyAsRawBytes(BCECPublicKey pub) throws IOException { byte[] raw; ByteArrayOutputStream bos = new ByteArrayOutputStream(65); bos.write(0x04); bos.write(pub.getQ().getXCoord().getEncoded()); bos.write(pub.getQ().getYCoord().getEncoded()); raw = bos.toByteArray(); logger.info("Raw key length:" + raw.length); return raw; }
/** * 初始化钱包 */ private void initWallet() { try { KeyPair keyPair = newECKeyPair(); BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate(); BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic(); byte[] publicKeyBytes = publicKey.getQ().getEncoded(false); this.setPrivateKey(privateKey); this.setPublicKey(publicKeyBytes); } catch (Exception e) { log.error("Fail to init wallet ! ", e); throw new RuntimeException("Fail to init wallet ! ", e); } }
public byte[] serializePublicKey() { ByteArrayOutputStream bs = new ByteArrayOutputStream(); BCECPublicKey pub = (BCECPublicKey) publicKey; try { switch (this.keyType) { case ECDSA: //bs.write(this.keyType.getLabel()); //bs.write(Curve.valueOf(pub.getParameters().getCurve()).getLabel()); bs.write(pub.getQ().getEncoded(true)); break; case SM2: bs.write(this.keyType.getLabel()); bs.write(Curve.valueOf(pub.getParameters().getCurve()).getLabel()); bs.write(pub.getQ().getEncoded(true)); break; default: // Should not reach here throw new Exception(ErrorCode.UnknownKeyType); } } catch (Exception e) { // Should not reach here e.printStackTrace(); return null; } return bs.toByteArray(); }
public byte[] serializePublicKey() { ByteArrayOutputStream bs = new ByteArrayOutputStream(); BCECPublicKey pub = (BCECPublicKey) publicKey; try { switch (this.keyType) { case ECDSA: //bs.write(this.keyType.getLabel()); //bs.write(Curve.valueOf(pub.getParameters().getCurve()).getLabel()); bs.write(pub.getQ().getEncoded(true)); break; case SM2: bs.write(this.keyType.getLabel()); bs.write(Curve.valueOf(pub.getParameters().getCurve()).getLabel()); bs.write(pub.getQ().getEncoded(true)); break; default: // Should not reach here throw new Exception(ErrorCode.UnknownKeyType); } } catch (Exception e) { // Should not reach here e.printStackTrace(); return null; } return bs.toByteArray(); }
private KeyPairWithSubjectPublicKeyInfo genECKeypair(final String curveNameOrOid, final SecureRandom random) throws Exception { ASN1ObjectIdentifier curveOid = AlgorithmUtil.getCurveOidForCurveNameOrOid(curveNameOrOid); if (curveOid == null) { throw new IllegalArgumentException("invalid curveNameOrOid '" + curveNameOrOid + "'"); } KeyPair kp = KeyUtil.generateECKeypair(curveOid, random); AlgorithmIdentifier algId = new AlgorithmIdentifier( X9ObjectIdentifiers.id_ecPublicKey, curveOid); BCECPublicKey pub = (BCECPublicKey) kp.getPublic(); byte[] keyData = pub.getQ().getEncoded(false); SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(algId, keyData); return new KeyPairWithSubjectPublicKeyInfo(kp, subjectPublicKeyInfo); }
public static String[] Encrypt(String pubkey, byte[] msg, int keylen) { try { com.github.ontio.account.Account account = new com.github.ontio.account.Account(false, Helper.hexToBytes(pubkey)); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) curveParaSpec[0]); ECDomainParameters ecDomain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); AsymmetricCipherKeyPair keys = new AsymmetricCipherKeyPair( new ECPublicKeyParameters(((BCECPublicKey) account.getPublicKey()).getQ(), ecDomain), null); byte[] out = new byte[(ecDomain.getCurve().getFieldSize() / 8) * 2 + 1]; ECIESKeyEncapsulation kem = new ECIESKeyEncapsulation(new KDF2BytesGenerator(defaultDigest), new SecureRandom()); KeyParameter key1; kem.init(keys.getPublic()); key1 = (KeyParameter) kem.encrypt(out, keylen); //AES key = key1 (is encrypted in out) byte[] IV = Hex.decode(getRandomString(keylen)); //choose random IV of length = keylen byte[] ciphertext; try { Cipher en = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); Key key = new SecretKeySpec(key1.getKey(), "AES"); en.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV)); ciphertext = en.doFinal(msg); } catch (Exception e) { throw new Exception("AES failed initialisation - " + e.toString(), e); } //(IV, out, ciphertext) return new String[]{Helper.toHexString(IV), Helper.toHexString(out), Helper.toHexString(ciphertext)}; } catch (Exception e) { e.printStackTrace(); } return null; }
public static ECPublicKeyParameters convertPublicKeyToParameters(BCECPublicKey ecPubKey) { ECParameterSpec parameterSpec = ecPubKey.getParameters(); ECDomainParameters domainParameters = new ECDomainParameters(parameterSpec.getCurve(), parameterSpec.getG(), parameterSpec.getN(), parameterSpec.getH()); return new ECPublicKeyParameters(ecPubKey.getQ(), domainParameters); }
public static String[] Encrypt(String pubkey, byte[] msg, int keylen) { try { com.github.ontio.account.Account account = new com.github.ontio.account.Account(false, Helper.hexToBytes(pubkey)); ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) curveParaSpec[0]); ECDomainParameters ecDomain = new ECDomainParameters(spec.getCurve(), spec.getG(), spec.getN()); AsymmetricCipherKeyPair keys = new AsymmetricCipherKeyPair( new ECPublicKeyParameters(((BCECPublicKey) account.getPublicKey()).getQ(), ecDomain), null); byte[] out = new byte[(ecDomain.getCurve().getFieldSize() / 8) * 2 + 1]; ECIESKeyEncapsulation kem = new ECIESKeyEncapsulation(new KDF2BytesGenerator(defaultDigest), new SecureRandom()); KeyParameter key1; kem.init(keys.getPublic()); key1 = (KeyParameter) kem.encrypt(out, keylen); //AES key = key1 (is encrypted in out) byte[] IV = Hex.decode(getRandomString(keylen)); //choose random IV of length = keylen byte[] ciphertext; try { Cipher en = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); Key key = new SecretKeySpec(key1.getKey(), "AES"); en.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV)); ciphertext = en.doFinal(msg); } catch (Exception e) { throw new Exception("AES failed initialisation - " + e.toString(), e); } //(IV, out, ciphertext) return new String[]{Helper.toHexString(IV), Helper.toHexString(out), Helper.toHexString(ciphertext)}; } catch (Exception e) { e.printStackTrace(); } return null; }