Hardcode keystore into java program

F

Fritz Bayer

Hi,

I generated a keystore file with the java keytool. Now I would like to
import the data via copy and paste into my java source code and store
the data as a String.

To achieve this I did the following steps:

1. native2ascii -encoding utf-8 keystorefile iso8859_keystore_data.txt
2. Replace " in the iso8859_keystore_data.txt with \"
3. Replace newlines in the iso8859_keystore_data.txt with \n
4. Copy and paste the data into my program.

When I load the keystore using " trustStore.load(new
ByteArrayInputStream(copyAndPastedString.getBytes("utf-8")),
password);" I get an exception saying that the Format is invalid. So
somewhere along the conversion the data get altered.

For all the IO pros out there I have some questions, which I think
will help me get closer to the solution of the problem.

1. Are the keystore files created with the java keytool actually
encoded in utf-8? Which encoding is used - I assumed its utf-8...

2. Generally speaking should I use the string "utf-8" or "utf8" in my
java program, when for example using String.getBytes(String encoding)
?

3. Do you know a program, which reads a utf-8 encoded file and writes
the data to a new file thereby producing only unicode escapes ? (This
would save step 2 and 3, which are error prone)

Fritz
 
R

Rogan Dawes

Fritz said:
Hi,

I generated a keystore file with the java keytool. Now I would like to
import the data via copy and paste into my java source code and store
the data as a String.

To achieve this I did the following steps:

1. native2ascii -encoding utf-8 keystorefile iso8859_keystore_data.txt
2. Replace " in the iso8859_keystore_data.txt with \"
3. Replace newlines in the iso8859_keystore_data.txt with \n
4. Copy and paste the data into my program.

Don't do that. Rather package your keystore and your class together in a
jar file. Your code might look something like:

KeyStore ks = KeyStore.getInstance("JKS");
ks.load(ClassLoader.getSystemResourceAsStream("MyStore"), "passwd");

Read the javadocs for getSystemResourceAsStream() or even
getResourceAsStream() for info about how the file is located.

If you HAVE to embed the keystore in your class, store it as a byte
array. That way, there is no conversion, and no corruption.

i.e. write a short program that reads an input stream, and writes the
individual bytes out, formatted in such a way as to be a Java parseable
byte array definition. Then copy and paste that output into your code.

You can probably then create a ByteArrayInputStream to read your
keystore from.

e.g.

byte[] keystore = new byte[] { 0x01, 0x02, ...... }; // MANY lines!!
InputStream is = new ByteArrayInputStream(keystore);
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(is, "passwd");

Regards,

Rogan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top