存储使用密钥库在Android的关键密钥、关键、Android

由网友(学渣代言人)分享简介:我使用的密钥库来保护我的私人密钥,但是当行:I am using KeyStore to protect my private key but when the line:FileOutputStream fos = ctx.openFileOutput("bs.keystore", Context.MODE_PR...

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

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.

阅读全文

相关推荐

最新文章