0

I have created a keystore(.jks) and I was accessing it in my IDE using the following code

KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream ksfis = new FileInputStream(keyStorePath);
BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
ks.load(ksbufin, spass.toCharArray());

but when I made the JAR file it gave an error. After searching for issue I came to know that I had to use

InputStream is = this.getClass().getClassLoader().getResourceAsStream(FilePath);

But this code gives me InputStream and my code expects ksfis to be FileInputStream. Can someone suggest me alternative to the code that expects FileInputStream.

UPDATE - MCVE

package com.token;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;

public class test {

   static final String keyStorePath = "Keys/keystore_pkcs8.jks";
   static final String alias = "mykey";
   static final String spass = "passwordhere";
   static final String kpass = "passwordhere";

   private static KeyPair getKeyPairFromKeyStore() throws Exception {

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

    //problem with this part of code
    Config conf = new Config();
    InputStream ksfis = conf.getFileInputStream(keyStorePath);
    BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
    ks.load(ksbufin, spass.toCharArray());

    PrivateKey privateKey = (PrivateKey) ks.getKey(alias, kpass.toCharArray());
    java.security.cert.Certificate cert = ks.getCertificate(alias);
    PublicKey publicKey = cert.getPublicKey();

    return new KeyPair(publicKey, privateKey);
}

public static void main(String[] args) {
    try {
        KeyPair ks = getKeyPairFromKeyStore();
    }
    catch(Exception e) {
        System.out.println(e.toString());
    }
  } 
}

Config class -

package com.token;

import java.io.InputStream;

class Config {
    public InputStream getFileInputStream(String FilePath) throws Exception{
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(FilePath);
        return is;
    }
}

UPDATE - Error with stackTrace

java.io.IOException: Stream closed
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:284)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at java.security.DigestInputStream.read(DigestInputStream.java:124)
at java.io.DataInputStream.readInt(DataInputStream.java:387)
at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:653)
at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:56)
at sun.security.provider.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:224)
at sun.security.provider.JavaKeyStore$DualFormatJKS.engineLoad(JavaKeyStore.java:70)
at java.security.KeyStore.load(KeyStore.java:1445)
at com.token.test.getKeyPairFromKeyStore(test.java:25)
at com.token.test.main(test.java:36)
ask
  • 157
  • 1
  • 8
  • 1
    Why does your code care if it's an `InputStream` or a `FileInputStream`? – lexicore Apr 13 '18 at 10:34
  • File file = new File(filepath); FileInputStream fis = new FileInputStream(file); from https://www.mkyong.com/java/how-to-read-file-in-java-fileinputstream/ – Bejond Apr 13 '18 at 10:35
  • 1
    Since resources *are not* files, the only way you can get a `FileInputStream` is copying resource to some temporary file. But I don't see why `FileInputStream` would be necessary. – lexicore Apr 13 '18 at 10:35
  • 1
    @bejond Does not work for resources. – lexicore Apr 13 '18 at 10:36
  • @lexicore For giving argument to BufferedInputStream I require a FileInputStream, I tried to replace it with InputStream but it gave error "java.io.IOException: Stream closed" – ask Apr 13 '18 at 10:37
  • 2
    If you refer to the [JavaDoc](https://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html#BufferedInputStream(java.io.InputStream)) `BufferedInputStream` only requires nothing more than an `InputStream`. –  Apr 13 '18 at 10:39
  • 2
    You don't need exactly `FileInputStream` to create `BufferedInputStream`. `BufferedInputStream` does not care. If you get IOEX `Stream closed`, there must be a different reason for this. Post [MCVE](http://stackoverflow.com/help/mcve) and we'll see what the problem is. – lexicore Apr 13 '18 at 10:40
  • [When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. Click this comment to find out how to provide what we need to help you.](https://stackoverflow.com/help/mcve) –  Apr 13 '18 at 10:40
  • URL url = getClass().getClassLoader().getResource(file); VirtualFile virtualFile = (VirtualFile) url.openConnection().getContent(); File tempFile = virtualFile.getPhysicalFile(); – Bejond Apr 13 '18 at 10:40
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Apr 13 '18 at 10:41
  • @bejond What kind of API is this? – lexicore Apr 13 '18 at 10:42
  • jboss-vfs-3.2.12.Final, VirtualFile. In wildfly, it copies resource to /standalone/tmp/vfs/. You can find temp file there. – Bejond Apr 13 '18 at 10:43
  • 2
    @bejond That's kind of a big leap you make there, assuming that OP is deploying on JBoss EAP/WildFly/Swarm – Robby Cornelissen Apr 13 '18 at 10:44
  • @RobbyCornelissen , yeah... But I think tell OP that the question is not necessary is kind of tough. I asked this kind of question before and be closed too. – Bejond Apr 13 '18 at 10:46
  • @bejond Well, as I said the only option is to copy resource into a file. – lexicore Apr 13 '18 at 10:48
  • 1
    @bejond Nobody said that the question is unnecessary. I disagree that "you don't need specifically `FileInputStream`" is "tough" here. First, it is simply true. Next, it helps to uncover the real problem the OP facing - IOEX "Stream Closed". – lexicore Apr 13 '18 at 10:51
  • @lexicore I have added the MCVE please see if this helps explain the problem better – ask Apr 13 '18 at 11:13
  • @JarrodRoberson I have added the MCVE please see if this helps explain the problem better – ask Apr 13 '18 at 11:14
  • @ask And the error your're getting? (Full exception stack trace, please.) – lexicore Apr 13 '18 at 11:23
  • @lexicore stack trace added – ask Apr 13 '18 at 11:29
  • 1
    @ask Hm, sorry, I can't say anything definitive. You don't close your stream correctly (you should do try-with-resources or similar), but I don't think this is a problem. – lexicore Apr 13 '18 at 11:32
  • The issue was with the paths, I found it. Thanks to all of you for the help. – ask Apr 13 '18 at 12:08

0 Answers0