当前位置: 首页>>代码示例>>Java>>正文


Java KeyPairGeneratorSpec类代码示例

本文整理汇总了Java中android.security.KeyPairGeneratorSpec的典型用法代码示例。如果您正苦于以下问题:Java KeyPairGeneratorSpec类的具体用法?Java KeyPairGeneratorSpec怎么用?Java KeyPairGeneratorSpec使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


KeyPairGeneratorSpec类属于android.security包,在下文中一共展示了KeyPairGeneratorSpec类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createKeys

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
/**
 * Creates a public and private key and stores it using the AndroidKeyStore,
 * so that only this application will be able to access the keys.
 */
@SuppressWarnings("deprecation")
public void createKeys() throws Exception {
    KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
    keyStore.load(null);
    if (keyStore.containsAlias(alias)) {
        Log.d(TAG, "[containsAlias]");
        return;
    }

    Calendar start = Calendar.getInstance();
    Calendar end = Calendar.getInstance();
    end.add(Calendar.YEAR, 30);
    KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
        .setAlias(alias)
        .setSubject(new X500Principal("CN=" + alias))
        .setSerialNumber(BigInteger.TEN)
        .setStartDate(start.getTime())
        .setEndDate(end.getTime())
        .build();
    KeyPairGenerator generator = KeyPairGenerator.getInstance(TYPE_RSA, ANDROID_KEY_STORE);
    generator.initialize(spec);
    KeyPair keyPair = generator.generateKeyPair();
    Log.d(TAG, "Public Key is: " + keyPair.getPublic().toString());
}
 
开发者ID:drakeet,项目名称:rebase-android,代码行数:29,代码来源:BlackBox.java

示例2: generateAsymmetricKeyPair

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
private static void generateAsymmetricKeyPair() throws SecureStorageException {
    try {
        if (isRTL()) {
            Locale.setDefault(Locale.US);
        }

        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        end.add(Calendar.YEAR, 99);

        KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context.get())
                .setAlias(KEY_ALIAS)
                .setSubject(new X500Principal(KEY_X500PRINCIPAL))
                .setSerialNumber(BigInteger.TEN)
                .setStartDate(start.getTime())
                .setEndDate(end.getTime())
                .build();

        KeyPairGenerator generator
                = KeyPairGenerator.getInstance(KEY_ENCRYPTION_ALGORITHM, KEY_KEYSTORE_NAME);
        generator.initialize(spec);
        generator.generateKeyPair();
    } catch (Exception e) {
        throw new SecureStorageException(e.getMessage(), e, KEYSTORE_EXCEPTION);
    }
}
 
开发者ID:adorsys,项目名称:secure-storage-android,代码行数:27,代码来源:KeystoreTool.java

示例3: createKeysJBMR2

import android.security.KeyPairGeneratorSpec; //导入依赖的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

示例4: generateKeyPair

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static void generateKeyPair(Context context, String alias)
        throws GeneralSecurityException {
    final Calendar start = new GregorianCalendar();
    final Calendar end = new GregorianCalendar();
    end.add(Calendar.YEAR, 100);
    final KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
    final KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
            .setAlias(alias)
            .setSubject(new X500Principal("CN=" + alias))
            .setSerialNumber(BigInteger.ONE)
            .setStartDate(start.getTime())
            .setEndDate(end.getTime())
            .build();
    gen.initialize(spec);
    gen.generateKeyPair();
}
 
开发者ID:privacyidea,项目名称:privacyidea-authenticator,代码行数:18,代码来源:SecretKeyWrapper.java

示例5: createNewKeys

import android.security.KeyPairGeneratorSpec; //导入依赖的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

示例6: generateNewKey

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
private void generateNewKey(@NonNull Context context, @NonNull String alias) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
    Calendar notBefore = Calendar.getInstance();
    Calendar notAfter = Calendar.getInstance();
    notAfter.add(Calendar.YEAR, 1);

    KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
            .setAlias(alias)
            .setSubject(new X500Principal("CN=zerokit"))
            .setSerialNumber(BigInteger.ONE)
            .setStartDate(notBefore.getTime())
            .setEndDate(notAfter.getTime())
            .build();
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", ANDROID_KEYSTORE);
    generator.initialize(spec);
    generator.generateKeyPair();
}
 
开发者ID:tresorit,项目名称:ZeroKit-Android-SDK,代码行数:17,代码来源:Zerokit.java

示例7: initializeKeystore

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
protected void initializeKeystore() throws KeyStoreException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
    // Create new key if needed
    if (!keyStore.containsAlias(keyAlias)) {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        end.add(Calendar.YEAR, 1);
        KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
                .setAlias(keyAlias)
                .setSubject(new X500Principal("CN=Sample Name, O=Android Authority"))
                .setSerialNumber(BigInteger.ONE)
                .setStartDate(start.getTime())
                .setEndDate(end.getTime())
                .build();
        KeyPairGenerator generator = KeyPairGenerator.getInstance(KEYPAIR_ALGO, ANDROID_KEY_STORE);
        generator.initialize(spec);
        generator.generateKeyPair();
    }
}
 
开发者ID:MobileTribe,项目名称:pandroid,代码行数:20,代码来源:RsaAesCryptoManager.java

示例8: createKeysJBMR2

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private 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:plusend,项目名称:DiyCode,代码行数:27,代码来源:KeyStoreHelper.java

示例9: createNewKey

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
public void createNewKey() {
    try {
        //new key if needed
        if (!keyStore.containsAlias(alias)) {

            KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM, KEY_PROVIDER);

            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            end.add(Calendar.YEAR, 1);
            kpg.initialize(new KeyPairGeneratorSpec.Builder(mContext)
                    .setAlias(alias)
                    .setSubject(new X500Principal(String.format("CN=%s, OU=%s", alias, mContext.getPackageName())))
                    .setSerialNumber(BigInteger.ONE)
                    .setStartDate(start.getTime())
                    .setEndDate(end.getTime())
                    .build()
            );

            KeyPair keyPair = kpg.generateKeyPair();
        }
    } catch (Exception e) {
        Log.e(TAG, Log.getStackTraceString(e));
    }
}
 
开发者ID:ivoribeiro,项目名称:AndroidQuiz,代码行数:26,代码来源:SecurityManager.java

示例10: shouldThrowOnNoSuchProviderErrorWhenTryingToObtainRSAKeys

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@Test
public void shouldThrowOnNoSuchProviderErrorWhenTryingToObtainRSAKeys() throws Exception {
    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
    exception.expect(KeyException.class);
    exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");

    PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
    KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
    KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
    PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);

    PowerMockito.mockStatic(KeyPairGenerator.class);
    PowerMockito.when(KeyPairGenerator.getInstance(ALGORITHM_RSA, ANDROID_KEY_STORE))
            .thenThrow(new NoSuchProviderException());

    cryptoUtil.getRSAKeyEntry();
}
 
开发者ID:auth0,项目名称:Auth0.Android,代码行数:18,代码来源:CryptoUtilTest.java

示例11: shouldThrowOnNoSuchAlgorithmErrorWhenTryingToObtainRSAKeys

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@Test
public void shouldThrowOnNoSuchAlgorithmErrorWhenTryingToObtainRSAKeys() throws Exception {
    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
    exception.expect(KeyException.class);
    exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");

    PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
    KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
    KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
    PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);

    PowerMockito.mockStatic(KeyPairGenerator.class);
    PowerMockito.when(KeyPairGenerator.getInstance(ALGORITHM_RSA, ANDROID_KEY_STORE))
            .thenThrow(new NoSuchAlgorithmException());

    cryptoUtil.getRSAKeyEntry();
}
 
开发者ID:auth0,项目名称:Auth0.Android,代码行数:18,代码来源:CryptoUtilTest.java

示例12: createNewKeys

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
private void createNewKeys(Context context, String key) {
    String alias = key;
    try {
        if (!keyStore.containsAlias(alias)) {
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            end.add(Calendar.YEAR, 1);
            KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context.getApplicationContext())
                    .setAlias(alias)
                    .setSubject(new X500Principal("CN=Sample Name, O=Android Authority"))
                    .setSerialNumber(BigInteger.ONE)
                    .setStartDate(start.getTime())
                    .setEndDate(end.getTime())
                    .build();
            KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
            generator.initialize(spec);

            KeyPair keyPair = generator.generateKeyPair();

        }
    } catch (Exception e) {
    }
}
 
开发者ID:kiwitechnologies,项目名称:SecurityAndroid,代码行数:24,代码来源:KeyChain.java

示例13: createNewKeys

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
public void createNewKeys(View view) {
    String alias = aliasText.getText().toString();
    try {
        // Create new key if needed
        if (!keyStore.containsAlias(alias)) {
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            end.add(Calendar.YEAR, 1);
            KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(this)
                    .setAlias(alias)
                    .setSubject(new X500Principal("CN=Sample Name, O=Android Authority"))
                    .setSerialNumber(BigInteger.ONE)
                    .setStartDate(start.getTime())
                    .setEndDate(end.getTime())
                    .build();
            KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
            generator.initialize(spec);

            KeyPair keyPair = generator.generateKeyPair();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Exception " + e.getMessage() + " occured", Toast.LENGTH_LONG).show();
        Log.e(TAG, Log.getStackTraceString(e));
    }
    refreshKeys();
}
 
开发者ID:obaro,项目名称:SimpleKeystoreApp,代码行数:27,代码来源:MainActivity.java

示例14: generateKeyPair

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static void generateKeyPair(Context context, String alias)
        throws GeneralSecurityException {
    final Calendar start = new GregorianCalendar();
    final Calendar end = new GregorianCalendar();
    end.add(Calendar.YEAR, 100);

    final KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
            .setAlias(alias)
            .setSubject(new X500Principal("CN=" + alias))
            .setSerialNumber(BigInteger.ONE)
            .setStartDate(start.getTime())
            .setEndDate(end.getTime())
            .build();

    final KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");

    gen.initialize(spec);
    gen.generateKeyPair();
}
 
开发者ID:0xbb,项目名称:otp-authenticator,代码行数:21,代码来源:SecretKeyWrapper.java

示例15: generateRSAKey

import android.security.KeyPairGeneratorSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.KITKAT)
@SuppressWarnings("deprecation")
private KeyPair generateRSAKey(String keystoreAlias) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    fixPrng();
    Calendar start = new GregorianCalendar();
    Calendar end = new GregorianCalendar();
    end.add(Calendar.YEAR, 10);
    KeyPairGeneratorSpec spec =
            new KeyPairGeneratorSpec.Builder(context)
                    .setAlias(keystoreAlias)
                    .setSubject(new X500Principal("CN=" + keystoreAlias))
                    .setSerialNumber(BigInteger.valueOf(1337))
                    .setStartDate(start.getTime())
                    .setEndDate(end.getTime())
                    .setKeySize(RSA.KEY_LENGTH_BITS)
                    .build();
    KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance(RSA.CIPHER, KeyStoreConstants.PROVIDER);
    kpGenerator.initialize(spec);
    KeyPair keyPair = kpGenerator.generateKeyPair();
    storePublicKey(keyPair.getPublic());
    return keyPair;
}
 
开发者ID:plum-umd,项目名称:java-sketch,代码行数:23,代码来源:CryptographyManager.java


注:本文中的android.security.KeyPairGeneratorSpec类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。