无法从KeyStore读取密钥

时间:2016-04-02 11:43:27

标签: java bouncycastle

我在使用KeyStore时遇到了一些问题。我在KeyStore上添加了密钥,无法从那里获取密钥。 我的代码:

        try 
        {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            SecureRandom random = new SecureRandom();
            keygen.init(128, random);
            SecretKey key = keygen.generateKey();
            KeyStore ks = KeyStore.getInstance("UBER", "BC");
            ks.load(null, pass);
            ks.store(new FileOutputStream(path), pass);
            ks.setKeyEntry(keyName, key, pass, null);
            System.out.println(ks.containsAlias(keyName));
            key = null;
            KeyStore ks1 = KeyStore.getInstance("UBER", "BC");
            ks1.load(new FileInputStream(path), pass);
            System.out.println(ks1.containsAlias(keyName));
        }

输出是:

true
false

似乎是关键的消失,或者我无法核心地加载KeyStore。你在这看到任何错误吗?

1 个答案:

答案 0 :(得分:3)

在向其添加密钥之前,您正在保存密钥库。交换这两行:

NSMutableArray *coordinateLatit=[[NSMutableArray alloc]init];

for(NSDictionary *d in nearbyStop)
{
    NSString    *latit=d[@"result"][@"lat"];

    if(lat) [coordinateLatit addObject:latit];
}

这样他们就是:

        ks.store(new FileOutputStream(path), pass);
        ks.setKeyEntry(keyName, key, pass, null);