5

我正在使用 KeyStore 来保护我的私钥,但是当线路:

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

执行我有这个例外:

'java.lang.NullPointerException'.

我不明白问题出在哪里。

代码:

    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();        
    }

谢谢您的帮助!

4

1 回答 1

4

改变:

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

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

现在,您将方法中的上下文参数分配给与全局参数相同的值,即 null。因此,您的上下文实际上并没有被存储。

于 2013-02-20T05:28:47.550 回答