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


Java KeyProperties類代碼示例

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


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

示例1: createNewKey

import android.security.keystore.KeyProperties; //導入依賴的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: generateNewAeadKey

import android.security.keystore.KeyProperties; //導入依賴的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

示例3: createKey

import android.security.keystore.KeyProperties; //導入依賴的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

示例4: createCipherKeyGenerator

import android.security.keystore.KeyProperties; //導入依賴的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

示例5: createCipher

import android.security.keystore.KeyProperties; //導入依賴的package包/類
private void createCipher(){
    if(cipherCreated){
        return;
    }

    try {
        cipher = Cipher.getInstance(
                KeyProperties.KEY_ALGORITHM_AES + "/"
                        + KeyProperties.BLOCK_MODE_CBC + "/"
                        + KeyProperties.ENCRYPTION_PADDING_PKCS7);

        cipherCreated = true;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get an instance of Cipher", e);
    }
}
 
開發者ID:OmarAflak,項目名稱:Fingerprint,代碼行數:17,代碼來源:CipherHelper.java

示例6: generateAuthenticationKey

import android.security.keystore.KeyProperties; //導入依賴的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.KeyProperties; //導入依賴的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.KeyProperties; //導入依賴的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.KeyProperties; //導入依賴的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: createRealWhorlwind

import android.security.keystore.KeyProperties; //導入依賴的package包/類
@TargetApi(Build.VERSION_CODES.M)
private static Whorlwind createRealWhorlwind(Context context, Storage storage, String keyAlias) {
  try {
    FingerprintManager fingerprintManager = context.getSystemService(FingerprintManager.class);

    KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
    keyStore.load(null); // Ensure the key store can be loaded before continuing.

    KeyPairGenerator keyGenerator =
        KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    RealWhorlwind.createCipher(); // If this doesn't throw, the cipher we need is available.

    return new RealWhorlwind(context, fingerprintManager, storage, keyAlias, keyStore,
        keyGenerator, keyFactory);
  } catch (Exception e) {
    Log.w("Cannot store securely.", e);
    return new NullWhorlwind();
  }
}
 
開發者ID:square,項目名稱:whorlwind,代碼行數:22,代碼來源:Whorlwind.java

示例11: createKeysM

import android.security.keystore.KeyProperties; //導入依賴的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

示例12: createKey

import android.security.keystore.KeyProperties; //導入依賴的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

示例13: createKey

import android.security.keystore.KeyProperties; //導入依賴的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

示例14: initKey

import android.security.keystore.KeyProperties; //導入依賴的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

示例15: createNewKeysIfNeeded

import android.security.keystore.KeyProperties; //導入依賴的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


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