Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中使用指纹扫描仪生成加密指纹密钥_Android - Fatal编程技术网

在android中使用指纹扫描仪生成加密指纹密钥

在android中使用指纹扫描仪生成加密指纹密钥,android,Android,我正在开发一个应用程序,我需要通过安卓设备内置的指纹扫描仪扫描指纹。根据指纹,我需要生成一个加密密钥,该密钥可以以字符串的形式存储在数据库中。以下是我尝试的代码: package com.ebookfrenzy.generatekey; import android.app.KeyguardManager; import android.content.Context; import android.security.keystore.KeyProperties

我正在开发一个应用程序,我需要通过安卓设备内置的指纹扫描仪扫描指纹。根据指纹,我需要生成一个加密密钥,该密钥可以以字符串的形式存储在数据库中。以下是我尝试的代码:

    package com.ebookfrenzy.generatekey;

    import android.app.KeyguardManager;
    import android.content.Context;
    import android.security.keystore.KeyProperties;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.hardware.fingerprint.FingerprintManager;
    import android.util.Base64;
    import android.widget.Toast;
    import android.security.keystore.KeyGenParameterSpec;
    import android.security.keystore.KeyPermanentlyInvalidatedException;

    import java.security.KeyStore;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.NoSuchAlgorithmException;
    import java.security.NoSuchProviderException;
    import java.security.cert.CertificateException;
    import java.security.InvalidAlgorithmParameterException;
    import java.io.IOException;
    import java.security.InvalidKeyException;
    import java.security.KeyStoreException;
    import java.security.UnrecoverableKeyException;

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

    public class MainActivity extends AppCompatActivity {

        private static final String KEY_NAME = "example_key";
        private FingerprintManager fingerprintManager;
        private Cipher cipher;
        private FingerprintManager.CryptoObject cryptoObject;
        private KeyStore keyStore;
        private KeyGenerator keyGenerator;
        byte[] En_Key;
        private String encrypted;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            generateKey();
            if(cipherInit())
            {
                cryptoObject=new FingerprintManager.CryptoObject(cipher);
               encrypted=byteArraytoHex(En_Key);
                Toast.makeText(this,encrypted, Toast.LENGTH_SHORT).show();

            }
        }
        protected void generateKey() {
            try {
                keyStore = KeyStore.getInstance("AndroidKeyStore");
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                keyGenerator = KeyGenerator.getInstance(
                KeyProperties.KEY_ALGORITHM_AES,
                "AndroidKeyStore");
            } catch (NoSuchAlgorithmException |
                    NoSuchProviderException e) {
                throw new RuntimeException(
                        "Failed to get KeyGenerator instance", e);
            }
            try {
                keyStore.load(null);
        keyGenerator.init(new
                KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                        KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(false)
                .setEncryptionPaddings(
                        KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());
        keyGenerator.generateKey();
    } catch (NoSuchAlgorithmException |
            InvalidAlgorithmParameterException
            | CertificateException | IOException e) {
        throw new RuntimeException(e);
    }
}
public boolean cipherInit() {
    try {
        cipher = Cipher.getInstance(
                KeyProperties.KEY_ALGORITHM_AES + "/"
                        + KeyProperties.BLOCK_MODE_CBC + "/"
                        + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException |
            NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get Cipher", e);
    }

    try {
        keyStore.load(null);
        SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
                null);
        En_Key=key.getEncoded();
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return true;
    } catch (KeyPermanentlyInvalidatedException e) {
               return false;
            } catch (KeyStoreException | CertificateException
            | UnrecoverableKeyException | IOException
            | NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to init Cipher", e);
    }
}
public static String byteArraytoHex(byte[] array)
{
    StringBuffer hexStr=new StringBuffer();
    for(byte b:array) {
        int inval = b & 0xff;
        if (inval < 0x10) {
            hexStr.append("0");
        }
        hexStr.append(Integer.toHexString(inval));
    }
    return hexStr.toString();
    }
package com.ebookfrenzy.generatekey;
导入android.app.KeyguardManager;
导入android.content.Context;
导入android.security.keystore.KeyProperties;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.hardware.fingerprint.FingerprintManager;
导入android.util.Base64;
导入android.widget.Toast;
导入android.security.keystore.KeyGenParameterSpec;
导入android.security.keystore.KeyPermanentlyInvalidatedException;
导入java.security.KeyStore;
导入java.security.invalidalgorithParameterException;
导入java.security.NoSuchAlgorithmException;
导入java.security.NoSuchProviderException;
导入java.security.cert.CertificateException;
导入java.security.invalidalgorithParameterException;
导入java.io.IOException;
导入java.security.InvalidKeyException;
导入java.security.KeyStoreException;
导入java.security.UnrecoverableKeyException;
导入javax.crypto.KeyGenerator;
导入javax.crypto.Cipher;
导入javax.crypto.NoSuchPaddingException;
导入javax.crypto.SecretKey;
导入javax.crypto.Cipher;
公共类MainActivity扩展了AppCompatActivity{
私有静态最终字符串密钥\u NAME=“example\u KEY”;
私人指纹经理;
专用密码;
私有指纹管理器。加密对象加密对象;
私有密钥库;
私钥生成器;
字节[]En_键;
私有字符串加密;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generateKey();
if(cipherit())
{
cryptoObject=新指纹管理器。cryptoObject(密码);
加密=byteArraytoHex(En_密钥);
Toast.makeText(this,加密,Toast.LENGTH_SHORT).show();
}
}
受保护的void generateKey(){
试一试{
keyStore=keyStore.getInstance(“AndroidKeyStore”);
}捕获(例外e){
e、 printStackTrace();
}
试一试{
keyGenerator=keyGenerator.getInstance(
KeyProperties.KEY_算法_AES,
“AndroidKeyStore”);
}catch(nosuchagorithmexception|
无此项(提供例外情况e){
抛出新的运行时异常(
“获取KeyGenerator实例失败”,e);
}
试一试{
keyStore.load(null);
keyGenerator.init(新的
KeyGenParameterSpec.Builder(键名称,
KeyProperties.PURPOSE\u加密|
KeyProperties.PURPOSE(解密)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(false)
.setEncryptionPaddings(
KeyProperties.ENCRYPTION_PADDING_PKCS7)
.build());
keyGenerator.generateKey();
}catch(nosuchagorithmexception|
无效算法参数异常
|证书例外(IOE例外){
抛出新的运行时异常(e);
}
}
公共布尔密码{
试一试{
cipher=cipher.getInstance(
KeyProperties.KEY\u算法\u AES+“/”
+KeyProperties.BLOCK_MODE_CBC+“/”
+KeyProperties.ENCRYPTION_PADDING_PKCS7);
}catch(nosuchagorithmexception|
无此填充例外(e){
抛出新的运行时异常(“获取密码失败”,e);
}
试一试{
keyStore.load(null);
SecretKey key=(SecretKey)keyStore.getKey(key\u NAME,
无效);
En_Key=Key.getEncoded();
cipher.init(cipher.ENCRYPT_模式,密钥);
返回true;
}捕获(密钥永久无效异常e){
返回false;
}catch(KeyStoreException | certificateeexception
|不可恢复的KeyException | IOException
|NoSuchAlgorithmException | InvalidKeyException e){
抛出新的运行时异常(“初始化密码失败”,e);
}
}
公共静态字符串byteArraytoHex(字节[]数组)
{
StringBuffer hexStr=新的StringBuffer();
for(字节b:数组){
int inval=b&0xff;
如果(无效<0x10){
hexStr.append(“0”);
}
append(Integer.toHexString(inval));
}
返回hexStr.toString();
}

}

但这不起作用。我错在哪里?有人能帮忙吗?“不起作用”不是一个精确的问题描述。好吧,好吧!在这段代码中,我不明白密钥实际上是在哪里生成的。根据我对android的理解,SecretKey是指纹认证后生成的密钥。所以我把它编码成字节的形式。然后我使用ByteArrayToHex函数将其转换为字符串。但这实际上不起作用。代码的其余部分是正确的。你能帮我理解加密密钥是在哪里生成的吗@亨利:但这不起作用。我错在哪里?有人能帮忙吗?“不起作用”不是一个精确的问题描述。好吧,好吧!在这段代码中,我不明白密钥实际上是在哪里生成的。根据我对android的理解,SecretKey是指纹认证后生成的密钥。所以我把它编码成字节的形式。然后我使用ByteArrayToHex函数将其转换为字符串。但事实并非如此