[Java lista] RSA kulcsos kódolás
Andras Dobrosi
dobrosi at gmail.com
2006. Okt. 19., Cs, 14:00:09 CEST
package hu.remedios.mpp.common;
import java.net.*;
import javax.net.ssl.*;
import java.security.*;
import java.io.*;
/**
* @author andris
*
*/
public class LightClient {
private boolean httpsMode;
private String keystoreFileName;
private HttpsURLConnection httpsconn;
private HttpURLConnection httpconn;
private String urlstr;
private PrintWriter pout;
private int lastResponseCode;
private String lastResponseContent;
private String keyStorePassword;
private String truststoreFileName;
private String trustStorePassword;
public LightClient() {
}
public LightClient(String keystoreFileName, String keyStorePassword,
String truststoreFileName, String trustStorePassword) {
init(keystoreFileName, keyStorePassword, truststoreFileName,
trustStorePassword);
}
public void init (String keystoreFileName, String keyStorePassword,
String truststoreFileName, String trustStorePassword) {
this.keystoreFileName = keystoreFileName;
this.keyStorePassword = keyStorePassword;
this.truststoreFileName = truststoreFileName;
this.trustStorePassword = trustStorePassword;
}
private void open(String method, String urlstr) {
try {
this.urlstr = urlstr;
httpsMode = urlstr.startsWith("https://");
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession
session) {
System.out.println("Warning: URL Host: "+urlHostName+"
vs. "+session.getPeerHost());
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
URL url = new URL(urlstr);
if(httpsMode) {
System.setProperty("java.protocol.handler.pkgs","
javax.net.ssl");
Security.setProperty("java.protocol.handler.pkgs","
javax.net.ssl");
System.setProperty("javax.net.ssl.keyStore",
keystoreFileName);
System.out.println("keystore file: " + keystoreFileName);
System.setProperty("javax.net.ssl.keyStorePassword",
keyStorePassword);
System.setProperty("javax.net.ssl.trustStore",
truststoreFileName);
System.setProperty("javax.net.ssl.trustStorePassword",
trustStorePassword);
httpsconn = (HttpsURLConnection) url.openConnection();
httpconn = (HttpURLConnection)httpsconn;
// read results...
} else {
httpconn = (HttpURLConnection) url.openConnection();
}
httpconn.setRequestMethod(method);
httpconn.setDoInput(true);
if(method.equalsIgnoreCase("POST")) {
httpconn.setDoOutput(true);
pout = new PrintWriter(new OutputStreamWriter(httpconn
.getOutputStream()), true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void close() {
if(pout != null) pout.close();
httpconn.disconnect();
}
private String receiveResponse() throws IOException {
String res = "";
String line = "";
lastResponseCode = httpconn.getResponseCode();
System.out.println("ResponseCode:" + lastResponseCode);
if (lastResponseCode < 500/*HttpURLConnection.HTTP_OK*/)
System.out.println("Posted ok!");
else {
System.out.println("Bad post...\n");
throw new IOException();
}
InputStream s1 = httpconn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(s1));
while ((line = in.readLine()) != null)
res += line;
lastResponseContent = res;
return res;
}
/**
* @param xml
* @throws IOException
*/
public void get(String urlstr) throws IOException {
System.out.println("GET " + urlstr);
open("GET", urlstr);
System.out.println(receiveResponse());
close();
}
public void post(String urlstr, String xml) throws IOException {
System.out.println("POST " + urlstr);
open("POST", urlstr);
pout.print(xml);
pout.flush();
System.out.println("response:" + receiveResponse());
close();
}
public int getLastResponseCode() {
return lastResponseCode;
}
public String getLastResponseContent() {
return lastResponseContent;
}
public static void main(String[] args) {
try {
LightClient lc = new LightClient(args[2], args[3], args[4],
args[5]);
try {
if(args[0].equalsIgnoreCase("POST")) {
lc.post(args[1], "Hello World!");
} else if(args[0].equalsIgnoreCase("GET")) {
lc.get(args[1]);
} else throw new ArrayIndexOutOfBoundsException();
System.out.println("Code:" + lc.getLastResponseCode());
System.out.println("Content:" + lc.getLastResponseContent
());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Content:" + lc.getLastResponseContent());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("java LightClient <POST|GET>
<keystoreFileName> <keyStorePassword> <truststoreFileName>
<trustStorePassword>");
}
}
/**
* @return Returns the urlstr.
*/
public String getUrlstr() {
return urlstr;
}
/**
* @param urlstr The urlstr to set.
*/
public void setUrlstr(String urlstr) {
this.urlstr = urlstr;
}
}
On 10/19/06, Olaj Péter <polaj at iqs.hu> wrote:
>
> Sziasztok!
>
> Egy Java progiból adatokat kellene küldeni és fogadni egy külső
> rendszertől kódolva. A külső rendszer (nem Java, PHP) üzemeltetői adják
> meg a .cert, .key kulcsokat és a passpharse-t. Kaptam egy PHP progit,
> amiben működik a dolog a kulcsokkal, szépen kódol és dekódol, viszont a
> Javas megvalósítással elakadtam. Tud valaki küldeni esetleg egy működő
> kódrészletet? Vagy valami példát, linket, stb?
>
> Köszönettel
> Olaj Péter
> _______________________________________________
> Javalist mailing list
> Javalist at javagrund.hu
> http://javagrund.hu/mailman/listinfo/javalist
>
--------- következő rész ---------
Egy csatolt HTML állomány át lett konvertálva...
URL: http://javagrund.hu/pipermail/javalist/attachments/20061019/eb4f6caf/attachment-0001.html
További információk a(z) Javalist levelezőlistáról