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


Java KeyPairGenerator.getInstance方法代碼示例

本文整理匯總了Java中java.security.KeyPairGenerator.getInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java KeyPairGenerator.getInstance方法的具體用法?Java KeyPairGenerator.getInstance怎麽用?Java KeyPairGenerator.getInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.security.KeyPairGenerator的用法示例。


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

示例1: testKeyPairGeneratorEccMultipleInits

import java.security.KeyPairGenerator; //導入方法依賴的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

示例2: testKeyPairGeneratorEccKeyGenAllCurves

import java.security.KeyPairGenerator; //導入方法依賴的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

示例3: testSignature4

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
/**
 * Test signature with a SOAP message and generate ECDSA keys during the
 * test.
 */
public void testSignature4() {
    String data = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:id=\"http://x-road.eu/xsd/identifiers\" xmlns:xrd=\"http://x-road.eu/xsd/xroad.xsd\"><SOAP-ENV:Header><xrd:client id:objectType=\"SUBSYSTEM\"><id:xRoadInstance>FI</id:xRoadInstance><id:memberClass>GOV</id:memberClass><id:memberCode>MEMBER1</id:memberCode><id:subsystemCode>subsystem</id:subsystemCode></xrd:client><xrd:service id:objectType=\"SERVICE\"><id:xRoadInstance>FI</id:xRoadInstance><id:memberClass>COM</id:memberClass><id:memberCode>MEMBER2</id:memberCode><id:subsystemCode>subsystem</id:subsystemCode><id:serviceCode>getRandom</id:serviceCode><id:serviceVersion>v1</id:serviceVersion></xrd:service><xrd:userId>EE1234567890</xrd:userId><xrd:id>ID-1234567890</xrd:id><xrd:protocolVersion>4.0</xrd:protocolVersion><xrd:requestHash algorithmId=\"SHA-512\">ZPbWPAOcJxzE81EmSk//R3DUQtqwMcuMMF9tsccJypdNcukzICQtlhhr3a/bTmexDrn8e/BrBVyl2t0ni/cUvw==</xrd:requestHash></SOAP-ENV:Header><SOAP-ENV:Body><ns1:getRandomResponse xmlns:ns1=\"http://producer.x-road.ee\"><request><data>1234567890 Каллио</data></request><response><data>9876543210</data></response></ns1:getRandomResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>";
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
        keyGen.initialize(256, new SecureRandom());
        KeyPair keyPair = keyGen.generateKeyPair();
        String signature = CryptoHelper.createSignature(keyPair.getPrivate(), data, "SHA512withECDSA");
        assertEquals(true, CryptoHelper.verifySignature(keyPair.getPublic(), data, signature, "SHA512withECDSA"));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
 
開發者ID:vrk-kpa,項目名稱:xrd4j,代碼行數:17,代碼來源:CryptoHelperTest.java

示例4: createRSA256KeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
private KeyPair createRSA256KeyPair() {
    try {
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(2048);
        return generator.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}
 
開發者ID:PacktPublishing,項目名稱:OAuth-2.0-Cookbook,代碼行數:10,代碼來源:JwkKeyPairManager.java

示例5: failsWhenUidIsTooLong

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
@Test
public void failsWhenUidIsTooLong() throws Exception {
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
  keyGen.initialize(512);
  KeyPair keys = keyGen.genKeyPair();

  FixedClock clock = new FixedClock(2002L);

  FirebaseTokenFactory tokenFactory = new FirebaseTokenFactory(FACTORY, clock);

  thrown.expect(IllegalStateException.class);
  tokenFactory.createSignedCustomAuthTokenForUser(
      Strings.repeat("a", 129), ISSUER, keys.getPrivate());
}
 
開發者ID:firebase,項目名稱:firebase-admin-java,代碼行數:15,代碼來源:FirebaseTokenFactoryTest.java

示例6: generateUserKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
/**
 * Generates a random user key pair.
 *
 * @param version  The encryption version for which the key pair should be created.
 * @param password The password which should be used to secure the private key.
 *
 * @return The generated user key pair.
 *
 * @throws InvalidKeyPairException  If the version for the user key pair is not supported.
 * @throws InvalidPasswordException If the password to secure the private key is invalid.
 * @throws CryptoSystemException    If a unknown error occurred.
 */
public static UserKeyPair generateUserKeyPair(String version, String password)
        throws InvalidKeyPairException, InvalidPasswordException, CryptoSystemException {
    validateUserKeyPairVersion(version);
    validatePassword(password);

    KeyPair keyPair;
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);
        keyPair = keyGen.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        throw new CryptoSystemException("Could not generate RSA key pair. Algorithm is " +
                "missing.", e);
    }

    String privateKeyString = encryptPrivateKey(keyPair.getPrivate(), password);
    String publicKeyString = getStringFromPublicKey(keyPair.getPublic());

    UserPrivateKey userPrivateKey = new UserPrivateKey();
    userPrivateKey.setVersion(version);
    userPrivateKey.setPrivateKey(privateKeyString);

    UserPublicKey userPublicKey = new UserPublicKey();
    userPublicKey.setVersion(version);
    userPublicKey.setPublicKey(publicKeyString);

    UserKeyPair userKeyPair = new UserKeyPair();
    userKeyPair.setUserPrivateKey(userPrivateKey);
    userKeyPair.setUserPublicKey(userPublicKey);

    return userKeyPair;
}
 
開發者ID:michaelnetter,項目名稱:dracoon-dropzone,代碼行數:45,代碼來源:Crypto.java

示例7: generateRSAKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
/**
 * 隨機生成RSA密鑰對
 *
 * @param keyLength 密鑰長度,範圍:512~2048<br>
 * 一般1024
 */
public static KeyPair generateRSAKeyPair(int keyLength) {
    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
        kpg.initialize(keyLength);
        return kpg.genKeyPair();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}
 
開發者ID:imliujun,項目名稱:LJFramework,代碼行數:17,代碼來源:RSAUtils.java

示例8: buildKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
private KeyPair buildKeyPair() {
    try {
        final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
        return keyGen.generateKeyPair();
    } catch (final NoSuchAlgorithmException e) {
        throw new TechnicalException(e);
    }
}
 
開發者ID:yaochi,項目名稱:pac4j-plus,代碼行數:9,代碼來源:ECSignatureConfigurationTests.java

示例9: generateKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
public void generateKeyPair() throws Exception {
    KeyPairGenerator psrssKeyGen = KeyPairGenerator.getInstance("PSRSS", new WPProvider());
    psrssKeyGen.initialize(512);
    KeyPair keyPair = psrssKeyGen.generateKeyPair();
    assertEquals(512, ((PSRSSPrivateKey) keyPair.getPrivate()).getKey().bitLength());
    assertEquals(512, ((PSRSSPublicKey) keyPair.getPublic()).getKey().bitLength());

    psrssKeyGen.initialize(513);
    keyPair = psrssKeyGen.generateKeyPair();
    assertEquals(513, ((PSRSSPrivateKey) keyPair.getPrivate()).getKey().bitLength());
    assertEquals(513, ((PSRSSPublicKey) keyPair.getPublic()).getKey().bitLength());
}
 
開發者ID:woefe,項目名稱:xmlrss,代碼行數:13,代碼來源:PSRSSKeyPairGeneratorTest.java

示例10: checkParam

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
private static void checkParam(AlgorithmParameters param,
        DSAGenParameterSpec genParam) throws InvalidParameterSpecException,
                NoSuchAlgorithmException, NoSuchProviderException,
                InvalidAlgorithmParameterException {
    String algorithm = param.getAlgorithm();
    if (!algorithm.equalsIgnoreCase(ALGORITHM_NAME)) {
        throw new RuntimeException(
                "Unexpected type of parameters: " + algorithm);
    }

    DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
    int valueL = spec.getP().bitLength();
    int strengthP = genParam.getPrimePLength();
    if (strengthP != valueL) {
        System.out.printf("P: Expected %d but actual %d%n", strengthP,
                valueL);
        throw new RuntimeException("Wrong P strength");
    }

    int valueN = spec.getQ().bitLength();
    int strengthQ = genParam.getSubprimeQLength();
    if (strengthQ != valueN) {
        System.out.printf("Q: Expected %d but actual %d%n", strengthQ,
                valueN);
        throw new RuntimeException("Wrong Q strength");
    }

    if (genParam.getSubprimeQLength() != genParam.getSeedLength()) {
        System.out.println("Defaut seed length should be the same as Q.");
        throw new RuntimeException("Wrong seed length");
    }

    // use the parameters to generate real DSA keys
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_NAME,
            PROVIDER_NAME);
    keyGen.initialize(spec);
    keyGen.generateKeyPair();
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:39,代碼來源:TestDSAGenParameterSpec.java

示例11: generateKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
public static BrowserIDKeyPair generateKeyPair(final int keysize) throws NoSuchAlgorithmException {
  final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
  keyPairGenerator.initialize(keysize);
  final KeyPair keyPair = keyPairGenerator.generateKeyPair();
  RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  return new BrowserIDKeyPair(new RSASigningPrivateKey(privateKey), new RSAVerifyingPublicKey(publicKey));
}
 
開發者ID:mozilla-mobile,項目名稱:FirefoxData-android,代碼行數:9,代碼來源:RSACryptoImplementation.java

示例12: generateKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
public static KeyPair generateKeyPair(String algorithm) throws NoSuchAlgorithmException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
    keyGen.initialize(1024);
    return keyGen.genKeyPair();
}
 
開發者ID:YMCoding,項目名稱:kafka-0.11.0.0-src-with-comment,代碼行數:6,代碼來源:TestSslUtils.java

示例13: main

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    Locale reservedLocale = Locale.getDefault();
    try {
        // first test if HumanInputStream really acts like a human being
        HumanInputStream.test();
        KeyToolTest t = new KeyToolTest();

        if (System.getProperty("file") != null) {
            t.sqeTest();
            t.testAll();
            t.i18nTest();
            t.v3extTest("RSA");
            t.v3extTest("DSA");
            boolean testEC = true;
            try {
                KeyPairGenerator.getInstance("EC");
            } catch (NoSuchAlgorithmException nae) {
                testEC = false;
            }
            if (testEC) t.v3extTest("EC");
        }

        if (System.getProperty("nss") != null) {
            t.srcP11Arg = NSS_SRC_P11_ARG;
            t.p11Arg = NSS_P11_ARG;

            t.testPKCS11();

            // FAIL:
            // 1. we still don't have srcprovidername yet
            // 2. cannot store privatekey into NSS keystore
            //    java.security.KeyStoreException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_TEMPLATE_INCOMPLETE.
            //t.testPKCS11ImportKeyStore();

            t.i18nPKCS11Test();
            //FAIL: currently PKCS11-NSS does not support 2 NSS KeyStores to be loaded at the same time
            //t.sszzTest();
        }

        if (System.getProperty("solaris") != null) {
            // For Solaris Cryptography Framework
            t.srcP11Arg = SUN_SRC_P11_ARG;
            t.p11Arg = SUN_P11_ARG;
            t.testPKCS11();
            t.testPKCS11ImportKeyStore();
            t.i18nPKCS11Test();
        }

        System.out.println("Test pass!!!");
    } finally {
        // restore the reserved locale
        Locale.setDefault(reservedLocale);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:55,代碼來源:KeyToolTest.java

示例14: main

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    if (initSecmod() == false) {
        return;
    }

    String configName = BASE + SEP + "nsscrypto.cfg";
    Provider p = getSunPKCS11(configName);

    if (args.length > 1 && "sm".equals(args[0])) {
        System.setProperty("java.security.policy",
                BASE + File.separator + args[1]);
        System.setSecurityManager(new SecurityManager());
    }

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
    KeyPair kp = kpg.generateKeyPair();

    System.out.println(kp.getPublic());
    System.out.println(kp.getPrivate());

    byte[] data = generateData(2048);

    Signature sig = Signature.getInstance("SHA1withRSA", p);
    sig.initSign(kp.getPrivate());

    sig.update(data);
    byte[] s = sig.sign();
    System.out.println("signature: " + toString(s));

    sig.initVerify(kp.getPublic());
    sig.update(data);
    boolean ok = sig.verify(s);
    if (ok == false) {
        throw new Exception("Signature verification failed");
    }

    System.out.println("OK");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:39,代碼來源:Crypto.java

示例15: testECDHKeyAgreement

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
@Test
public void testECDHKeyAgreement()
    throws NoSuchProviderException, NoSuchAlgorithmException,
           InvalidParameterSpecException, InvalidKeyException,
           InvalidAlgorithmParameterException {

    /* initialize key pair generator */
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC", "wolfJCE");
    ECGenParameterSpec ecsp = new ECGenParameterSpec("secp256r1");
    keyGen.initialize(ecsp);

    KeyAgreement aKeyAgree = KeyAgreement.getInstance("ECDH", "wolfJCE");
    KeyAgreement bKeyAgree = KeyAgreement.getInstance("ECDH", "wolfJCE");

    KeyPair aPair = keyGen.generateKeyPair();
    KeyPair bPair = keyGen.generateKeyPair();

    aKeyAgree.init(aPair.getPrivate());
    bKeyAgree.init(bPair.getPrivate());

    aKeyAgree.doPhase(bPair.getPublic(), true);
    bKeyAgree.doPhase(aPair.getPublic(), true);

    byte secretA[] = aKeyAgree.generateSecret();
    byte secretB[] = bKeyAgree.generateSecret();

    assertArrayEquals(secretA, secretB);

    /* now, try reusing the A object without calling init() again */
    KeyAgreement cKeyAgree = KeyAgreement.getInstance("ECDH", "wolfJCE");
    KeyPair cPair = keyGen.generateKeyPair();
    cKeyAgree.init(cPair.getPrivate());

    aKeyAgree.doPhase(cPair.getPublic(), true);
    cKeyAgree.doPhase(aPair.getPublic(), true);

    byte secretA2[] = aKeyAgree.generateSecret();
    byte secretC[]  = cKeyAgree.generateSecret();

    assertArrayEquals(secretA2, secretC);
}
 
開發者ID:wolfSSL,項目名稱:wolfcrypt-jni,代碼行數:42,代碼來源:WolfCryptKeyAgreementTest.java


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