當前位置: 首頁>>代碼示例>>Java>>正文


Java ECGenParameterSpec類代碼示例

本文整理匯總了Java中java.security.spec.ECGenParameterSpec的典型用法代碼示例。如果您正苦於以下問題:Java ECGenParameterSpec類的具體用法?Java ECGenParameterSpec怎麽用?Java ECGenParameterSpec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ECGenParameterSpec類屬於java.security.spec包,在下文中一共展示了ECGenParameterSpec類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testEciesBasic

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
/**
 * Check that key agreement using ECIES works. This example does not specify an IESParametersSpec.
 * BouncyCastle v.1.52 uses the following algorithms: KDF2 with SHA1 for the key derivation
 * AES-CBC with PKCS #5 padding. HMAC-SHA1 with a 20 byte digest. The AES and the HMAC key are
 * both 128 bits.
 */
@SuppressWarnings("InsecureCryptoUsage")
@Test
public void testEciesBasic() throws Exception {
  ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
  KeyPairGenerator kf = KeyPairGenerator.getInstance("EC");
  kf.initialize(ecSpec);
  KeyPair keyPair = kf.generateKeyPair();
  PrivateKey priv = keyPair.getPrivate();
  PublicKey pub = keyPair.getPublic();
  byte[] message = "Hello".getBytes("UTF-8");
  Cipher ecies = Cipher.getInstance("ECIESwithAES-CBC");
  ecies.init(Cipher.ENCRYPT_MODE, pub);
  byte[] ciphertext = ecies.doFinal(message);
  System.out.println("testEciesBasic:" + TestUtil.bytesToHex(ciphertext));
  ecies.init(Cipher.DECRYPT_MODE, priv, ecies.getParameters());
  byte[] decrypted = ecies.doFinal(ciphertext);
  assertEquals(TestUtil.bytesToHex(message), TestUtil.bytesToHex(decrypted));
}
 
開發者ID:google,項目名稱:wycheproof,代碼行數:25,代碼來源:EciesTest.java

示例2: ServerHandshakePacket

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
public ServerHandshakePacket(BedrockPlayer p) {
    super(PacketRegistry.NetworkType.SERVER_TO_CLIENT_HANDSHAKE_PACKET);

    try {
        KeyPairGenerator generator = KeyPairGenerator.getInstance("EC");
        generator.initialize(new ECGenParameterSpec("secp384r1"));
        KeyPair serverKeyPair = generator.generateKeyPair();

        byte[] token = Security.generateRandomToken();
        byte[] serverKey = Security.getServerKey(serverKeyPair, token);
        p.enableEncryption(serverKey);

        publicKey = serverKeyPair;
        serverToken = token;
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException e) {
        e.printStackTrace();
    }
}
 
開發者ID:KernelFreeze,項目名稱:BedrockProxy,代碼行數:19,代碼來源:ServerHandshakePacket.java

示例3: initialize

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
public void initialize(
    int strength,
    SecureRandom random)
{
    this.random = random;

    if (ecParams != null)
    {
        try
        {
            initialize((ECGenParameterSpec)ecParams, random);
        }
        catch (InvalidAlgorithmParameterException e)
        {
            throw new InvalidParameterException("key size not configurable.");
        }
    }
    else
    {
        throw new InvalidParameterException("unknown key size.");
    }
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:23,代碼來源:KeyPairGeneratorSpi.java

示例4: initialize

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
public void initialize(
    int             strength,
    SecureRandom    random)
{
    this.strength = strength;
    this.random = random;
    ECGenParameterSpec ecParams = (ECGenParameterSpec)ecParameters.get(Integers.valueOf(strength));

    if (ecParams != null)
    {
        try
        {
            initialize(ecParams, random);
        }
        catch (InvalidAlgorithmParameterException e)
        {
            throw new InvalidParameterException("key size not configurable.");
        }
    }
    else
    {
        throw new InvalidParameterException("unknown key size.");
    }
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:25,代碼來源:KeyPairGeneratorSpi.java

示例5: initialize

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
public void initialize(
    int strength,
    SecureRandom random)
{
    this.strength = strength;
    this.random = random;

    if (ecParams != null)
    {
        try
        {
            initialize((ECGenParameterSpec)ecParams, random);
        }
        catch (InvalidAlgorithmParameterException e)
        {
            throw new InvalidParameterException("key size not configurable.");
        }
    }
    else
    {
        throw new InvalidParameterException("unknown key size.");
    }
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:24,代碼來源:KeyPairGeneratorSpi.java

示例6: getPublicKey

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
/**
 * 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;
	}
}
 
開發者ID:V2GClarity,項目名稱:RISE-V2G,代碼行數:31,代碼來源:SecurityUtils.java

示例7: getPrivateKey

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
/**
 * Returns the ECPrivateKey instance from its raw bytes. Note that you must provide the "s" value of the 
 * private key, not e.g. the byte array from reading a PKCS#8 key file.
 * 
 * @param privateKeyBytes The byte array (the "s" value) of the private key
 * @return The ECPrivateKey instance
 */
public static ECPrivateKey getPrivateKey(byte[] privateKeyBytes) {
	try {
		AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
		parameters.init(new ECGenParameterSpec("secp256r1"));
		
		ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);
		ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(new BigInteger(privateKeyBytes), ecParameterSpec);
		
		ECPrivateKey privateKey = (ECPrivateKey) KeyFactory.getInstance("EC").generatePrivate(ecPrivateKeySpec);

		return privateKey;
	} catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException e) {
		getLogger().error(e.getClass().getSimpleName() + " occurred when trying to get private key from raw bytes", e);
		return null;
	}
}
 
開發者ID:V2GClarity,項目名稱:RISE-V2G,代碼行數:24,代碼來源:SecurityUtils.java

示例8: initialize

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
@Override
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    if (params instanceof ECParameterSpec) {
        this.params = ECUtil.getECParameterSpec(null,
                                                (ECParameterSpec)params);
        if (this.params == null) {
            throw new InvalidAlgorithmParameterException(
                "Unsupported curve: " + params);
        }
    } else if (params instanceof ECGenParameterSpec) {
        String name = ((ECGenParameterSpec)params).getName();
        this.params = ECUtil.getECParameterSpec(null, name);
        if (this.params == null) {
            throw new InvalidAlgorithmParameterException(
                "Unknown curve name: " + name);
        }
    } else {
        throw new InvalidAlgorithmParameterException(
            "ECParameterSpec or ECGenParameterSpec required for EC");
    }
    this.keySize =
        ((ECParameterSpec)this.params).getCurve().getField().getFieldSize();
    this.random = random;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:27,代碼來源:ECKeyPairGenerator.java

示例9: getEccCurveNameFromSpec

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
@Test
public void getEccCurveNameFromSpec()
    throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {

    /* generate key pair */
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
    ECGenParameterSpec genSpec = new ECGenParameterSpec("secp256r1");
    kpg.initialize(genSpec);

    KeyPair pair = kpg.genKeyPair();
    ECPrivateKey priv = (ECPrivateKey)pair.getPrivate();

    ECParameterSpec spec = priv.getParams();

    String curveName = Ecc.getCurveName(spec);

    assertEquals(curveName, "SECP256R1");
}
 
開發者ID:wolfSSL,項目名稱:wolfcrypt-jni,代碼行數:19,代碼來源:EccTest.java

示例10: testKeyPairGeneratorEccKeyGenAllCurves

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
@Test
public void testKeyPairGeneratorEccKeyGenAllCurves()
    throws NoSuchProviderException, NoSuchAlgorithmException,
           InvalidAlgorithmParameterException {

    /* try generating keys for all supported curves */
    for (int i = 0; i < enabledCurves.size(); i++) {

        KeyPairGenerator kpg =
            KeyPairGenerator.getInstance("EC", "wolfJCE");

        ECGenParameterSpec ecSpec =
            new ECGenParameterSpec(enabledCurves.get(i));
        kpg.initialize(ecSpec);

        KeyPair kp = kpg.generateKeyPair();
    }
}
 
開發者ID:wolfSSL,項目名稱:wolfcrypt-jni,代碼行數:19,代碼來源:WolfCryptKeyPairGeneratorTest.java

示例11: testKeyPairGeneratorEccMultipleInits

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
@Test
public void testKeyPairGeneratorEccMultipleInits()
    throws NoSuchProviderException, NoSuchAlgorithmException,
           InvalidAlgorithmParameterException {

    if (enabledCurves.size() > 0) {

        KeyPairGenerator kpg =
            KeyPairGenerator.getInstance("EC", "wolfJCE");

        ECGenParameterSpec ecSpec =
            new ECGenParameterSpec(enabledCurves.get(0));

        kpg.initialize(ecSpec);
        kpg.initialize(ecSpec);
    }
}
 
開發者ID:wolfSSL,項目名稱:wolfcrypt-jni,代碼行數:18,代碼來源:WolfCryptKeyPairGeneratorTest.java

示例12: testKeyPairGeneratorEccMultipleKeyGen

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
@Test
public void testKeyPairGeneratorEccMultipleKeyGen()
    throws NoSuchProviderException, NoSuchAlgorithmException,
           InvalidAlgorithmParameterException {

    if (enabledCurves.size() > 0) {

        KeyPairGenerator kpg =
            KeyPairGenerator.getInstance("EC", "wolfJCE");

        ECGenParameterSpec ecSpec =
            new ECGenParameterSpec(enabledCurves.get(0));
        kpg.initialize(ecSpec);

        KeyPair kp1 = kpg.generateKeyPair();
        KeyPair kp2 = kpg.generateKeyPair();
    }
}
 
開發者ID:wolfSSL,項目名稱:wolfcrypt-jni,代碼行數:19,代碼來源:WolfCryptKeyPairGeneratorTest.java

示例13: testKeyPairGeneratorEccNewKeyFromExisting

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
@Test
public void testKeyPairGeneratorEccNewKeyFromExisting()
    throws NoSuchProviderException, NoSuchAlgorithmException,
           InvalidAlgorithmParameterException, InvalidKeySpecException {

    if (enabledCurves.size() > 0) {

        KeyPairGenerator kpg =
            KeyPairGenerator.getInstance("EC", "wolfJCE");

        ECGenParameterSpec ecSpec =
            new ECGenParameterSpec(enabledCurves.get(0));
        kpg.initialize(ecSpec);

        KeyPair kp = kpg.generateKeyPair();

        KeyFactory kf = KeyFactory.getInstance("EC");
        PublicKey pub = kf.generatePublic(new X509EncodedKeySpec(
                    kp.getPublic().getEncoded()));
        PrivateKey priv = kf.generatePrivate(new PKCS8EncodedKeySpec(
                    kp.getPrivate().getEncoded()));
    }
}
 
開發者ID:wolfSSL,項目名稱:wolfcrypt-jni,代碼行數:24,代碼來源:WolfCryptKeyPairGeneratorTest.java

示例14: isAvailableCurve

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
private static boolean isAvailableCurve(int curveId) {
    String oid = idToOidMap.get(curveId);
    if (oid != null) {
        AlgorithmParameters params = null;
        try {
            params = JsseJce.getAlgorithmParameters("EC");
            params.init(new ECGenParameterSpec(oid));
        } catch (Exception e) {
            return false;
        }

        // cache the parameters
        idToParams.put(curveId, params);

        return true;
    }

    return false;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:20,代碼來源:SupportedEllipticCurvesExtension.java

示例15: generateEcKeyPair

import java.security.spec.ECGenParameterSpec; //導入依賴的package包/類
@Override
public KeyPair generateEcKeyPair(final EcCurve curveName) throws NoSuchAlgorithmException,
                                                                 InvalidAlgorithmParameterException {
	if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
		Security.addProvider(new BouncyCastleProvider());
	}
	KeyPairGenerator kpg;
	try {
		kpg = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME); //$NON-NLS-1$
	}
	catch (final Exception e) {
		Logger.getLogger("es.gob.jmulticard").warning( //$NON-NLS-1$
			"No se ha podido obtener un generador de pares de claves de curva eliptica con SpongyCastle, se usara el generador por defecto: " + e //$NON-NLS-1$
		);
		kpg = KeyPairGenerator.getInstance("EC"); //$NON-NLS-1$
	}

	Logger.getLogger("es.gob.jmulticard").info( //$NON-NLS-1$
		"Seleccionado el siguiente generador de claves de curva eliptica: " + kpg.getClass().getName() //$NON-NLS-1$
	);

	final AlgorithmParameterSpec parameterSpec = new ECGenParameterSpec(curveName.toString());
	kpg.initialize(parameterSpec);
	return kpg.generateKeyPair();
}
 
開發者ID:MiFirma,項目名稱:mi-firma-android,代碼行數:26,代碼來源:JseCryptoHelper.java


注:本文中的java.security.spec.ECGenParameterSpec類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。