import android.content.Context;
import android.os.Build;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.support.annotation.RequiresApi;
import android.util.Base64;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.UnrecoverableKeyException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
public class KeyStoreUtil {
private static final String FIXED_IV = "467096543987";
KeyStore keyStore;
Context context;
public static final String ALIAS = "PROJECTNAME8588555111";
public KeyStoreUtil(Context context) {
try {
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
this.context = context;
} catch (Exception e) {
logger.severe("KeyStore intialization " + e);
keyStore = null;
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
public void createKey() {
try {
if (!keyStore.containsAlias(KeyStoreUtil.ALIAS)) {
if (!keyStore.containsAlias(KeyStoreUtil.ALIAS)) {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyGenerator.init(
new KeyGenParameterSpec.Builder(KeyStoreUtil.ALIAS,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setRandomizedEncryptionRequired(false)
.build());
keyGenerator.generateKey();
}
}
} catch (KeyStoreException | NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
logger.severe("Create Key: " + e);
}
}
private java.security.Key getSecretKey() throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
return keyStore.getKey(KeyStoreUtil.ALIAS, null);
}
public String encryptData(String data) {
Cipher c = null;
String result = null;
try {
if ("0".equals(data) || data.isEmpty()) {
return data;
}
c = Cipher.getInstance("AES/GCM/NoPadding");
c.init(Cipher.ENCRYPT_MODE, getSecretKey(), new GCMParameterSpec(128, FIXED_IV.getBytes()));
byte[] encodedBytes = c.doFinal(data.getBytes());
result = Base64.encodeToString(encodedBytes, Base64.DEFAULT);
} catch (InvalidKeyException | InvalidAlgorithmParameterException | KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
logger.severe("Encrypt data: " + e);
}
return result;
}
public String decryptData(String data) {
String decryptedText = null;
byte[] encodedData = Base64.decode(data, Base64.DEFAULT);
Cipher c = null;
try {
if ("0".equals(data) || data.isEmpty()) {
return data;
}
c = Cipher.getInstance("AES/GCM/NoPadding");
SecretKey sKey = (SecretKey) getSecretKey();
GCMParameterSpec spec = new GCMParameterSpec(128, FIXED_IV.getBytes());
c.init(Cipher.DECRYPT_MODE, sKey, spec);
byte[] decodedData = c.doFinal(encodedData);
decryptedText = new String(decodedData, "UTF-8");
} catch (NoSuchAlgorithmException | NoSuchPaddingException | KeyStoreException | InvalidKeyException | InvalidAlgorithmParameterException | UnrecoverableKeyException | BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException e) {
logger.severe("Decrypt data: " + e);
}
return decryptedText;
}
}
Comments
Post a Comment