Hardcode keystore into java program

Discussion in 'Java' started by Fritz Bayer, Oct 24, 2004.

  1. Fritz Bayer

    Fritz Bayer Guest

    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
    Fritz Bayer, Oct 24, 2004
    #1
    1. Advertising

  2. Fritz Bayer

    Rogan Dawes Guest

    Fritz Bayer wrote:
    > 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
    --
    Rogan Dawes

    *ALL* messages to will be dropped, and added
    to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
    Rogan Dawes, Oct 25, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim S.

    Hardcode user for helper.exe

    Tim S., Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    458
    Tim S.
    Jun 26, 2003
  2. Chris Jackson

    Re: Best Practice......Const Vs. Hardcode

    Chris Jackson, Aug 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    362
    Rajeev Soni
    Aug 11, 2003
  3. John_Woo

    How not to hardcode

    John_Woo, Aug 7, 2006, in forum: Java
    Replies:
    3
    Views:
    419
    Luc The Perverse
    Aug 7, 2006
  4. ragz_82
    Replies:
    0
    Views:
    459
    ragz_82
    Jul 1, 2009
  5. Sam Roberts
    Replies:
    3
    Views:
    933
    Sam Roberts
    Dec 5, 2004
Loading...

Share This Page