96 lines
3.9 KiB
Java
96 lines
3.9 KiB
Java
package com.ctf.games.release.activity;
|
|
|
|
import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG;
|
|
import static androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL;
|
|
import static androidx.biometric.BiometricPrompt.ERROR_USER_CANCELED;
|
|
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.util.Base64;
|
|
import android.util.Log;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.biometric.BiometricManager;
|
|
import androidx.biometric.BiometricPrompt;
|
|
|
|
import com.ctf.games.release.R;
|
|
import com.jc.jcfw.security.BiometricHelper;
|
|
import com.jc.jcfw.security.CryptographyManager;
|
|
import com.jc.jcfw.security.CryptographyManagerImpl;
|
|
import com.jc.jcfw.security.EncryptedData;
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
public class BiometricActivity extends AppCompatActivity {
|
|
private static final String TAG = BiometricActivity.class.getSimpleName();
|
|
private BiometricPrompt.PromptInfo promptInfo;
|
|
private CryptographyManager cryptographyManager;
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_biometric);
|
|
Log.i(TAG, "onCreate: " + getIntent().getDataString());
|
|
promptInfo = BiometricHelper.createPromptInfo(this);
|
|
cryptographyManager = new CryptographyManagerImpl();
|
|
Intent intent = getIntent();
|
|
String action = intent.getStringExtra("action");
|
|
String funId = intent.getStringExtra("funid");
|
|
String account = intent.getStringExtra("account");
|
|
// check if action is exists
|
|
if ("encrypt".equals(action)) {
|
|
String password = intent.getStringExtra("password");
|
|
authenticateToEncrypt(funId, password);
|
|
} else if ("decrypt".equals(action)) {
|
|
String iv = intent.getStringExtra("iv");
|
|
authenticateToDecrypt(account, iv);
|
|
}
|
|
}
|
|
|
|
public void authenticateToEncrypt(String funId, String text) {
|
|
if (BiometricManager.from(this)
|
|
.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS) {
|
|
Cipher cipher = cryptographyManager.getInitializedCipherForEncryption("cebg_wallet_key");
|
|
BiometricPrompt biometricPrompt = BiometricHelper.createBiometricPrompt(this, _result -> {
|
|
if (_result.isError()) {
|
|
if (_result.getErrcode() == ERROR_USER_CANCELED) {
|
|
// close current activity
|
|
finish();
|
|
}
|
|
return;
|
|
}
|
|
EncryptedData encryptedData = cryptographyManager.encryptData(text, _result.getCipher());
|
|
String encryptedString = Base64.encodeToString(encryptedData.getCiphertext(), Base64.DEFAULT);
|
|
String ivString = Base64.encodeToString(encryptedData.getInitializationVector(), Base64.DEFAULT);
|
|
Log.i(TAG, "encrypted msg: " + encryptedString);
|
|
Log.i(TAG, "encrypted iv: " + ivString);
|
|
finish();
|
|
});
|
|
biometricPrompt.authenticate(promptInfo, new BiometricPrompt.CryptoObject(cipher));
|
|
}
|
|
}
|
|
|
|
public void authenticateToDecrypt(String text, String iv) {
|
|
if (BiometricManager.from(this)
|
|
.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) == BiometricManager.BIOMETRIC_SUCCESS) {
|
|
byte[] ivData = Base64.decode(iv, Base64.DEFAULT);
|
|
byte[] textData = Base64.decode(text, Base64.DEFAULT);
|
|
Cipher cipher = cryptographyManager.getInitializedCipherForDecryption("cebg_wallet_key", ivData);
|
|
BiometricPrompt biometricPrompt = BiometricHelper.createBiometricPrompt(this, _result -> {
|
|
if (_result.isError()) {
|
|
if (_result.getErrcode() == ERROR_USER_CANCELED) {
|
|
// close current activity
|
|
finish();
|
|
}
|
|
return;
|
|
}
|
|
String decryptedMsg = cryptographyManager.decryptData(textData, _result.getCipher());
|
|
Log.i(TAG, "decrypted msg: " + decryptedMsg);
|
|
finish();
|
|
});
|
|
biometricPrompt.authenticate(promptInfo, new BiometricPrompt.CryptoObject(cipher));
|
|
}
|
|
}
|
|
|
|
}
|