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


Java KeyGenParameterSpec类代码示例

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


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

示例1: createNewKey

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
/**
 * Create a new key in the Keystore
 */
private void createNewKey(){
  try {
    final KeyStore keyStore = KeyStore.getInstance(AndroidKeyStore);
    keyStore.load(null);

    final KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, AndroidKeyStore);

    // Build one key to be used for encrypting and decrypting the file
    keyGenerator.init(
        new KeyGenParameterSpec.Builder(ALIAS,
            KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
            .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
            .build());
    keyGenerator.generateKey();
    Log.i(TAG, "Key created in Keystore");

  }catch (KeyStoreException | InvalidAlgorithmParameterException | NoSuchProviderException | NoSuchAlgorithmException | CertificateException | IOException  kS){
    Log.e(TAG, kS.getMessage());
  }
}
 
开发者ID:Esri,项目名称:mapbook-android,代码行数:25,代码来源:CredentialCryptographer.java

示例2: createKeys

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
private static SecretKey createKeys(String alias, boolean auth_required) throws InvalidAlgorithmParameterException, KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException {
    // Create the keys if necessary

    KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE);

    // Set the alias of the entry in Android KeyStore where the key will appear
    // and the constrains (purposes) in the constructor of the Builder
    keyGenerator.init(new KeyGenParameterSpec.Builder(alias,
            KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
            .setBlockModes(NEW_BLOCK_MODE)
            .setUserAuthenticationRequired(auth_required)
            .setUserAuthenticationValidityDurationSeconds(AUTH_DURATION_SEC)
            .setRandomizedEncryptionRequired(false)
            .setEncryptionPaddings(NEW_PADDING)
            .build());
    return keyGenerator.generateKey();

}
 
开发者ID:breadwallet,项目名称:breadwallet-android,代码行数:19,代码来源:BRKeyStore.java

示例3: generateNewAeadKey

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
/**
 * Generates a new key in Android Keystore.
 *
 * <p>At the moment it can generate only AES256-GCM keys.
 */
public static void generateNewAeadKey(String keyUri)
    throws GeneralSecurityException {
  String keyId = Validators.validateKmsKeyUriAndRemovePrefix(PREFIX, keyUri);
  KeyGenerator keyGenerator = KeyGenerator.getInstance(
      KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
  KeyGenParameterSpec spec =
      new KeyGenParameterSpec.Builder(keyId,
          KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
              .setKeySize(256)
              .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
              .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
              .build();
  keyGenerator.init(spec);
  keyGenerator.generateKey();
}
 
开发者ID:google,项目名称:tink,代码行数:21,代码来源:AndroidKeystoreKmsClient.java

示例4: createKey

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
/**
 * Creates a symmetric key in the Android Key Store which can only be used after the user has
 * authenticated with fingerprint.
 */
public void createKey() {
    // The enrolling flow for fingerprint. This is where you ask the user to set up fingerprint
    // for your flow. Use of keys is necessary if you need to know if the set of
    // enrolled fingerprints has changed.
    try {
        // Set the alias of the entry in Android KeyStore where the key will appear
        // and the constrains (purposes) in the constructor of the Builder
        mKeyGenerator = KeyGenerator.getInstance(
                KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                        KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                        // Require the user to authenticate with a fingerprint to authorize every use
                        // of the key
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());
        mKeyGenerator.generateKey();
    } catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:sfilmak,项目名称:MakiLite,代码行数:28,代码来源:FingerprintUiHelper.java

示例5: createCipherKeyGenerator

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
private void createCipherKeyGenerator(){
    if(cipherKeyGenCreated){
        return;
    }
    try {
        cipherKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, provider);
        cipherKeyGenerator.init(new KeyGenParameterSpec.Builder(keyName,KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());

        cipherKeyGenCreated = true;
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
        throw new RuntimeException("Failed to create key generator", e);
    }
}
 
开发者ID:OmarAflak,项目名称:Fingerprint,代码行数:18,代码来源:CipherHelper.java

示例6: generateAuthenticationKey

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
/**
 * Generate a new RSA key pair entry in the Android Keystore by
 * using the KeyPairGenerator API. This creates both a KeyPair
 * and a self-signed certificate, both with the same alias
 */
private void generateAuthenticationKey() throws GeneralSecurityException {

  KeyPairGenerator kpg = KeyPairGenerator.getInstance(
      KeyProperties.KEY_ALGORITHM_RSA, keystoreName);
  kpg.initialize(new KeyGenParameterSpec.Builder(
      keyAlias,
      KeyProperties.PURPOSE_SIGN)
      .setKeySize(2048)
      .setCertificateSubject(new X500Principal("CN=unused"))
      .setDigests(KeyProperties.DIGEST_SHA256)
      .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1)
      .build());

  kpg.generateKeyPair();
}
 
开发者ID:jpuderer,项目名称:Taxi-Datalogger,代码行数:21,代码来源:MqttAuthentication.java

示例7: createKeysM

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.M)
static void createKeysM(String alias, boolean requireAuth) {
    try {
        KeyPairGenerator keyPairGenerator =
                KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA,
                        SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
        keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(alias,
                KeyProperties.PURPOSE_ENCRYPT
                        | KeyProperties.PURPOSE_DECRYPT).setAlgorithmParameterSpec(
                new RSAKeyGenParameterSpec(1024, F4))
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA384,
                        KeyProperties.DIGEST_SHA512)
                // Only permit the private key to be used if the user authenticated
                // within the last five minutes.
                .setUserAuthenticationRequired(requireAuth)
                .build());
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        Log.d(TAG, "Public Key is: " + keyPair.getPublic().toString());
    } catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:ronghao,项目名称:CacheManage,代码行数:25,代码来源:KeyStoreHelper.java

示例8: initFingerprintManager

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
private void initFingerprintManager() throws Throwable {
    mFpManager = (FingerprintManager) mContext.getSystemService(Context.FINGERPRINT_SERVICE);
    if (!mFpManager.isHardwareDetected())
        throw new IllegalStateException("Fingerprint hardware not present");

    KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
    KeyGenerator keyGenerator = KeyGenerator.getInstance(
            KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
    keyStore.load(null);
    keyGenerator.init(new KeyGenParameterSpec.Builder(
            KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
            .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
            .build());
    keyGenerator.generateKey();

    Cipher cipher = Cipher.getInstance(
            KeyProperties.KEY_ALGORITHM_AES + "/" +
            KeyProperties.BLOCK_MODE_CBC + "/" +
            KeyProperties.ENCRYPTION_PADDING_PKCS7);
    SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);
    cipher.init(Cipher.ENCRYPT_MODE, key);

    mFpHandler = new FingerprintHandler(cipher);

    if (DEBUG) log("Fingeprint manager initialized");
}
 
开发者ID:WrBug,项目名称:GravityBox,代码行数:28,代码来源:FingerprintLauncher.java

示例9: generateKeyPair

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
public KeyPair generateKeyPair(String keyAlias)
        throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException
{
    if (!allGood)
        return null;

    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
            KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");

    keyPairGenerator.initialize(
            new KeyGenParameterSpec.Builder(
                    keyAlias,
                    KeyProperties.PURPOSE_SIGN)
                    .setAlgorithmParameterSpec(new ECGenParameterSpec("secp256r1"))
                    .setDigests(KeyProperties.DIGEST_SHA256)
                    .setUserAuthenticationRequired(true)
                    .setUserAuthenticationValidityDurationSeconds(5 * 60)
                    .build());

    return keyPairGenerator.generateKeyPair();
}
 
开发者ID:zsavvas,项目名称:ReCRED_FIDO_UAF_OIDC,代码行数:22,代码来源:AndroidKeyStoreController.java

示例10: createKeysM

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.M)
private static void createKeysM(String alias, boolean requireAuth)
    throws NoSuchProviderException, NoSuchAlgorithmException,
    InvalidAlgorithmParameterException {
    KeyPairGenerator keyPairGenerator =
        KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA,
            SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
    keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(alias,
        KeyProperties.PURPOSE_ENCRYPT
            | KeyProperties.PURPOSE_DECRYPT).setAlgorithmParameterSpec(
        new RSAKeyGenParameterSpec(1024, F4))
        .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
        .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA384,
            KeyProperties.DIGEST_SHA512)
        // Only permit the private key to be used if the user authenticated
        // within the last five minutes.
        .setUserAuthenticationRequired(requireAuth)
        .build());
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    Log.d(TAG, "Public Key is: " + keyPair.getPublic().toString());
}
 
开发者ID:plusend,项目名称:DiyCode,代码行数:23,代码来源:KeyStoreHelper.java

示例11: createKey

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
public void createKey() {
    try {
        mKeyStore = KeyStore.getInstance("AndroidKeyStore");
        mKeyStore.load(null);
        mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                        KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());
        mKeyGenerator.generateKey();

    } catch (Exception e) {
        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}
 
开发者ID:Urucas,项目名称:android-fingerprint-example,代码行数:19,代码来源:MainActivity.java

示例12: createKey

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
/**
 * Creates a symmetric key in the Android Key Store which can only be used after the user has
 * authenticated with fingerprint.
 */
public void createKey() {
    // The enrolling flow for fingerprint. This is where you ask the user to set up fingerprint
    // for your flow. Use of keys is necessary if you need to know if the set of
    // enrolled fingerprints has changed.
    try {
        mKeyStore.load(null);
        // Set the alias of the entry in Android KeyStore where the key will appear
        // and the constrains (purposes) in the constructor of the Builder
        mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                        KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                        // Require the user to authenticate with a fingerprint to authorize every use
                        // of the key
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());
        mKeyGenerator.generateKey();
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException
            | CertificateException | IOException e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:Keyurpatel93,项目名称:DynamicShuttleRide,代码行数:28,代码来源:MainActivity.java

示例13: initKey

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
/**
 * Creates a symmetric key in the Android Key Store which can only be used after the user has
 * authenticated with fingerprint.
 */
public boolean initKey() {
    try {
        mKeyStore = KeyStore.getInstance("AndroidKeyStore");
        mKeyStore.load(null);
        mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());
        mKeyGenerator.generateKey();
        return true;
    } catch (Exception e) {
        return false;
    }
}
 
开发者ID:PacktPublishing,项目名称:Android-Sensor-Programming-By-Example,代码行数:21,代码来源:FingerPrintActivity.java

示例14: createNewKeysIfNeeded

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
@Override
protected void createNewKeysIfNeeded() throws KeyStoreException {
    try {
        if (this.keyStore.containsAlias(this.alias)) return;

        final KeyGenerator keyGenerator = KeyGenerator
                .getInstance(KeyProperties.KEY_ALGORITHM_AES, KeyStoreBase.ANDROID_KEY_STORE);

        keyGenerator.init(new KeyGenParameterSpec.Builder(this.alias,
                KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                .setRandomizedEncryptionRequired(false)
                .build());

        keyGenerator.generateKey();
    } catch (NoSuchAlgorithmException | NoSuchProviderException
            | InvalidAlgorithmParameterException | java.security.KeyStoreException e) {
        throw new KeyStoreException(new Throwable(e.getMessage()));
    }
}
 
开发者ID:toshiapp,项目名称:toshi-android-client,代码行数:22,代码来源:KeystoreHandler23.java

示例15: generateKey

import android.security.keystore.KeyGenParameterSpec; //导入依赖的package包/类
protected SecretKey generateKey() {
    SecretKey key = null;
    try {
        KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(
                getKeyAlias(),
                KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT);

        KeyGenParameterSpec keySpec = builder
                .setKeySize(CIPHER_KEY_LENGHT)
                .setBlockModes(CIPHER_BLOCKS)
                .setEncryptionPaddings(CIPHER_PADDING)
                .setRandomizedEncryptionRequired(false) //FIXME: set to true because we should be using IND-CPA but this means that a IV has to be store per token (less generic than i though)
                .setUserAuthenticationRequired(isKeyPinRequired())
                .setUserAuthenticationValidityDurationSeconds(getKeyPinDuration())
                .build();

        KeyGenerator  kg = KeyGenerator.getInstance(CIPHER_ALGO, KEYSTORE_TYPE);
        kg.init(keySpec);
        key = kg.generateKey();
    } catch (InvalidAlgorithmParameterException | NoSuchProviderException | NoSuchAlgorithmException e) {
        Log.e(TAG, "Couldn't generate secret key", e);
    }
    return key;
}
 
开发者ID:kalemontes,项目名称:OIDCAndroidLib,代码行数:25,代码来源:SensitiveDataPostApi23.java


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