当前位置: 首页>>代码示例>>Java>>正文


Java BCECPrivateKey类代码示例

本文整理汇总了Java中org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey的典型用法代码示例。如果您正苦于以下问题:Java BCECPrivateKey类的具体用法?Java BCECPrivateKey怎么用?Java BCECPrivateKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


BCECPrivateKey类属于org.bouncycastle.jcajce.provider.asymmetric.ec包,在下文中一共展示了BCECPrivateKey类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: genCert

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; //导入依赖的package包/类
protected Certificate genCert(ToBeSignedCertificate tbs, CertificateType certType, AlgorithmIndicator alg,  PublicKey publicKey, PrivateKey signingPrivateKey, PublicKey signingPublicKey, Certificate signingCert) throws IOException, IllegalArgumentException, SignatureException{
	byte[] toBeSignedData = tbs.getEncoded();
	IssuerIdentifier issuerIdentifier;
	try{
		if(signingCert == null){
			issuerIdentifier = new IssuerIdentifier(HashAlgorithm.sha256);
		}else{
			HashedId8 h8 = new HashedId8(cryptoManager.digest(signingCert.getEncoded(), HashAlgorithm.sha256));
			issuerIdentifier = new IssuerIdentifier(h8);
		}
		Signature signature;
		if(certType == CertificateType.explicit){
		  signature = cryptoManager.signMessage(toBeSignedData, alg, publicKey, signingPrivateKey, certType, signingCert);
		  return new Certificate(issuerIdentifier, tbs, signature);
		}else{
			ImplicitCertificateData cert = new ImplicitCertificateData(issuerIdentifier, tbs);
			return ecqvHelper.genImplicitCertificate(cert, alg, (ECPublicKey) publicKey, signingCert, (BCECPublicKey) signingPublicKey, (BCECPrivateKey) signingPrivateKey);

		}
		
	}catch(NoSuchAlgorithmException e){
		throw new IllegalArgumentException("Error, no such algorithm exception: " + e.getMessage());
	}
}
 
开发者ID:pvendil,项目名称:c2c-common,代码行数:25,代码来源:BaseCertGenerator.java

示例2: decodeKeyPair

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; //导入依赖的package包/类
public static KeyPair decodeKeyPair(ECKeyPair ecKeyPair) {
    byte[] bytes = Numeric.toBytesPadded(ecKeyPair.getPublicKey(), 64);
    BigInteger x = Numeric.toBigInt(Arrays.copyOfRange(bytes, 0, 32));
    BigInteger y = Numeric.toBigInt(Arrays.copyOfRange(bytes, 32, 64));
    ECPoint q = curve.createPoint(x, y);
    BCECPublicKey publicKey = new BCECPublicKey(ALGORITHM, new ECPublicKeyParameters(q, dp), BouncyCastleProvider.CONFIGURATION);
    BCECPrivateKey privateKey = new BCECPrivateKey(ALGORITHM, new ECPrivateKeyParameters(ecKeyPair.getPrivateKey(), dp), publicKey, p, BouncyCastleProvider.CONFIGURATION);
    return new KeyPair(publicKey, privateKey);
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:10,代码来源:CryptoUtil.java

示例3: create

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; //导入依赖的package包/类
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);
}
 
开发者ID:web3j,项目名称:web3j,代码行数:16,代码来源:ECKeyPair.java

示例4: generateKey

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; //导入依赖的package包/类
private static ECKey generateKey() {
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDsA", new org.bouncycastle.jce.provider.BouncyCastleProvider());
        ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256k1");
        keyGen.initialize(ecSpec, new SecureRandom());
        KeyPair generateKeyPair = keyGen.generateKeyPair();

        BCECPrivateKey private1 = (BCECPrivateKey) generateKeyPair.getPrivate();
        BCECPublicKey public1 = (BCECPublicKey) generateKeyPair.getPublic();
        String X = public1.engineGetQ().getAffineXCoord().toBigInteger().toString(16);
        String Y = public1.engineGetQ().getAffineYCoord().toBigInteger().toString(16);

        // format string to 64 length with zero in head

        String x = formatStringAdd0(X, 64);
        String y = formatStringAdd0(Y, 64);

        // public key string
        String publicKeyStr = "04" + x + y;

        // set public key begin with 04
        return new ECKey(private1.getS().toByteArray(), false, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:bushidowallet,项目名称:bushido-java-core,代码行数:28,代码来源:MultiSigScriptHashInputTest.java

示例5: generateKeyPair

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; //导入依赖的package包/类
public void generateKeyPair(){
  try {
    LOGGER.info("Generating new keypair");
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", BC_PROVIDER);
    keyGen.initialize(secp256k1, random);
    KeyPair localKeypair = keyGen.generateKeyPair();
    localPrivateECKey=(BCECPrivateKey) localKeypair.getPrivate();
    localPublicECKey=(BCECPublicKey) localKeypair.getPublic();
    localId=new NodeIdentifier(localPublicECKey);
  } catch (Exception e) {
    throw new Error(e);
  }
}
 
开发者ID:pmarches,项目名称:peercentrum-core,代码行数:14,代码来源:NodeIdentity.java

示例6: eciesDecrypt

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; //导入依赖的package包/类
public ByteString eciesDecrypt(PrivateKey recipientPrivateKey, ByteString cipherText) {
    BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) recipientPrivateKey;
    ECNamedCurveSpec ecNamedCurveSpec = (ECNamedCurveSpec) bcecPrivateKey.getParams();
    int level = SecurityLevel.from(ecNamedCurveSpec.getName()).size();

    //cipherText = ephemeralPubKeyBytes + encryptedTokBytes + macBytes
    //ephemeralPubKeyBytes = first ((384+7)/8)*2 + 1 bytes = first 97 bytes
    //hmac is sha3_384 = 48 bytes or sha3_256 = 32 bytes
    int ephemeralPubKeyLength = ((level + 7) / 8) * 2 + 1;
    int hmacLength = level >> 3;
    int cipherTextLength = cipherText.size();

    if (cipherTextLength <= ephemeralPubKeyLength + hmacLength)
        throw new RuntimeException(String.format("Illegal cipherText length: %d must be > %d", cipherTextLength, ephemeralPubKeyLength + hmacLength));

    ByteString ephemeralPubKey = cipherText.substring(0, ephemeralPubKeyLength);
    ByteString encryptedContent = cipherText.substring(ephemeralPubKeyLength, cipherTextLength - hmacLength);
    ByteString hmac = cipherText.substring(cipherTextLength - hmacLength);

    ECPrivateKeyParameters ecdhPrivateKeyParameters;
    try {
        ecdhPrivateKeyParameters = (ECPrivateKeyParameters) (PrivateKeyFactory.createKey(bcecPrivateKey.getEncoded()));
    } catch (IOException e) {
        logger.error("ECIES decrypt load private key exception", e);
        throw new RuntimeException(e);
    }
    ECDomainParameters ecDomainParameters = ecdhPrivateKeyParameters.getParameters();
    ECCurve ecCurve = ecDomainParameters.getCurve();
    ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(ecCurve.decodePoint(ephemeralPubKey.toByteArray()), ecDomainParameters);
    BasicAgreement agree = new ECDHBasicAgreement();
    agree.init(ecdhPrivateKeyParameters);
    byte[] keyAgreement = agree.calculateAgreement(ecPublicKeyParameters).toByteArray();

    HKDFParameters hkdfParameters = new HKDFParameters(keyAgreement, null, null);
    HKDFBytesGenerator hkdfBytesGenerator = new HKDFBytesGenerator(digest);
    hkdfBytesGenerator.init(hkdfParameters);
    byte[] hkdfOutputBytes = new byte[AESKEY_LENGTH + HMACKEY_LENGTH];
    hkdfBytesGenerator.generateBytes(hkdfOutputBytes, 0, AESKEY_LENGTH + HMACKEY_LENGTH);
    ByteString hkdfOutput = ByteString.copyFrom(hkdfOutputBytes);
    ByteString aesKey = hkdfOutput.substring(0, AESKEY_LENGTH);
    ByteString hmacKey = hkdfOutput.substring(AESKEY_LENGTH, AESKEY_LENGTH + HMACKEY_LENGTH);
    HMac hMac = new HMac(digest);
    hMac.init(new KeyParameter(hmacKey.toByteArray()));
    hMac.update(encryptedContent.toByteArray(), 0, encryptedContent.size());
    byte[] recoveredHmac = new byte[hMac.getMacSize()];
    hMac.doFinal(recoveredHmac, 0);
    if (!MessageDigest.isEqual(hmac.toByteArray(), recoveredHmac)) {
        throw new RuntimeException("HMAC verify failed");
    }

    CFBBlockCipher aesCipher = new CFBBlockCipher(
            new AESEngine(), BLOCK_BIT_SIZE);
    ByteString iv = encryptedContent.substring(0, IV_LENGTH);
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(aesKey.toByteArray()), iv.toByteArray());
    aesCipher.init(false, ivAndKey);
    byte[] decryptedBytes = new byte[500];
    aesCipher.decryptBlock(encryptedContent.substring(IV_LENGTH).toByteArray(), 0, decryptedBytes, 0);
    return ByteString.copyFrom(decryptedBytes);
}
 
开发者ID:GrapeBaBa,项目名称:fabric-java,代码行数:60,代码来源:Crypto.java

示例7: loadKeyPairAndCertificateFromFile

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; //导入依赖的package包/类
protected void loadKeyPairAndCertificateFromFile() throws Exception {
    PemReader certificatePEMReader=new PemReader(new FileReader(this.localCertificateFile));
    PemObject certificatePEM=certificatePEMReader.readPemObject();
    certificatePEMReader.close();
    CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
    this.cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certificatePEM.getContent()));
    localCertificateChainArray=new Certificate[]{cert};


    PKCS8EncodedKeySpec encodedKeySpec;
//    if(false){
//      PemReader privateKeyReader=new PemReader(new FileReader(this.localPrivateKeyFile));
//      PemObject privateKeyPEM=privateKeyReader.readPemObject();
//      privateKeyReader.close();
//      encodedKeySpec = new PKCS8EncodedKeySpec(privateKeyPEM.getContent());
//    }
//    else if(true){
      PEMParser privateKeyParser=new PEMParser(new FileReader(this.localPrivateKeyFile));
      PemObject privateKeyPEM=privateKeyParser.readPemObject();
      privateKeyParser.close();
      encodedKeySpec = new PKCS8EncodedKeySpec(privateKeyPEM.getContent());
//    }
//    else{
//      //For whatever reason PemReader is not accessible from outside their package
//      Class pemReaderClass=Class.forName("io.netty.handler.ssl.PemReader");
//      Method readPrivateKeyMethod = pemReaderClass.getDeclaredMethod("readPrivateKey", File.class);
//      readPrivateKeyMethod.setAccessible(true);
//      ByteBuf encodedKeyBuf = (ByteBuf) readPrivateKeyMethod.invoke(null, localPrivateKeyFile);
//      //    ByteBuf encodedKeyBuf = PemReader.readPrivateKey(localPrivateKeyFile);
//      byte[] encodedKey = new byte[encodedKeyBuf.readableBytes()];
//      encodedKeyBuf.readBytes(encodedKey).release();
//      encodedKeySpec = new PKCS8EncodedKeySpec(encodedKey);
//    }
    KeyFactory ecKeyFactory = KeyFactory.getInstance("EC", BC_PROVIDER);
    localPrivateECKey = (BCECPrivateKey) ecKeyFactory.generatePrivate(encodedKeySpec);
    localPublicECKey = (BCECPublicKey) localCertificateChainArray[0].getPublicKey();

    localId=new NodeIdentifier(localPublicECKey);
    LOGGER.debug("Loaded identity "+localPublicECKey);
    LOGGER.debug("Loaded identity "+localId);
  }
 
开发者ID:pmarches,项目名称:peercentrum-core,代码行数:42,代码来源:NodeIdentity.java

示例8: ECDSAKeyFactory

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey; //导入依赖的package包/类
public ECDSAKeyFactory(SignatureAlgorithm signatureAlgorithm, String dnName)
        throws InvalidParameterException, NoSuchProviderException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException, SignatureException, InvalidKeyException, CertificateEncodingException {
    if (signatureAlgorithm == null) {
        throw new InvalidParameterException("The signature algorithm cannot be null");
    }

    this.signatureAlgorithm = signatureAlgorithm;

    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(signatureAlgorithm.getCurve().getName());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
    keyGen.initialize(ecSpec, new SecureRandom());

    this.keyPair = keyGen.generateKeyPair();
    BCECPrivateKey privateKeySpec = (BCECPrivateKey) keyPair.getPrivate();
    BCECPublicKey publicKeySpec = (BCECPublicKey) keyPair.getPublic();

    BigInteger x = publicKeySpec.getQ().getX().toBigInteger();
    BigInteger y = publicKeySpec.getQ().getY().toBigInteger();
    BigInteger d = privateKeySpec.getD();

    this.ecdsaPrivateKey = new ECDSAPrivateKey(d);
    this.ecdsaPublicKey = new ECDSAPublicKey(signatureAlgorithm, x, y);

    if (StringUtils.isNotBlank(dnName)) {
        // Create certificate
        GregorianCalendar startDate = new GregorianCalendar(); // time from which certificate is valid
        GregorianCalendar expiryDate = new GregorianCalendar(); // time after which certificate is not valid
        expiryDate.add(Calendar.YEAR, 1);
        BigInteger serialNumber = new BigInteger(1024, new Random()); // serial number for certificate

        X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
        X500Principal principal = new X500Principal(dnName);

        certGen.setSerialNumber(serialNumber);
        certGen.setIssuerDN(principal);
        certGen.setNotBefore(startDate.getTime());
        certGen.setNotAfter(expiryDate.getTime());
        certGen.setSubjectDN(principal); // note: same as issuer
        certGen.setPublicKey(keyPair.getPublic());
        certGen.setSignatureAlgorithm("SHA256WITHECDSA");

        X509Certificate x509Certificate = certGen.generate(privateKeySpec, "BC");
        this.certificate = new Certificate(signatureAlgorithm, x509Certificate);
    }
}
 
开发者ID:GluuFederation,项目名称:oxAuth,代码行数:48,代码来源:ECDSAKeyFactory.java


注:本文中的org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。