Posted By:
Chandra_Mouli
Posted On:
Thursday, December 12, 2002 09:38 PM
Hi I need help. I written code as given below to make comunication with one of the secure server. I got keystore file from the person who maintaning the server(We are developing a application. In this we are implemented online creditcard payment process. In this I enabled protocol and CipherSuit as specified by our payment gateway client. But problem is it's not able to make SSLHandshake. Can any body refer code that I given below and if my code goes wrong at any place, plese correct it and send back to me. thank you and regards Mouli ***************************************** SSLConnectionToPaywareHost.java **********
More>>
Hi I need help.
I written code as given below to make comunication with one of the secure server.
I got keystore file from the person who maintaning the server(We are developing a application. In this we are implemented online creditcard payment process.
In this I enabled protocol and CipherSuit as specified by our payment gateway client.
But problem is it's not able to make SSLHandshake.
Can any body refer code that I given below and if my code goes wrong at any place, plese correct it and send back to me.
thank you and regards
Mouli
*****************************************
SSLConnectionToPaywareHost.java
*****************************************
import java.net.*;
import java.io.*;
import javax.net.ssl.*;
import javax.security.cert.X509Certificate;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
public class SSLConnectionToPaywareHost
{
private static final String HOST = "191.192.43.3"; // SSL Server IP
private static final int DEFAULT_PORT = 3443; // SSL Server Port
private SSLConnectionToPaywareHost(){};
public static SSLConnectionToPaywareHost getConnection()
{
return new SSLConnectionToPaywareHost();
}
/** Method for loading keystore and opening a ssl socket to SSLSever
*/
public byte[] send(byte[] data)
{
byte responseData[];
StringBuffer response = new StringBuffer();
try{
// Declaring SSLSocket variable
SSLSocket socket = null;
// Declaring SSLSocketFactory
SSLSocketFactory factory = null;
// Intializing SSLContext
SSLContext ctx = SSLContext.getInstance( "SSL" );
char[] store_passphrase = "StorePass".toCharArray(); // Key Store Password
char[] key_passphrase = "KeyPass".toCharArray(); // Key password
// KeyStore for the SSL client certificate
KeyStore ks = KeyStore.getInstance( "JKS") ;
ks.load(new FileInputStream("cert/KeyStore2.auth"), store_passphrase);
// Intializing KeyManagerFactory
KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
kmf.init(ks, key_passphrase);
// Intializing TrustManagerFactor
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init( ks );
// Intializing SSLContext
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
// getting SSLCocketFactory with intialized SSLContext
factory = ctx.getSocketFactory();
// Getting SSLSocket
socket = (SSLSocket)factory.createSocket(HOST, DEFAULT_PORT);
// Loading secure server supported protocol;
String[] protocols = {"SSLv3"};
socket.setEnabledProtocols(protocols);
// Loading secure server supported CipherSuit
String[] suppCiphers = {"SSL_RSA_WITH_3DES_EDE_CBC_SHA"};
socket.setEnabledCipherSuites(suppCiphers);
// Getting output stream from socket
OutputStream out = socket.getOutputStream();
// Sending data to paywareEhost
System.out.println("Sending Data>>>>>>>>>>
:"+new String(data)+":");
out.write(data);
out.flush();
int bytesRead;
byte buffer[] = new byte[5024];
ByteArrayOutputStream bao = new ByteArrayOutputStream();
InputStream in = socket.getInputStream();
System.out.println("Getting Data
<
<
<
<
<
<
<
<
<
<
<");
while ((bytesRead = in.read(buffer)) != -1)
bao.write(buffer,0,bytesRead);
System.out.println("recived Data:"+new String(bao.toByteArray()));
// Closing all connections
in.close();
out.close();
socket.close();
return bao.toByteArray();
}
catch ( Exception e )
{
e.printStackTrace();
}
return null;
}
}
*****************************************
End of SSLConnectionToPaywareHost.java
*****************************************
<<Less