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


Java KeyPairGenerator.generateKeyPair方法代碼示例

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


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

示例1: createNewKeys

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
private static KeyPair createNewKeys(Context ctx, String alias) {
    KeyPair keyPair = null;
    try {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        end.add(Calendar.YEAR, 1);
        KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx)
                .setAlias(alias)
                .setSubject(new X500Principal("CN=" + alias))
                .setSerialNumber(BigInteger.ONE)
                .setStartDate(start.getTime())
                .setEndDate(end.getTime())
                .build();
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", keyStoreInstance);
        generator.initialize(spec);
        keyPair = generator.generateKeyPair();
    } catch (Exception e) {
        Toast.makeText(ctx, "Exception " + e.getMessage() + " occured", Toast.LENGTH_LONG).show();
        Log.e(TAG, Log.getStackTraceString(e));
    }
    return keyPair;
}
 
開發者ID:ceanyd,項目名稱:react-native-caller-id-android,代碼行數:23,代碼來源:DataBase.java

示例2: generateKey

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
/**
 * The method that will create both the public and private key used to encrypt and decrypt the data.
 * 
 * @param publicKeyOutput
 * 		The path of where the public key will be created.
 * 
 * @param privateKeyOutput
 * 		The path of where the private key will be created.
 * 
 * @return {@code true} If this operation was successful, otherwise {@code false}.
 */
public static boolean generateKey(String publicKeyOutput, String privateKeyOutput) {
	try {
		final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
		keyGen.initialize(2048);

		final KeyPair key = keyGen.generateKeyPair();

		try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(publicKeyOutput)))) {
			dos.write(key.getPublic().getEncoded());
		}

		try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(privateKeyOutput)))) {
			dos.write(key.getPrivate().getEncoded());
		}
		
		return true;

	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return false;
}
 
開發者ID:nshusa,項目名稱:crypto-utils,代碼行數:35,代碼來源:RSAUtils.java

示例3: main

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
public static void main(String args[]) throws Exception {
    for (int i=0; i<10000; i++) {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
        KeyPair kp = kpg.generateKeyPair();
        DSAPrivateKey dpk = (DSAPrivateKey)kp.getPrivate();
        int len = dpk.getX().bitLength();
        if (len <= 152) {
            if (!use(kp)) {
                String os = System.getProperty("os.name");
                // Solaris bug, update the following line once it's fixed
                if (os.equals("SunOS")) {
                    throw new IllegalStateException(
                            "Don't panic. This is a Solaris bug");
                } else {
                    throw new RuntimeException("Real test failure");
                }
            }
            break;
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:SolarisShortDSA.java

示例4: testKeyPairGeneratorDhInitializeWithParamSpec

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

    int testDHKeySizes[] = { 512, 1024, 2048 };

    for (int i = 0; i < testDHKeySizes.length; i++) {

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

        DHParameterSpec spec = new DHParameterSpec(
                new BigInteger(prime),
                new BigInteger(base),
                testDHKeySizes[i]);

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

示例5: generateKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
/**
 * Generates RSA KeyPair
 */
public static KeyPair generateKeyPair()
{
    try
    {
        KeyPairGenerator keypairgenerator = KeyPairGenerator.getInstance("RSA");
        keypairgenerator.initialize(1024);
        return keypairgenerator.generateKeyPair();
    }
    catch (NoSuchAlgorithmException nosuchalgorithmexception)
    {
        nosuchalgorithmexception.printStackTrace();
        LOGGER.error("Key pair generation failed!");
        return null;
    }
}
 
開發者ID:SkidJava,項目名稱:BaseClient,代碼行數:19,代碼來源:CryptManager.java

示例6: init

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
private static void init() {
	try {
		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
		cipher = Cipher.getInstance(INSTANCE_TYPE);
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
		generator.initialize(1024);
		KeyPair pair = generator.generateKeyPair();
		PUBLIC_KEY = org.apache.commons.codec.binary.Base64.encodeBase64String(pair.getPublic().getEncoded());
		privKey = pair.getPrivate();
	} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) {
		e.printStackTrace();
		Log.e(e.getMessage());
	}
}
 
開發者ID:DSM-DMS,項目名稱:DMS,代碼行數:15,代碼來源:RSA.java

示例7: PaillierCryptosystemUtil

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
/**
 * The constructor PaillierCryptosystemUtil is responsible for adding the
 * Paillier Provider, initialise the key size and generate public and
 * private keys. This is done once only here with the clear idea to be used
 * the same keys for all encryption and decryption operations within the
 * class. The instance of PaillierCryptosystemUtil is made just once from
 * the main class, this way we assure that the homomorphic properties of
 * Paillier Cryptosystem will be in use.
 * 
 * @throws NoSuchAlgorithmException
 * 
 */
PaillierCryptosystemUtil() throws NoSuchAlgorithmException {

	// Add dynamically the desired provider
	Security.addProvider(new PaillierProvider());
	// generates pairs of public and private keys
	KeyPairGenerator kpg = KeyPairGenerator.getInstance("Paillier");
	// Initialise key size in bits
	kpg.initialize(32);// 8 bits = 1 byte
	KeyPair keyPair = kpg.generateKeyPair();
	pubKey = keyPair.getPublic();
	privKey = keyPair.getPrivate();

}
 
開發者ID:peterstefanov,項目名稱:paillier,代碼行數:26,代碼來源:PaillierCryptosystemUtil.java

示例8: createKeysJBMR2

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
static void createKeysJBMR2(Context context, String alias)
        throws NoSuchProviderException, NoSuchAlgorithmException,
        InvalidAlgorithmParameterException {

    Calendar start = new GregorianCalendar();
    Calendar end = new GregorianCalendar();
    end.add(Calendar.YEAR, 30);

    KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
            // You'll use the alias later to retrieve the key. It's a key
            // for the key!
            .setAlias(alias)
            .setSubject(new X500Principal("CN=" + alias))
            .setSerialNumber(BigInteger.valueOf(Math.abs(alias.hashCode())))
            // Date range of validity for the generated pair.
            .setStartDate(start.getTime())
            .setEndDate(end.getTime())
            .build();

    KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance(SecurityConstants.TYPE_RSA,
            SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
    kpGenerator.initialize(spec);
    KeyPair kp = kpGenerator.generateKeyPair();
    Log.d(TAG, "Public Key is: " + kp.getPublic().toString());
}
 
開發者ID:ronghao,項目名稱:CacheManage,代碼行數:27,代碼來源:KeyStoreHelper.java

示例9: generateKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
private KeyPair generateKeyPair(String algorithm)
        throws NoSuchAlgorithmException {
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(algorithm);
    keyGenerator.initialize(512);
    KeyPair keyPair = keyGenerator.generateKeyPair();
    return keyPair;
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:8,代碼來源:DigitalSignatureValidatorTest.java

示例10: generateEphemeralKeys

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
/**
 * Generates a random key pair based on a curve
 * @param curve the curve to use
 * @return a randomly generated KeyPair
 */
public static KeyPair generateEphemeralKeys(EllipticCurveParameters curve) {
    try {
        final ECParameterSpec curveParams = EllipticCurveParameters.encodeECParameterSpec(curve);
        final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
        keyPairGenerator.initialize(curveParams);

        return keyPairGenerator.generateKeyPair();
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }

    return null;
}
 
開發者ID:mDL-ILP,項目名稱:mDL-ILP,代碼行數:19,代碼來源:ECCUtils.java

示例11: initKey

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
/**
 * 初始化密鑰
 *
 * @return
 * @throws Exception
 */
public static Map<String, Object> initKey() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    keyPairGenerator.initialize(512);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    // 公鑰
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    // 私鑰
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

    Map<String, Object> keyMap = new HashMap<String, Object>(2);
    keyMap.put("PublicKey", publicKey);
    keyMap.put("PrivateKey", privateKey);
    return keyMap;
}
 
開發者ID:abook23,項目名稱:godlibrary,代碼行數:21,代碼來源:RSAUtlis.java

示例12: genKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
/**
 * 生成RSA密鑰對
 */
public static void genKeyPair() {
	KeyPairGenerator keyPairGenerator = null;
	try {
		keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	}
	keyPairGenerator.initialize(KEYSIZE);
	KeyPair keyPair = keyPairGenerator.generateKeyPair();
	PRIVATE_KEY = keyPair.getPrivate();
	PUBLIC_KEY = keyPair.getPublic();
}
 
開發者ID:sheefee,項目名稱:simple-sso,代碼行數:16,代碼來源:RSAUtil.java

示例13: getRSAKeyPair

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
private static KeyPair getRSAKeyPair() {
	try {
		KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
		keyGen.initialize(2048);
		return keyGen.generateKeyPair();
	} catch (Exception e) {
		System.out.println("Unable to initialize RSA key pair.");
		e.printStackTrace();
		return null;
	}
}
 
開發者ID:gahana,項目名稱:edge-jwt-sample,代碼行數:12,代碼來源:JWTUtil.java

示例14: main

import java.security.KeyPairGenerator; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
    keyPairGenerator.initialize(1024);
    KeyPair keys = keyPairGenerator.generateKeyPair();
    PublicKey publicKey = keys.getPublic();
    byte[] sigBytes = new byte[100];

    Signature signature = Signature.getInstance("SHA1withDSA");
    signature.initVerify(publicKey);
    try {
        signature.verify(sigBytes, Integer.MAX_VALUE, 1);
    } catch (IllegalArgumentException ex) {
        // Expected
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:VerifyRangeCheckOverflow.java

示例15: 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,代碼來源:ECEncryptionConfigurationTests.java


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