Android:如何仅为我的应用设置密码/ PIN码?
创始人
2024-10-13 16:00:56
0

要为你的Android应用设置密码或PIN码,你可以使用Android提供的KeyStore和KeyguardManager来实现。以下是一个示例代码,演示了如何实现这一功能:

  1. 首先,在你的AndroidManifest.xml文件中添加以下权限:

  1. 在你的Activity中,添加以下代码:
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.security.KeyStore;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class MainActivity extends Activity {

    private static final String KEY_NAME = "my_key";
    private static final int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 123;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

            if (keyguardManager.isKeyguardSecure()) {
                createKey();

                Cipher cipher = getCipher();

                if (cipher != null) {
                    Intent intent = keyguardManager.createConfirmDeviceCredentialIntent("Unlock", "Confirm your screen lock PIN or password");
                    if (intent != null) {
                        startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
                    } else {
                        // Device does not support screen lock
                    }
                } else {
                    // Cipher initialization failed
                }
            } else {
                // Device does not have a screen lock
            }
        } else {
            // Device is running on a version prior to Marshmallow
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void createKey() {
        try {
            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);

            KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
            keyGenerator.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());

            keyGenerator.generateKey();
        } catch (Exception e) {
            Log.e("TAG", Log.getStackTraceString(e));
        }
    }

    private Cipher getCipher() {
        try {
            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);

            SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, null);

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

            cipher.init(Cipher.ENCRYPT_MODE, key);
            return cipher;
        } catch (Exception e) {
            Log.e("TAG", Log.getStackTraceString(e));
        }
        return null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
            if (resultCode == RESULT_OK) {
                // User has successfully authenticated, perform your app operations here
            } else {
                // User failed to authenticate, handle accordingly
            }
        }
    }
}

这段代码将在应用启动时检查设备是否有屏幕锁定,并要求用户输入密码或PIN码进行验证。如果验证成功,你可以在onActivityResult方法中执行你的应用逻辑。

请注意,上述代码仅适用于Android 6.0(Marshmallow)及更高版本。在更早的版本中,你可以使用不同的方法来实现此功能。

相关内容

热门资讯

玩家实测!fishpoker透... 玩家实测!fishpoker透视底牌,xpoker透视辅助,分享教程(有挂技巧)1、点击下载安装,微...
揭秘几款!hhpoker有辅助... 1、揭秘几款!hhpoker有辅助吗,wepoker脚本下载,第三方教程(有挂辅助);详细教程。2、...
总算清楚!wepoker祈福有... 总算清楚!wepoker祈福有用吗,wepoker私人局怎么玩,攻略教程(有挂方法);玩家必备必赢加...
记者揭秘!aapoker辅助怎... 记者揭秘!aapoker辅助怎么用,hhpoker是真的还是假的,专业教程(有挂技巧);hhpoke...
盘点一款!epoker透视底牌... 1、盘点一款!epoker透视底牌,大菠萝免费辅助,详细教程(有挂辅助);详细教程。2、大菠萝免费辅...
重大通报!wepoker高级辅... 1、重大通报!wepoker高级辅助,wepoker国外版透视,规律教程(有挂技巧);详细教程。2、...
一秒答解!wepoker究竟有... 一秒答解!wepoker究竟有没有透视,hhpoker脚本下载,介绍教程(有挂透明);建议优先通过w...
每日必备!aapoker公共底... 1、每日必备!aapoker公共底牌,wepoker底牌透视脚本,攻略方法(有挂软件)(UU pok...
今日百科!wepoker破解游... 今日百科!wepoker破解游戏盒子,hardrock透视工具,新2025教程(有挂软件)是由北京得...
1.9分钟了解!wepoker... 自定义wepoker私人局俱乐部辅助系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专...