存储使用密钥库在Android的关键 [英] Storing key using KeyStore in Android

查看:127
本文介绍了存储使用密钥库在Android的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的密钥库来保护我的私人密钥,但是当行:

I am using KeyStore to protect my private key but when the line:

FileOutputStream fos = ctx.openFileOutput("bs.keystore", Context.MODE_PRIVATE);

执行我有这样的例外:

is executed I have this exception:

'java.lang.NullPointerException'.

我不明白问题出在哪里。

I don't understand where is the problem.

code:

    private Context ctx;

    public DataSec(Context ctx) 
    {
    ctx = this.ctx;
    }

    public void genKey() throws Exception
    {
    SecretKey key = KeyGenerator.getInstance("AES").generateKey();

    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, "clavedekey".toCharArray());

    PasswordProtection pass = new PasswordProtection("fedsgjk".toCharArray());
    KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(key);
    ks.setEntry("secretKeyAlias", skEntry, pass);

    FileOutputStream fos = ctx.openFileOutput("bs.keystore", Context.MODE_PRIVATE);
    ks.store(fos, "clavedekey".toCharArray());
    fos.close();        
    }

感谢您的帮助!

Thanks for the Help!

推荐答案

修改

public DataSec(Context ctx) 
{
ctx = this.ctx;
}

public DataSec(Context ctx) 
{
    this.ctx = ctx;
}

现在,你分配的​​方法相同的值作为全球性的,这是空的上下文参数。由此,上下文实际上并不获取存储

Right now, you're assigning the context parameter in the method to the same value as the global one, which is null. Due to this, your context doesn't actually get stored.

这篇关于存储使用密钥库在Android的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆