从KeyStore获取密钥时出错(java)

时间:2014-03-24 21:40:10

标签: java keystore

我面临一个非常奇怪的错误。这个错误今天开始发生,它在星期五工作,没有任何改变,至少我不知道。

我有一个简单的KeyStore,我存储一个密钥来加密一些数据。仅用于测试创建的单独应用程序来处理此问题。这是代码:

public static void main(String[] args) {

    Properties prop = new Properties();
    try {

       prop.load(new FileInputStream("./security/security.properties"));

       String ksFile = prop.getProperty("KSFile");
       String ksPassword = prop.getProperty("KSPassword");
       String keyAlias = prop.getProperty("KeyAlias");
       String keyPassword = prop.getProperty("KeyPassword");
       prop.getProperty("CardDataEncryptionKey");

       KeyStore ks;
       ks = KeyStore.getInstance("JCEKS");

       FileInputStream fis = null;
       fis = new FileInputStream(ksFile);

       ks.load(fis, ksPassword.toCharArray());


       // For symmetric
       char[] keycharArray = keyPassword.toCharArray();
       ProtectionParameter proParam = new KeyStore.PasswordProtection( keycharArray );

       KeyStore.Entry entry = ks.getEntry( keyAlias, proParam );
       SecretKeyEntry aeskey = (KeyStore.SecretKeyEntry) entry;         

       aeskey.getSecretKey();


       fis.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

代码非常基础,只需打开security.properties中配置的密钥库即可。问题是现在我在以下行中收到错误:

       KeyStore.Entry entry = ks.getEntry( keyAlias, proParam );

我重复这一点并没有发生,只是突然开始。文件没问题,路径没问题,文件打开了,但是当我尝试获取KeyEntry时,我得到一个NumberFormatException,真的没有线索是否发生了什么。这是错误堆栈跟踪:

Test at localhost:62013 
    Thread [main] (Suspended (exception NumberFormatException)) 
        owns: InetAddress$Cache  (id=58)    
        owns: Object  (id=59)   
        Integer.parseInt(String, int) line: not available   
        Integer.<init>(String) line: not available  
        InetAddressCachePolicy.<clinit>() line: not available   
        InetAddress$Cache.getPolicy() line: not available   
        InetAddress$Cache.put(String, InetAddress[]) line: not available    
        InetAddress.cacheInitIfNeeded() line: not available 
        InetAddress.cacheAddresses(String, InetAddress[], boolean) line: not available  
        InetAddress.getAddressesFromNameService(String, InetAddress) line: not available    
        InetAddress.getLocalHost() line: not available  
        JarVerifier.getSystemEntropy() line: 783    
        JarVerifier.testSignatures(X509Certificate, CertificateFactory) line: 706   
        JarVerifier.access$400(X509Certificate, CertificateFactory) line: 34    
        JarVerifier$1.run() line: 183   
        JarVerifier$1.run() line: 149   
        AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
        JarVerifier.<clinit>() line: 148    
        JceSecurity.loadPolicies(File, CryptoPermissions, CryptoPermissions) line: 316  
        JceSecurity.setupJurisdictionPolicies() line: 261   
        JceSecurity.access$000() line: 48   
        JceSecurity$1.run() line: 78    
        AccessController.doPrivileged(PrivilegedExceptionAction<T>) line: not available [native method] 
        JceSecurity.<clinit>() line: 76 
        JceSecurityManager.<clinit>() line: 65  
        CipherForKeyProtector(Cipher).<init>(CipherSpi, Provider, String) line: 252 
        CipherForKeyProtector.<init>(CipherSpi, Provider, String) line: 377 
        KeyProtector.unseal(SealedObject) line: 347 
        JceKeyStore.engineGetKey(String, char[]) line: 133  
        JceKeyStore(KeyStoreSpi).engineGetEntry(String, KeyStore$ProtectionParameter) line: not available   
        KeyStore.getEntry(String, KeyStore$ProtectionParameter) line: not available 
        Test.main(String[]) line: 39    

请大家,给我一点希望! 谢谢!

1 个答案:

答案 0 :(得分:0)

最后这是一个Eclipse问题,也不知道它为什么会发生。在我更改域密码后,即使我重新启动Windows两次,我也开始收到此错误。

谁知道! 感谢。