org.bouncycastle.math.ec.ECPoint类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(1620)

本文整理了Java中org.bouncycastle.math.ec.ECPoint类的一些代码示例,展示了ECPoint类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ECPoint类的具体详情如下:
包路径:org.bouncycastle.math.ec.ECPoint
类名称:ECPoint

ECPoint介绍

[英]base class for points on elliptic curves.
[中]椭圆曲线上点的基类。

代码示例

代码示例来源:origin: web3j/web3j

verifyPrecondition(sig.r.signum() >= 0, "r must be positive");
verifyPrecondition(sig.s.signum() >= 0, "s must be positive");
verifyPrecondition(message != null, "message cannot be null");
BigInteger i = BigInteger.valueOf((long) recId / 2);
BigInteger x = sig.r.add(i.multiply(n));
if (!R.multiply(n).isInfinity()) {
  return null;
ECPoint q = ECAlgorithms.sumOfTwoMultiplies(CURVE.getG(), eInvrInv, R, srInv);
byte[] qBytes = q.getEncoded(false);
return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length));

代码示例来源:origin: SeanDragon/protools

/**
 * 验签
 *
 * @param M          签名信息
 * @param signature  签名
 * @param IDA        签名方唯一标识
 * @param aPublicKey 签名方公钥
 * @return true or false
 */
public boolean verify(String M, Signature signature, String IDA, ECPoint aPublicKey) {
  if (!between(signature.r, BigInteger.ONE, n))
    return false;
  if (!between(signature.s, BigInteger.ONE, n))
    return false;
  byte[] M_ = join(ZA(IDA, aPublicKey), M.getBytes());
  BigInteger e = new BigInteger(1, sm3hash(M_));
  BigInteger t = signature.r.add(signature.s).mod(n);
  if (t.equals(BigInteger.ZERO))
    return false;
  ECPoint p1 = G.multiply(signature.s).normalize();
  ECPoint p2 = aPublicKey.multiply(t).normalize();
  BigInteger x1 = p1.add(p2).normalize().getXCoord().toBigInteger();
  BigInteger R = e.add(x1).mod(n);
  if (R.equals(signature.r))
    return true;
  return false;
}

代码示例来源:origin: SeanDragon/protools

/**
 * 判断生成的公钥是否合法
 *
 * @param publicKey
 * @return
 */
private boolean checkPublicKey(ECPoint publicKey) {
  if (!publicKey.isInfinity()) {
    BigInteger x = publicKey.getXCoord().toBigInteger();
    BigInteger y = publicKey.getYCoord().toBigInteger();
    if (between(x, new BigInteger("0"), p) && between(y, new BigInteger("0"), p)) {
      BigInteger xResult = x.pow(3).add(a.multiply(x)).add(b).mod(p);
      BigInteger yResult = y.pow(2).mod(p);
      return yResult.equals(xResult) && publicKey.multiply(n).isInfinity();
    }
  }
  return false;
}

代码示例来源:origin: web3j/web3j

private Bip32ECKeyPair deriveChildKey(int childNumber) {
  if (!hasPrivateKey()) {
    byte[] parentPublicKey = getPublicKeyPoint().getEncoded(true);
    ByteBuffer data = ByteBuffer.allocate(37);
    data.put(parentPublicKey);
    byte[] chainCode = Arrays.copyOfRange(i, 32, 64);
    Arrays.fill(i, (byte) 0);
    BigInteger ilInt = new BigInteger(1, il);
    Arrays.fill(il, (byte) 0);
    ECPoint ki = Sign.publicPointFromPrivate(ilInt).add(getPublicKeyPoint());
        Sign.publicFromPoint(ki.getEncoded(true)),
        childNumber, chainCode, this);
  } else {
      data.put(getPrivateKeyBytes33());
    } else {
      byte[] parentPublicKey = getPublicKeyPoint().getEncoded(true);
      data.put(parentPublicKey);
    byte[] chainCode = Arrays.copyOfRange(i, 32, 64);
    Arrays.fill(i, (byte) 0);
    BigInteger ilInt = new BigInteger(1, il);
    Arrays.fill(il, (byte) 0);
    BigInteger privateKey = getPrivateKey().add(ilInt).mod(Sign.CURVE.getN());

代码示例来源:origin: SeanDragon/protools

/**
 * 密钥协商发起第一步
 *
 * @return
 */
public TransportEntity keyExchange_1() {
  rA = random(n);
  // rA=new BigInteger("83A2C9C8 B96E5AF7 0BD480B4 72409A9A 327257F1
  // EBB73F5B 073354B2 48668563".replace(" ", ""),16);
  RA = G.multiply(rA).normalize();
  return new TransportEntity(RA.getEncoded(false), null, Z, keyPair.getPublicKey());
}

代码示例来源:origin: SeanDragon/protools

/**
 * 签名
 *
 * @param M       签名信息
 * @param IDA     签名方唯一标识
 * @param keyPair 签名方密钥对
 * @return 签名
 */
public Signature sign(String M, String IDA, SM2KeyPair keyPair) {
  byte[] ZA = ZA(IDA, keyPair.getPublicKey());
  byte[] M_ = join(ZA, M.getBytes());
  BigInteger e = new BigInteger(1, sm3hash(M_));
  // BigInteger k = new BigInteger(
  // "6CB28D99 385C175C 94F94E93 4817663F C176D925 DD72B727 260DBAAE
  // 1FB2F96F".replace(" ", ""), 16);
  BigInteger k;
  BigInteger r;
  do {
    k = random(n);
    ECPoint p1 = G.multiply(k).normalize();
    BigInteger x1 = p1.getXCoord().toBigInteger();
    r = e.add(x1);
    r = r.mod(n);
  } while (r.equals(BigInteger.ZERO) || r.add(k).equals(n));
  BigInteger s = ((keyPair.getPrivateKey().add(BigInteger.ONE).modInverse(n))
      .multiply((k.subtract(r.multiply(keyPair.getPrivateKey()))).mod(n))).mod(n);
  return new Signature(r, s);
}

代码示例来源:origin: SeanDragon/protools

ECPoint RB = G.multiply(rB).normalize();
BigInteger x2 = RB.getXCoord().toBigInteger();
x2 = _2w.add(x2.and(_2w.subtract(BigInteger.ONE)));
BigInteger tB = keyPair.getPrivateKey().add(x2.multiply(rB)).mod(n);
ECPoint RA = curve.decodePoint(entity.R).normalize();
BigInteger x1 = RA.getXCoord().toBigInteger();
x1 = _2w.add(x1.and(_2w.subtract(BigInteger.ONE)));
ECPoint aPublicKey = curve.decodePoint(entity.K).normalize();
ECPoint temp = aPublicKey.add(RA.multiply(x1).normalize()).normalize();
ECPoint V = temp.multiply(ecc_bc_spec.getH().multiply(tB)).normalize();
if (V.isInfinity())
  throw new IllegalStateException();
this.V = V;
byte[] xV = V.getXCoord().toBigInteger().toByteArray();
byte[] yV = V.getYCoord().toBigInteger().toByteArray();
byte[] KB = KDF(join(xV, yV, entity.Z, this.Z), 16);
key = KB;
printHexString(KB);
byte[] sB = sm3hash(new byte[]{0x02}, yV,
    sm3hash(xV, entity.Z, this.Z, RA.getXCoord().toBigInteger().toByteArray(),
        RA.getYCoord().toBigInteger().toByteArray(), RB.getXCoord().toBigInteger().toByteArray(),
        RB.getYCoord().toBigInteger().toByteArray()));
return new TransportEntity(RB.getEncoded(false), sB, this.Z, keyPair.getPublicKey());

代码示例来源:origin: ontio/ontology-java-sdk

case SHA256WITHECDSA:
case SM3WITHSM2:
  BigInteger d = new BigInteger(1, prikey);
  ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec((String) this.curveParams[0]);
  ECParameterSpec paramSpec = new ECNamedCurveSpec(spec.getName(), spec.getCurve(), spec.getG(), spec.getN());
  this.privateKey = kf.generatePrivate(priSpec);
  org.bouncycastle.math.ec.ECPoint Q = spec.getG().multiply(d).normalize();
  if (Q == null || Q.getAffineXCoord() == null || Q.getAffineYCoord() == null) {
    throw new SDKException(ErrorCode.OtherError("normalize error"));
      new ECPoint(Q.getAffineXCoord().toBigInteger(), Q.getAffineYCoord().toBigInteger()),
      paramSpec);
  this.publicKey = kf.generatePublic(pubSpec);

代码示例来源:origin: hyperledger-archives/fabric-api

@Override
public byte[] getPublicKeyAtOffset(byte[] publicKey, byte[] offset) {
  BigInteger offsetInt = new BigInteger(publicKey);
  boolean invert = false;
  if (offsetInt.compareTo(BigInteger.ZERO) < 0) {
    invert = true;
    offsetInt = offsetInt.abs();
  }
  ECPoint oG = curve.getG().multiply(offsetInt);
  if (invert) {
    oG = oG.negate();
  }
  return oG.add(curve.getCurve().decodePoint(publicKey)).getEncoded(true);
}

代码示例来源:origin: web3j/web3j

/**
 * Returns public key from the given private key.
 *
 * @param privKey the private key to derive the public key from
 * @return BigInteger encoded public key
 */
public static BigInteger publicKeyFromPrivate(BigInteger privKey) {
  ECPoint point = publicPointFromPrivate(privKey);
  byte[] encoded = point.getEncoded(false);
  return new BigInteger(1, Arrays.copyOfRange(encoded, 1, encoded.length));  // remove prefix
}

代码示例来源:origin: SeanDragon/protools

/**
 * 生成密钥对
 *
 * @return
 */
public SM2KeyPair generateKeyPair() {
  BigInteger d = random(n.subtract(new BigInteger("1")));
  SM2KeyPair keyPair = new SM2KeyPair(G.multiply(d).normalize(), d);
  if (checkPublicKey(keyPair.getPublicKey())) {
    return keyPair;
  } else {
    return null;
  }
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
      EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression),
      ecSpec.getOrder(),
      BigInteger.valueOf(ecSpec.getCofactor()),
      ecSpec.getCurve().getSeed());
BigInteger bX = this.ecPublicKey.getQ().getAffineXCoord().toBigInteger();
BigInteger bY = this.ecPublicKey.getQ().getAffineYCoord().toBigInteger();
byte[] encKey = new byte[64];

代码示例来源:origin: jpcsp/jpcsp

public byte[] multiplyPublicKey(byte[] pub, byte[] priv) {
    PublicKey multPubKey = null;
    ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(new BigInteger(priv), spec);
    ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(curve.decodePoint(pub), spec);
    ECPublicKeySpec newPublicKeySpec = new ECPublicKeySpec(publicKeySpec.getQ().multiply(privateKeySpec.getD()), spec);
    try {
      multPubKey = f.generatePublic(newPublicKeySpec);
    } catch (Exception e) {  
    }       
    return multPubKey.getEncoded();
  }
}

代码示例来源:origin: ch.dissem.jabit/jabit-cryptography-bouncy

@Override
  public byte[] createPoint(byte[] x, byte[] y) {
    return EC_CURVE_PARAMETERS.getCurve().createPoint(
      new BigInteger(1, x),
      new BigInteger(1, y)
    ).getEncoded(false);
  }
}

代码示例来源:origin: openhab/openhab-core

long startTime = System.currentTimeMillis();
org.bouncycastle.jce.spec.ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(CURVE_NAME);
ECField field = new ECFieldFp(ecSpec.getCurve().getField().getCharacteristic());
EllipticCurve curve = new EllipticCurve(field, ecSpec.getCurve().getA().toBigInteger(),
    ecSpec.getCurve().getB().toBigInteger());
ECPoint pointG = new ECPoint(ecSpec.getG().getXCoord().toBigInteger(),
    ecSpec.getG().getYCoord().toBigInteger());
ECParameterSpec spec = new ECParameterSpec(curve, pointG, ecSpec.getN(), ecSpec.getH().intValue());
KeyPairGenerator g = KeyPairGenerator.getInstance(KEY_PAIR_GENERATOR_TYPE);
g.initialize(spec, new SecureRandom());
BigInteger serialNumber = BigInteger.valueOf(randomNumber >= 0 ? randomNumber : randomNumber * -1);
Date notBefore = new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30);
Date notAfter = new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * 10));

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

public String toString()
{
  StringBuffer    buf = new StringBuffer();
  String          nl = Strings.lineSeparator();
  buf.append("EC Public Key").append(nl);
  buf.append("            X: ").append(this.q.getAffineXCoord().toBigInteger().toString(16)).append(nl);
  buf.append("            Y: ").append(this.q.getAffineYCoord().toBigInteger().toString(16)).append(nl);
  return buf.toString();
}

代码示例来源:origin: com.bushidowallet/bushido-core-lib

private boolean hasError(ECDSASignature signature) {
  final BigInteger r = signature.r;
  final BigInteger s = signature.s;
  if (!(r.compareTo(BigInteger.ZERO) == 1 && r.compareTo(key.params.getN()) == -1) || !(s.compareTo(BigInteger.ZERO) == 1 && s.compareTo(key.params.getN()) == -1)) {
    //r and s not in range
    return true;
  }
  final BigInteger e = BigIntegerUtil.fromBytes(hashbuf, 16, endian);
  final BigInteger n = key.params.getN();
  final BigInteger sinv = s.modInverse(n);
  final BigInteger u1 = sinv.multiply(e).mod(n);
  final BigInteger u2 = sinv.multiply(r).mod(n);
  final ECPoint g = key.params.getG();
  final ECPoint p = ECAlgorithms.sumOfTwoMultiplies(g, u1, key.curve.getCurve().decodePoint(key.getPublic()), u2).normalize();
  if (p.isInfinity()) {
    //p is infinity
    return true;
  }
  if (p.getAffineXCoord().toBigInteger().mod(n).compareTo(r) != 0) {
    //invalid signature
    return true;
  } else {
    return false;
  }
}

代码示例来源:origin: SeanDragon/protools

/**
 * 取得用户标识字节数组
 *
 * @param IDA
 * @param aPublicKey
 * @return
 */
private static byte[] ZA(String IDA, ECPoint aPublicKey) {
  byte[] idaBytes = IDA.getBytes();
  int entlenA = idaBytes.length * 8;
  byte[] ENTLA = new byte[]{(byte) (entlenA & 0xFF00), (byte) (entlenA & 0x00FF)};
  byte[] ZA = sm3hash(ENTLA, idaBytes, a.toByteArray(), b.toByteArray(), gx.toByteArray(), gy.toByteArray(),
      aPublicKey.getXCoord().toBigInteger().toByteArray(),
      aPublicKey.getYCoord().toBigInteger().toByteArray());
  return ZA;
}

代码示例来源:origin: Yubico/java-webauthn-server

private static boolean isP256(ECParameterSpec params) {
  ECNamedCurveParameterSpec p256 = ECNamedCurveTable.getParameterSpec("P-256");
  return (Objects.equals(p256.getN(), params.getOrder())
    && Objects.equals(p256.getG().getAffineXCoord().toBigInteger(), params.getGenerator().getAffineX())
    && Objects.equals(p256.getG().getAffineYCoord().toBigInteger(), params.getGenerator().getAffineY())
    && Objects.equals(p256.getH(), BigInteger.valueOf(params.getCofactor()))
  );
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
      EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression),
      ecSpec.getOrder(),
      BigInteger.valueOf(ecSpec.getCofactor()),
      ecSpec.getCurve().getSeed());
BigInteger      bX = this.q.getAffineXCoord().toBigInteger();
BigInteger      bY = this.q.getAffineYCoord().toBigInteger();
byte[]          encKey = new byte[64];
    EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression),
    ecSpec.getOrder(),
    BigInteger.valueOf(ecSpec.getCofactor()),
    ecSpec.getCurve().getSeed());
ECCurve curve = this.engineGetQ().getCurve();
ASN1OctetString p = (ASN1OctetString)
  new X9ECPoint(curve.createPoint(this.getQ().getAffineXCoord().toBigInteger(), this.getQ().getAffineYCoord().toBigInteger(), withCompression)).toASN1Primitive();

相关文章