Java 类java.security.spec.ECPublicKeySpec 实例源码

项目:ipack    文件:JCEECPublicKey.java   
public JCEECPublicKey(
    String              algorithm,
    org.bouncycastle.jce.spec.ECPublicKeySpec     spec)
{
    this.algorithm = algorithm;
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getX().toBigInteger(), q.getY().toBigInteger(), false);
        }               
        this.ecSpec = null;
    }
}
项目:ipack    文件:BCDSTU4145PublicKey.java   
public BCDSTU4145PublicKey(
    org.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getX().toBigInteger(), q.getY().toBigInteger(), false);
        }
        this.ecSpec = null;
    }
}
项目:ipack    文件:BCECGOST3410PublicKey.java   
public BCECGOST3410PublicKey(
    org.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getX().toBigInteger(), q.getY().toBigInteger(), false);
        }               
        this.ecSpec = null;
    }
}
项目:citizen-sdk-android    文件:CryptoService.java   
private static ECPublicKey importBrowserAuthPublicKey(String publicKey)
throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException
 {
     int xPos = publicKey.indexOf('x');
     int yPos = publicKey.indexOf('y');

     if (xPos != 0 || yPos < 0 || xPos >= yPos - 1 || publicKey.length() <= yPos + 2) {
        throw new InvalidKeyException("Incorrectly formatted ECDH key.");
     }

     String x = publicKey.substring(publicKey.indexOf('x') + 1, publicKey.indexOf('y'));
     String y = publicKey.substring(publicKey.indexOf('y') + 1);

     KeyFactory kf = KeyFactory.getInstance("EC");

     ECPoint point = new ECPoint(new BigInteger(x, 16), new BigInteger(y, 16));

     ECPublicKey convertedPubKey = (ECPublicKey) kf.generatePublic(new ECPublicKeySpec(point, BROWSER_AUTH_EC_SPEC));

     return convertedPubKey;
 }
项目:RISE-V2G    文件:SecurityUtils.java   
/**
 * Returns the ECPublicKey instance from its encoded raw bytes. 
 * The first byte has the fixed value 0x04 indicating the uncompressed form.
 * Therefore, the byte array must be of form: [0x04, x coord of point (32 bytes), y coord of point (32 bytes)]
 * 
 * @param publicKeyBytes The byte array representing the encoded raw bytes of the public key
 * @return The ECPublicKey instance
 */
public static ECPublicKey getPublicKey(byte[] publicKeyBytes) {
    // First we separate x and y of coordinates into separate variables
    byte[] x = new byte[32];
    byte[] y = new byte[32];
    System.arraycopy(publicKeyBytes, 1, x, 0, 32);
    System.arraycopy(publicKeyBytes, 33, y, 0, 32);

    try {
        KeyFactory kf = KeyFactory.getInstance("EC");

        AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
        parameters.init(new ECGenParameterSpec("secp256r1"));
        ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);

        ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)), ecParameterSpec);
        ECPublicKey ecPublicKey = (ECPublicKey) kf.generatePublic(ecPublicKeySpec);
        return ecPublicKey;
    } catch (NoSuchAlgorithmException | InvalidParameterSpecException | InvalidKeySpecException e) {
        getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get public key from raw bytes", e);
        return null;
    }
}
项目:openjdk-jdk10    文件:TestKeyFactory.java   
private static void testPublic(KeyFactory kf, PublicKey key) throws Exception {
    System.out.println("Testing public key...");
    PublicKey key2 = (PublicKey)kf.translateKey(key);
    KeySpec keySpec = kf.getKeySpec(key, ECPublicKeySpec.class);
    PublicKey key3 = kf.generatePublic(keySpec);
    KeySpec x509Spec = kf.getKeySpec(key, X509EncodedKeySpec.class);
    PublicKey key4 = kf.generatePublic(x509Spec);
    KeySpec x509Spec2 = new X509EncodedKeySpec(key.getEncoded());
    PublicKey key5 = kf.generatePublic(x509Spec2);
    testKey(key, key);
    testKey(key, key2);
    testKey(key, key3);
    testKey(key, key4);
    testKey(key, key5);
}
项目:wycheproof    文件:EcdhTest.java   
/**
 * Returns this key as ECPublicKeySpec or null if the key cannot be represented as
 * ECPublicKeySpec. The later happens for example if the order of cofactor are not positive.
 */
public ECPublicKeySpec getSpec() {
  try {
    ECFieldFp fp = new ECFieldFp(p);
    EllipticCurve curve = new EllipticCurve(fp, a, b);
    ECPoint g = new ECPoint(gx, gy);
    // ECParameterSpec requires that the cofactor h is specified.
    if (h == null) {
      return null;
    }
    ECParameterSpec params = new ECParameterSpec(curve, g, n, h);
    ECPoint pubPoint = new ECPoint(pubx, puby);
    ECPublicKeySpec pub = new ECPublicKeySpec(pubPoint, params);
    return pub;
  } catch (Exception ex) {
    System.out.println(comment + " throws " + ex.toString());
    return null;
  }
}
项目:wycheproof    文件:EcUtil.java   
/**
 * Returns a weak public key of order 3 such that the public key point is on the curve specified
 * in ecParams. This method is used to check ECC implementations for missing step in the
 * verification of the public key. E.g. implementations of ECDH must verify that the public key
 * contains a point on the curve as well as public and secret key are using the same curve.
 *
 * @param ecParams the parameters of the key to attack. This must be a curve in Weierstrass form
 *     over a prime order field.
 * @return a weak EC group with a genrator of order 3.
 */
public static ECPublicKeySpec getWeakPublicKey(ECParameterSpec ecParams)
    throws GeneralSecurityException {
  EllipticCurve curve = ecParams.getCurve();
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
  keyGen.initialize(ecParams);
  BigInteger p = getModulus(curve);
  BigInteger three = new BigInteger("3");
  while (true) {
    // Generate a point on the original curve
    KeyPair keyPair = keyGen.generateKeyPair();
    ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
    ECPoint w = pub.getW();
    BigInteger x = w.getAffineX();
    BigInteger y = w.getAffineY();
    // Find the curve parameters a,b such that 3*w = infinity.
    // This is the case if the following equations are satisfied:
    //    3x == l^2 (mod p)
    //    l == (3x^2 + a) / 2*y (mod p)
    //    y^2 == x^3 + ax + b (mod p)
    BigInteger l;
    try {
      l = modSqrt(x.multiply(three), p);
    } catch (GeneralSecurityException ex) {
      continue;
    }
    BigInteger xSqr = x.multiply(x).mod(p);
    BigInteger a = l.multiply(y.add(y)).subtract(xSqr.multiply(three)).mod(p);
    BigInteger b = y.multiply(y).subtract(x.multiply(xSqr.add(a))).mod(p);
    EllipticCurve newCurve = new EllipticCurve(curve.getField(), a, b);
    // Just a sanity check.
    checkPointOnCurve(w, newCurve);
    // Cofactor and order are of course wrong.
    ECParameterSpec spec = new ECParameterSpec(newCurve, w, p, 1);
    return new ECPublicKeySpec(w, spec);
  }
}
项目:Aki-SSL    文件:JCEECPublicKey.java   
public JCEECPublicKey(
    String              algorithm,
    org.bouncycastle.jce.spec.ECPublicKeySpec     spec)
{
    this.algorithm = algorithm;
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger(), false);
        }               
        this.ecSpec = null;
    }
}
项目:Aki-SSL    文件:BCDSTU4145PublicKey.java   
public BCDSTU4145PublicKey(
    org.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
项目:Aki-SSL    文件:BCECGOST3410PublicKey.java   
public BCECGOST3410PublicKey(
    org.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
项目:SI    文件:SecurityInfoSerDesTest.java   
@Test
public void security_info_rpk_ser_des_then_equal() throws Exception {
    byte[] publicX = Hex
            .decodeHex("89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5".toCharArray());
    byte[] publicY = Hex
            .decodeHex("cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68".toCharArray());
    // Get Elliptic Curve Parameter spec for secp256r1
    AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
    algoParameters.init(new ECGenParameterSpec("secp256r1"));
    ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);

    // Create key specs
    KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)),
            parameterSpec);

    SecurityInfo si = SecurityInfo.newRawPublicKeyInfo("myendpoint",
            KeyFactory.getInstance("EC").generatePublic(publicKeySpec));

    byte[] data = SecurityInfoSerDes.serialize(si);

    assertEquals(
            "{\"ep\":\"myendpoint\",\"rpk\":{\"x\":\"89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5\",\"y\":\"cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68\",\"params\":\"secp256r1\"}}",
            new String(data));
    System.err.println(new String(SecurityInfoSerDes.serialize(SecurityInfoSerDes.deserialize(data))));
    assertEquals(si, SecurityInfoSerDes.deserialize(data));
}
项目:leshan    文件:SecurityInfoSerDesTest.java   
@Test
public void security_info_rpk_ser_des_then_equal() throws Exception {
    byte[] publicX = Hex
            .decodeHex("89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5".toCharArray());
    byte[] publicY = Hex
            .decodeHex("cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68".toCharArray());
    // Get Elliptic Curve Parameter spec for secp256r1
    AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
    algoParameters.init(new ECGenParameterSpec("secp256r1"));
    ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);

    // Create key specs
    KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)),
            parameterSpec);

    SecurityInfo si = SecurityInfo.newRawPublicKeyInfo("myendpoint",
            KeyFactory.getInstance("EC").generatePublic(publicKeySpec));

    byte[] data = SecurityInfoSerDes.serialize(si);

    assertEquals(
            "{\"ep\":\"myendpoint\",\"rpk\":{\"x\":\"89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5\",\"y\":\"cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68\",\"params\":\"secp256r1\"}}",
            new String(data));
    System.err.println(new String(SecurityInfoSerDes.serialize(SecurityInfoSerDes.deserialize(data))));
    assertEquals(si, SecurityInfoSerDes.deserialize(data));
}
项目:UAF    文件:KeyCodec.java   
/**
 * Decode based on X, Y 32 byte integers
 * 
 * @param pubKey
 * @param curveName
 *            - Example secp256r1
 * @return
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchProviderException
 */
public static PublicKey getPubKeyFromCurve(byte[] pubKey, String curveName)
        throws InvalidKeySpecException, NoSuchAlgorithmException,
        NoSuchProviderException {

    ECNamedCurveParameterSpec spec = ECNamedCurveTable
            .getParameterSpec(curveName);
    KeyFactory kf = KeyFactory.getInstance("ECDSA",
            new BouncyCastleProvider());
    ECNamedCurveSpec params = new ECNamedCurveSpec(curveName,
            spec.getCurve(), spec.getG(), spec.getN());
    ECPoint point = ECPointUtil.decodePoint(params.getCurve(), pubKey);
    ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params);
    ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec);
    return pk;
}
项目:SI    文件:SecurityInfoSerDesTest.java   
@Test
public void security_info_rpk_ser_des_then_equal() throws Exception {
    byte[] publicX = Hex
            .decodeHex("89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5".toCharArray());
    byte[] publicY = Hex
            .decodeHex("cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68".toCharArray());
    // Get Elliptic Curve Parameter spec for secp256r1
    AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
    algoParameters.init(new ECGenParameterSpec("secp256r1"));
    ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);

    // Create key specs
    KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)),
            parameterSpec);

    SecurityInfo si = SecurityInfo.newRawPublicKeyInfo("myendpoint",
            KeyFactory.getInstance("EC").generatePublic(publicKeySpec));

    byte[] data = SecurityInfoSerDes.serialize(si);

    assertEquals(
            "{\"ep\":\"myendpoint\",\"rpk\":{\"x\":\"89c048261979208666f2bfb188be1968fc9021c416ce12828c06f4e314c167b5\",\"y\":\"cbf1eb7587f08e01688d9ada4be859137ca49f79394bad9179326b3090967b68\",\"params\":\"secp256r1\"}}",
            new String(data));
    System.err.println(new String(SecurityInfoSerDes.serialize(SecurityInfoSerDes.deserialize(data))));
    assertEquals(si, SecurityInfoSerDes.deserialize(data));
}
项目:j2ssh-maverick    文件:Ssh2EcdsaSha2NistPublicKey.java   
public static void main(String[] args) throws Exception {


    KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
    ECGenParameterSpec gps = new ECGenParameterSpec ("secp256r1"); // NIST P-256 
    kpg.initialize(gps); 
    KeyPair apair = kpg.generateKeyPair(); 
    ECPublicKey apub  = (ECPublicKey)apair.getPublic();
    ECParameterSpec aspec = apub.getParams();
    // could serialize aspec for later use (in compatible JRE)
    //
    // for test only reuse bogus pubkey, for real substitute values 
    ECPoint apoint = apub.getW();
    BigInteger x = apoint.getAffineX(), y = apoint.getAffineY();
    // construct point plus params to pubkey
    ECPoint bpoint = new ECPoint (x,y); 
    ECPublicKeySpec bpubs = new ECPublicKeySpec (bpoint, aspec);
    KeyFactory kfa = KeyFactory.getInstance ("EC");
    ECPublicKey bpub = (ECPublicKey) kfa.generatePublic(bpubs);

    new Ssh2EcdsaSha2NistPublicKey(bpub);
}
项目:RipplePower    文件:JCEECPublicKey.java   
public JCEECPublicKey(
    String              algorithm,
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec     spec)
{
    this.algorithm = algorithm;
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger(), false);
        }               
        this.ecSpec = null;
    }
}
项目:RipplePower    文件:BCDSTU4145PublicKey.java   
public BCDSTU4145PublicKey(
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
项目:RipplePower    文件:BCECGOST3410PublicKey.java   
public BCECGOST3410PublicKey(
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
项目:cn1    文件:ECPublicKeySpec_ImplTest.java   
/**
 * Test #3 for <code>ECPublicKeySpec(ECPoint, ECParameterSpec)</code> constructor<br> 
 * Assertion: throws <code>IllegalArgumentException</code> if
 * <code>w</code> is point at infinity<br>
 * Test preconditions: pass <code>ECPoint.POINT_INFINITY</code>
 * as mentioned parameter value<br>
 * Expected: must throw <code>IllegalArgumentException</code>
 */
public final void testECPublicKeySpec03() {
    // Valid (see note below) parameters set
    EllipticCurve c =
        new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
                          BigInteger.ZERO,
                          BigInteger.valueOf(4L));
    ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));

    try {
        new ECPublicKeySpec(ECPoint.POINT_INFINITY,
                new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10));
       fail("Expected IAE not thrown");
    } catch (IllegalArgumentException ok) {
    }
}
项目:cn1    文件:ECPublicKeySpec_ImplTest.java   
/**
 * Test for <code>getParams()</code> method<br>
 * Assertion: returns associated EC parameters<br>
 * Test preconditions: <code>ECPublicKeySpec</code> instance
 * created using valid parameters<br>
 * Expected: must return params value which is equal
 * to the one passed to the constructor; (both must refer
 * the same object)
 */
public final void testGetParams() {
    // Valid (see note below) parameters set
    EllipticCurve c =
        new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
                          BigInteger.ZERO,
                          BigInteger.valueOf(4L));
    ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
    ECParameterSpec params =
        new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);

    ECPublicKeySpec ks = new ECPublicKeySpec(g, params);
    ECParameterSpec paramsRet = ks.getParams();

    assertEquals(params, paramsRet);
    assertSame(params, paramsRet);
}
项目:cn1    文件:ECPublicKeySpec_ImplTest.java   
/**
 * Test for <code>getW()</code> method<br>
 * Assertion: returns associated public point<br>
 * Test preconditions: <code>ECPublicKeySpec</code> instance
 * created using valid parameters<br>
 * Expected: must return w value which is equal
 * to the one passed to the constructor; (both must refer
 * the same object)
 */
public final void testGetW() {
    // Valid (see note below) parameters set
    EllipticCurve c =
        new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
                          BigInteger.ZERO,
                          BigInteger.valueOf(4L));
    ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
    ECParameterSpec params =
        new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);

    ECPublicKeySpec ks = new ECPublicKeySpec(g, params);
    ECPoint wRet = ks.getW();

    assertEquals(g, wRet);
    assertSame(g, wRet);
}
项目:CryptMeme    文件:JCEECPublicKey.java   
public JCEECPublicKey(
    String              algorithm,
    org.bouncycastle.jce.spec.ECPublicKeySpec     spec)
{
    this.algorithm = algorithm;
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger(), false);
        }               
        this.ecSpec = null;
    }
}
项目:CryptMeme    文件:BCDSTU4145PublicKey.java   
public BCDSTU4145PublicKey(
    org.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
项目:CryptMeme    文件:BCECGOST3410PublicKey.java   
public BCECGOST3410PublicKey(
    org.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
项目:mtools    文件:ECCCoder.java   
/**
 * 加密<br>
 * 用公钥加密
 * 
 * @param data
 * @param privateKey
 * @return
 * @throws Exception
 */
public static byte[] encrypt(byte[] data, String privateKey)
        throws Exception {
    // 对公钥解密
    byte[] keyBytes = decryptBASE64(privateKey);

    // 取得公钥
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = ECKeyFactory.INSTANCE;

    ECPublicKey pubKey = (ECPublicKey) keyFactory
            .generatePublic(x509KeySpec);

    ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(pubKey.getW(),
            pubKey.getParams());

    // 对数据加密
    // TODO Chipher不支持EC算法 未能实现
    Cipher cipher = new NullCipher();
    // Cipher.getInstance(ALGORITHM, keyFactory.getProvider());
    cipher.init(Cipher.ENCRYPT_MODE, pubKey, ecPublicKeySpec.getParams());

    return cipher.doFinal(data);
}
项目:test4java    文件:ECCCoder.java   
/**
 * 加密<br>
 * 用公钥加密
 * 
 * @param data
 * @param privateKey
 * @return
 * @throws Exception
 */
public static byte[] encrypt(byte[] data, String privateKey)
        throws Exception {
    // 对公钥解密
    byte[] keyBytes = decryptBASE64(privateKey);

    // 取得公钥
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = ECKeyFactory.INSTANCE;

    ECPublicKey pubKey = (ECPublicKey) keyFactory
            .generatePublic(x509KeySpec);

    ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(pubKey.getW(),
            pubKey.getParams());

    // 对数据加密
    // TODO Chipher不支持EC算法 未能实现
    Cipher cipher = new NullCipher();
    // Cipher.getInstance(ALGORITHM, keyFactory.getProvider());
    cipher.init(Cipher.ENCRYPT_MODE, pubKey, ecPublicKeySpec.getParams());

    return cipher.doFinal(data);
}
项目:tink    文件:EllipticCurves.java   
public static byte[] computeSharedSecret(ECPrivateKey myPrivateKey, ECPoint publicPoint)
    throws GeneralSecurityException {
  checkPointOnCurve(publicPoint, myPrivateKey.getParams().getCurve());
  // Explicitly reconstruct the peer public key using private key's spec.
  ECParameterSpec privSpec = myPrivateKey.getParams();
  EllipticCurve privCurve = privSpec.getCurve();
  ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(publicPoint, privSpec);
  KeyFactory kf = KeyFactory.getInstance("EC");
  PublicKey publicKey = kf.generatePublic(publicKeySpec);
  KeyAgreement ka = EngineFactory.KEY_AGREEMENT.getInstance("ECDH");
  ka.init(myPrivateKey);
  ka.doPhase(publicKey, true /* lastPhase */);
  byte[] secret = ka.generateSecret();
  validateSharedSecret(secret, myPrivateKey);
  return secret;
}
项目:ripple-lib-java    文件:JCEECPublicKey.java   
public JCEECPublicKey(
    String              algorithm,
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec     spec)
{
    this.algorithm = algorithm;
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger(), false);
        }               
        this.ecSpec = null;
    }
}
项目:ripple-lib-java    文件:BCDSTU4145PublicKey.java   
public BCDSTU4145PublicKey(
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
项目:ripple-lib-java    文件:BCECGOST3410PublicKey.java   
public BCECGOST3410PublicKey(
    org.ripple.bouncycastle.jce.spec.ECPublicKeySpec spec)
{
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.ripple.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getAffineXCoord().toBigInteger(), q.getAffineYCoord().toBigInteger());
        }
        this.ecSpec = null;
    }
}
项目:freeVM    文件:ECPublicKeySpec_ImplTest.java   
/**
 * Test #3 for <code>ECPublicKeySpec(ECPoint, ECParameterSpec)</code> constructor<br> 
 * Assertion: throws <code>IllegalArgumentException</code> if
 * <code>w</code> is point at infinity<br>
 * Test preconditions: pass <code>ECPoint.POINT_INFINITY</code>
 * as mentioned parameter value<br>
 * Expected: must throw <code>IllegalArgumentException</code>
 */
public final void testECPublicKeySpec03() {
    // Valid (see note below) parameters set
    EllipticCurve c =
        new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
                          BigInteger.ZERO,
                          BigInteger.valueOf(4L));
    ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));

    try {
        new ECPublicKeySpec(ECPoint.POINT_INFINITY,
                new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10));
       fail("Expected IAE not thrown");
    } catch (IllegalArgumentException ok) {
    }
}
项目:freeVM    文件:ECPublicKeySpec_ImplTest.java   
/**
 * Test for <code>getParams()</code> method<br>
 * Assertion: returns associated EC parameters<br>
 * Test preconditions: <code>ECPublicKeySpec</code> instance
 * created using valid parameters<br>
 * Expected: must return params value which is equal
 * to the one passed to the constructor; (both must refer
 * the same object)
 */
public final void testGetParams() {
    // Valid (see note below) parameters set
    EllipticCurve c =
        new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
                          BigInteger.ZERO,
                          BigInteger.valueOf(4L));
    ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
    ECParameterSpec params =
        new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);

    ECPublicKeySpec ks = new ECPublicKeySpec(g, params);
    ECParameterSpec paramsRet = ks.getParams();

    assertEquals(params, paramsRet);
    assertSame(params, paramsRet);
}
项目:freeVM    文件:ECPublicKeySpec_ImplTest.java   
/**
 * Test for <code>getW()</code> method<br>
 * Assertion: returns associated public point<br>
 * Test preconditions: <code>ECPublicKeySpec</code> instance
 * created using valid parameters<br>
 * Expected: must return w value which is equal
 * to the one passed to the constructor; (both must refer
 * the same object)
 */
public final void testGetW() {
    // Valid (see note below) parameters set
    EllipticCurve c =
        new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
                          BigInteger.ZERO,
                          BigInteger.valueOf(4L));
    ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
    ECParameterSpec params =
        new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);

    ECPublicKeySpec ks = new ECPublicKeySpec(g, params);
    ECPoint wRet = ks.getW();

    assertEquals(g, wRet);
    assertSame(g, wRet);
}
项目:freeVM    文件:ECPublicKeySpec_ImplTest.java   
/**
 * Test #3 for <code>ECPublicKeySpec(ECPoint, ECParameterSpec)</code> constructor<br> 
 * Assertion: throws <code>IllegalArgumentException</code> if
 * <code>w</code> is point at infinity<br>
 * Test preconditions: pass <code>ECPoint.POINT_INFINITY</code>
 * as mentioned parameter value<br>
 * Expected: must throw <code>IllegalArgumentException</code>
 */
public final void testECPublicKeySpec03() {
    // Valid (see note below) parameters set
    EllipticCurve c =
        new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
                          BigInteger.ZERO,
                          BigInteger.valueOf(4L));
    ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));

    try {
        new ECPublicKeySpec(ECPoint.POINT_INFINITY,
                new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10));
       fail("Expected IAE not thrown");
    } catch (IllegalArgumentException ok) {
    }
}
项目:freeVM    文件:ECPublicKeySpec_ImplTest.java   
/**
 * Test for <code>getParams()</code> method<br>
 * Assertion: returns associated EC parameters<br>
 * Test preconditions: <code>ECPublicKeySpec</code> instance
 * created using valid parameters<br>
 * Expected: must return params value which is equal
 * to the one passed to the constructor; (both must refer
 * the same object)
 */
public final void testGetParams() {
    // Valid (see note below) parameters set
    EllipticCurve c =
        new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
                          BigInteger.ZERO,
                          BigInteger.valueOf(4L));
    ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
    ECParameterSpec params =
        new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);

    ECPublicKeySpec ks = new ECPublicKeySpec(g, params);
    ECParameterSpec paramsRet = ks.getParams();

    assertEquals(params, paramsRet);
    assertSame(params, paramsRet);
}
项目:freeVM    文件:ECPublicKeySpec_ImplTest.java   
/**
 * Test for <code>getW()</code> method<br>
 * Assertion: returns associated public point<br>
 * Test preconditions: <code>ECPublicKeySpec</code> instance
 * created using valid parameters<br>
 * Expected: must return w value which is equal
 * to the one passed to the constructor; (both must refer
 * the same object)
 */
public final void testGetW() {
    // Valid (see note below) parameters set
    EllipticCurve c =
        new EllipticCurve(new ECFieldFp(BigInteger.valueOf(5L)),
                          BigInteger.ZERO,
                          BigInteger.valueOf(4L));
    ECPoint g = new ECPoint(BigInteger.ZERO, BigInteger.valueOf(2L));
    ECParameterSpec params =
        new ECParameterSpec(c, g, BigInteger.valueOf(5L), 10);

    ECPublicKeySpec ks = new ECPublicKeySpec(g, params);
    ECPoint wRet = ks.getW();

    assertEquals(g, wRet);
    assertSame(g, wRet);
}
项目:holico    文件:ECDHECryptography.java   
/**
 * Called by the server. Extracts the client's public key from the encoded
 * point and then runs the specified key agreement algorithm (ECDH) to
 * generate the premaster secret.
 * 
 * @param encodedPoint
 *            the client's public key (encoded)
 * @return the premaster secret
 */
public SecretKey getSecret(byte[] encodedPoint) {
    SecretKey secretKey = null;
    try {
        // extract public key
        ECParameterSpec params = publicKey.getParams();
        ECPoint point = decodePoint(encodedPoint, params.getCurve());

        KeyFactory keyFactory = KeyFactory.getInstance(KEYPAIR_GENERATOR_INSTANCE);
        ECPublicKeySpec keySpec = new ECPublicKeySpec(point, params);
        PublicKey peerPublicKey = keyFactory.generatePublic(keySpec);

        secretKey = getSecret(peerPublicKey);

    } catch (Exception e) {
        LOG.severe("Could not generate the premaster secret.");
        e.printStackTrace();
    }
    return secretKey;
}
项目:holico    文件:ECDHServerKeyExchange.java   
/**
 * Called by the client after receiving the {@link ServerKeyExchange}
 * message and verification.
 * 
 * @return the server's ephemeral public key.
 */
public ECPublicKey getPublicKey(ECParameterSpec params) {
    if (publicKey == null) {

        try {
            point = ECDHECryptography.decodePoint(pointEncoded, params.getCurve());

            KeyFactory keyFactory = KeyFactory.getInstance(KEYPAIR_GENERATOR_INSTANCE);
            publicKey = (ECPublicKey) keyFactory.generatePublic(new ECPublicKeySpec(point, params));
        } catch (Exception e) {
            LOG.severe("Could not reconstruct the server's ephemeral public key.");
            e.printStackTrace();
        }

    }
    return publicKey;
}
项目:irma_future_id    文件:JCEECPublicKey.java   
public JCEECPublicKey(
    String              algorithm,
    org.bouncycastle.jce.spec.ECPublicKeySpec     spec)
{
    this.algorithm = algorithm;
    this.q = spec.getQ();

    if (spec.getParams() != null) // can be null if implictlyCa
    {
        ECCurve curve = spec.getParams().getCurve();
        EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getParams().getSeed());

        this.ecSpec = EC5Util.convertSpec(ellipticCurve, spec.getParams());
    }
    else
    {
        if (q.getCurve() == null)
        {
            org.bouncycastle.jce.spec.ECParameterSpec s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();

            q = s.getCurve().createPoint(q.getX().toBigInteger(), q.getY().toBigInteger(), false);
        }               
        this.ecSpec = null;
    }
}