A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearSSL socket php code needs to be converted to Java - page 1
User InfoPosts
SSL socket php code needs to be converted to Java#1
I have a Pem file that I use with this php code to connect to a c++ SSL server, but now I need this php code written in Java

$ctx = stream_context_create();

stream_context_set_option($ctx, (ssl(, (local_cert(, (cert.pem();

stream_context_set_option($ctx, (ssl(, (passphrase(, $pass);

$fp = stream_socket_client((ssl://serverURL(, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

if (!$fp) {

print "Failed to connect $err $errstr\n";

Any ideas how to read in the cert.pem file in Java and then establish the SSL socket?

Thank you!

posted date: 2009-04-06 11:56:00

Re: SSL socket php code needs to be converted to Java#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-06 11:56:01

Re: SSL socket php code needs to be converted to Java#3
You will need Bouncy Castle in your classpath.PEMReader pr = new PEMReader(new FileReader("cert.pem"));X509Certificate cert = (X509Certificate) pr.readObject();PEMReader kr = new PEMReader(new FileReader("privkey.pem"), new PasswordFinder() { public char[] getPassword() { return "passphase".toCharArray(); }});KeyPair key = (KeyPair) kr.readObject();KeyStore ksKeys = KeyStore.getInstance("JKS");ksKeys.load(null, "passphase".toCharArray());ksKeys.setCertificateEntry("MyCert", cert);ksKeys.setKeyEntry("Mykey", key.getPrivate(), "passphase".toCharArray(), new Certificate[]{cert});KeyManagerFactory kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm());kmf.init(ksKeys, "passphase".toCharArray());TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm());tmf.init(ksKeys);SSLContext sslContext = SSLContext.getInstance("TLS");sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);Socket socket = sslContext.getSocketFactory().createSocket( "localhost", 4433);BufferedReader in = new BufferedReader(new InputStreamReader( socket.getInputStream()));PrintWriter out = new PrintWriter(new OutputStreamWriter( socket.getOutputStream()));out.println("Hello World");System.out.println(in.readLine());out.close();in.close();If you run openssl s_server -Verify cert.pem -cert cert.pem -key privkey.pem, it should show:depth=0 /C=AU/ST=Some-State/O=Internet Widgits Pty Ltdverify error:num=18:self signed certificateverify return:1depth=0 /C=AU/ST=Some-State/O=Internet Widgits Pty Ltdverify return:1Hello WorldDONEshutting down SSLCONNECTION CLOSEDACCEPT

posted date: 2009-04-07 01:32:00

select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.