Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.os.Build;

import java.security.Key;
import java.security.NoSuchAlgorithmException;

import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    private static final int ITERATION_COUNT = 1000;
    private static final int KEY_LENGTH = 128;

    /**
     *
     * @throws InvalidKeySpecException
     * @throws NoSuchAlgorithmException
     */
    private static Key deriveKeyPbkdf2(byte[] salt, String password)
            throws InvalidKeySpecException, NoSuchAlgorithmException {

        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
        SecretKeyFactory factory;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1And8bit");
        } else {
            factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        }

        byte[] keyBytes = factory.generateSecret(keySpec).getEncoded();

        return new SecretKeySpec(keyBytes, "AES");
    }
}