0

I have a server.java file as:

import javax.net.ssl.*;
import java.io.*;
//*****************
public class Server
{
    public static void main(String[] args)
    {
        try {
            SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
            SSLServerSocket sslserversocket =(SSLServerSocket)sslserversocketfactory.createServerSocket(80);

            SSLSocket sslsocket = (SSLSocket)
            sslserversocket.accept();

            InputStream is = sslsocket.getInputStream();
            InputStreamReader isr = new
            InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);
            String string = null;

            while ((string = br.readLine()) != null)
            {
            System.out.println(string);
            System.out.flush();
            }
        } 

        catch (Exception e){
            e.printStackTrace();
        }
    }
}

and a Client.java file as:

import javax.net.ssl.*;
import java.io.*;
//*****************
public class Client
{
    public static void main(String[] args)
    {
        try {
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("127.0.0.1", 80);

            InputStream is = System.in;
            InputStreamReader isr = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);
            OutputStream os = sslsocket.getOutputStream();

            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);

            String string = null;
            while ((string = br.readLine()) != null)
            {
            bw.write(string + '\n');
            bw.flush();
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

also i created my own certificate like this:

C:\Temp>keytool -genkey -keystore myCertificate -keyalg RSA
Enter keystore password: abcdefg
Re-enter new password: abcdefg
What is your first and last name?
[Unknown]: first last
What is the name of your organizational unit?
[Unknown]: cs
What is the name of your organization?
[Unknown]: stackoverflow
What is the name of your City or Locality?
[Unknown]: NYC
What is the name of your State or Province?
[Unknown]: NY
What is the two-letter country code for this unit?
[Unknown]: us
Is CN=first last, OU=cs, O=stackoverflow, L=NYC, ST=NY, C=us correct?
[no]: yes
Enter key password for <mykey>
(RETURN if same as keystore password):

but no matter what i do i keep getting error as:

main, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during
handshake
main, SEND TLSv1 ALERT:  fatal, description = handshake_failure
main, WRITE: TLSv1 Alert, length = 2
main, called closeSocket()
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
        at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
        at sun.security.ssl.AppOutputStream.write(Unknown Source)
        at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
        at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
        at sun.nio.cs.StreamEncoder.implFlush(Unknown Source)
        at sun.nio.cs.StreamEncoder.flush(Unknown Source)
        at java.io.OutputStreamWriter.flush(Unknown Source)
        at java.io.BufferedWriter.flush(Unknown Source)
        at Client.main(Client.java:25)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
        at sun.security.ssl.InputRecord.read(Unknown Source)
        ... 11 more

My concern is if i change the port from 80 to something else, the code works perfectly fine but i have to specifically work on this port itself. The certificate and both java files are in same folder. Can you guys help me figure out a way to perform SSL operations on port 80?

user9517536248
  • 355
  • 5
  • 24

2 Answers2

0

I figured a way out to do it.

Basically what i did is used java's keystore to generate a keystore.jks file and then i used SSLContext to make my keystore certificate valid and hence performed SSL on port 80.

Thanks for help guys.

user9517536248
  • 355
  • 5
  • 24
-1

SSL is running on port 443。And it is default ,just like 80,no show on the adress bar.

lidl
  • 49
  • 10