java.io.IOException: Keystore was tampered with, or password was incorrect

  • Follow


 Hello

I'm trying to create a new https-connection with my own Java
application.

Before running the application I tried to create my private key and
retrieve the server side public key.

My (client) private key I tried to create like this:
"C:\Program Files\Java\jre1.5.0_04\bin\keytool" -genkey -alias
clientprivate -keystore client.private -storetype JKS -keyalg rsa
-dname "CN=John Smith, OU=MyUnit, O=MyOrganization, L=Helsinki,
S=Uusimaa, C=Finland" -storepass clientpw -keypass clientpw

Web site (server) public key I tried to retrieve by the Internet
Explorer:
Click the lock icon at the status bar of IE, Details->Copy to
File...->Next->Base-64
Encoding X.509 (.CER)->Next->Import.
Name of the .cer file: temp.cer

Then use keytool to create server.public file:
"C:\Program Files\Java\jre1.5.0_04\bin\keytool" -import -noprompt
-alias serverpublic -keystore server.public -file temp.cer -storepass
public


Now the application should be ready to run. But when running it,
exception occurs:

HttpsTester started.
java.io.IOException: Keystore was tampered with, or password was
incorrect
        at sun.security.provider.JavaKeyStore.engineLoad(Unknown
Source)
        at java.security.KeyStore.load(Unknown Source)
        at HttpsTester.start(HttpsTester.java:27)
        at HttpsTester.main(HttpsTester.java:63)



I suppose the problem is with the keys. Maybe I should not create the
private key like that, because maybe the server is expecting the same
private key than IE uses? Or maybe I should retrieve the full
certification chain for the server side public key (how to do that)?

Any ideas?

TR


CODE
============================


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.security.KeyStore;
import java.security.SecureRandom;


import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;


public class HttpsTester {


        private void start() {


                String clientPw = "clientpw";
                String serverPw = "public";
                String host = "https://mail.hut.fi/";
                int port = 443;


                SecureRandom secureRandom = new SecureRandom();
                secureRandom.nextInt();


                try {
                        KeyStore serverKeyStore =
KeyStore.getInstance("JKS");
                        serverKeyStore.load(new
FileInputStream("server.public"),
clientPw.toCharArray());


                        KeyStore clientKeyStore =
KeyStore.getInstance("JKS");
                        clientKeyStore.load( new
FileInputStream("client.private"),
serverPw.toCharArray() );


                        TrustManagerFactory tmf =
TrustManagerFactory.getInstance("SunX509");
                        tmf.init(serverKeyStore);


                        KeyManagerFactory kmf =
KeyManagerFactory.getInstance("SunX509");
                        kmf.init(clientKeyStore,
clientPw.toCharArray());


                        SSLContext sslContext =
SSLContext.getInstance("TLS");
                        sslContext.init(kmf.getKeyManagers(),
tmf.getTrustManagers(),
secureRandom);


                        SSLSocketFactory sf =
sslContext.getSocketFactory();
                        SSLSocket socket = (SSLSocket)sf.createSocket(
host, port );


                        BufferedReader d = new BufferedReader(new
InputStreamReader(socket.getInputStream()));


                        while (true) {
                                String newLine = d.readLine();
                                if (newLine == null) {
                                        break;
                                }
                                System.out.println("newLine");
                        }
                }
                catch (Exception e) {
                        e.printStackTrace(System.out);
                }
        }


        public static void main(String[] args) {
                System.out.println("HttpsTester started.");


                HttpsTester tester = new HttpsTester();
                tester.start(); 
        }

0
Reply tratilai (1) 4/3/2006 7:42:23 AM


0 Replies
407 Views

(page loaded in 0.045 seconds)

5/9/2013 11:16:17 AM


Reply: