Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.net.InetAddress;
  5. import java.net.UnknownHostException;
  6. import java.security.KeyManagementException;
  7. import java.security.KeyStore;
  8. import java.security.KeyStoreException;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.security.UnrecoverableKeyException;
  11. import java.security.cert.CertificateException;
  12. import java.util.Random;
  13.  
  14. import javax.net.ssl.*;
  15.  
  16. public class Server {
  17.  
  18. private String ksname;
  19. private char kspass[], ctpass[];
  20. private int port;
  21. private InetAddress ip;
  22.  
  23. public Server(){
  24. ksname = null;
  25. kspass = new char[200];
  26. ctpass = new char[200];
  27. kspass = null;ctpass = null;
  28. port = 10000 + (new Random()).nextInt(10000);
  29. try {
  30. ip = InetAddress.getByName("127.0.0.1");
  31. } catch (UnknownHostException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. public Server(String cname, String cpass, String pass, int p, String host){
  38. ksname = cname;
  39. kspass = new char[200];
  40. kspass = cpass.toCharArray();
  41. ctpass = new char[200];
  42. ctpass = pass.toCharArray();
  43. port = p;
  44. try {
  45. ip = InetAddress.getByName(host);
  46. } catch (UnknownHostException e) {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. }
  50. }
  51.  
  52. public SSLServerSocket getServer(){
  53. SSLServerSocket s = null;
  54. try {
  55. KeyStore ks = KeyStore.getInstance("JKS");
  56. ks.load(new FileInputStream(ksname), kspass);
  57. KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
  58. kmf.init(ks, ctpass);
  59. SSLContext sc = SSLContext.getInstance("TLS");
  60. sc.init(kmf.getKeyManagers(), null, null);
  61. SSLServerSocketFactory ssf = sc.getServerSocketFactory();
  62. s = (SSLServerSocket) ssf.createServerSocket(port,0,ip);
  63. System.out.println("Server created on port " + port +"n");
  64. } catch (KeyStoreException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. } catch (NoSuchAlgorithmException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. } catch (CertificateException e) {
  71. // TODO Auto-generated catch block
  72. e.printStackTrace();
  73. } catch (FileNotFoundException e) {
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. } catch (IOException e) {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. } catch (KeyManagementException e) {
  80. // TODO Auto-generated catch block
  81. e.printStackTrace();
  82. } catch (UnrecoverableKeyException e) {
  83. // TODO Auto-generated catch block
  84. e.printStackTrace();
  85. }
  86. return s;
  87. }
  88. }
  89.  
  90. import java.io.BufferedReader;
  91. import java.io.BufferedWriter;
  92. import java.io.IOException;
  93. import java.io.InputStreamReader;
  94. import java.io.OutputStreamWriter;
  95. import java.util.ArrayList;
  96. import java.util.Map;
  97. import java.util.Random;
  98.  
  99. import javax.net.ssl.*;
  100.  
  101. public class MServer {
  102.  
  103. private SSLSocket c = null;
  104. private BufferedReader r = null;
  105. private BufferedWriter w = null;
  106. private int port = 10579;
  107.  
  108. private void execute(){
  109. Server server = new Server("ppp.jks", "user", "pass", port, "127.0.0.1");
  110. SSLServerSocket s = server.getServer();
  111. printServerSocketInfo(s);
  112. try {
  113. c = (SSLSocket) s.accept();
  114. printSocketInfo(c);
  115. r = new BufferedReader(new InputStreamReader(c.getInputStream()));
  116. w = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));
  117.  
  118. System.out.println("in m");
  119. System.out.flush();
  120. String str = null;
  121. str = r.readLine();
  122. System.out.println(str);
  123. if(str.equals("hi")){
  124. w.write("Connection OK");
  125. System.out.println("Connection OK2");
  126. }
  127.  
  128. while((str = r.readLine()) != null){
  129. if(str.equals("done")){
  130. break;
  131. }
  132. resolve(str);
  133. }
  134.  
  135. } catch (IOException e) {
  136. // TODO Auto-generated catch block
  137. e.printStackTrace();
  138. }
  139.  
  140. }
  141.  
  142. private void resolve(String str){
  143. //does something
  144. }
  145.  
  146. private void printMessage(int returnValue, int correctReturnValue, String printFor){
  147. //something
  148. }
  149.  
  150. private static void printSocketInfo(SSLSocket s) {
  151. System.out.println("Socket class: "+s.getClass());
  152. System.out.println(" Remote address = "
  153. +s.getInetAddress().toString());
  154. System.out.println(" Remote port = "+s.getPort());
  155. System.out.println(" Local socket address = "
  156. +s.getLocalSocketAddress().toString());
  157. System.out.println(" Local address = "
  158. +s.getLocalAddress().toString());
  159. System.out.println(" Local port = "+s.getLocalPort());
  160. System.out.println(" Need client authentication = "
  161. +s.getNeedClientAuth());
  162. SSLSession ss = s.getSession();
  163. System.out.println(" Cipher suite = "+ss.getCipherSuite());
  164. System.out.println(" Protocol = "+ss.getProtocol());
  165. }
  166.  
  167. private static void printServerSocketInfo(SSLServerSocket s) {
  168. System.out.println("Server socket class: "+s.getClass());
  169. System.out.println(" Socket address = "
  170. +s.getInetAddress().toString());
  171. System.out.println(" Socket port = "
  172. +s.getLocalPort());
  173. System.out.println(" Need client authentication = "
  174. +s.getNeedClientAuth());
  175. System.out.println(" Want client authentication = "
  176. +s.getWantClientAuth());
  177. System.out.println(" Use client mode = "
  178. +s.getUseClientMode());
  179. }
  180.  
  181. public static void main(String args[]){
  182. MServer m = new MServer();
  183. m.execute();
  184. }
  185.  
  186. }
  187.  
  188. import java.io.BufferedReader;
  189. import java.io.BufferedWriter;
  190. import java.io.IOException;
  191. import java.io.InputStreamReader;
  192. import java.io.OutputStreamWriter;
  193.  
  194. import javax.net.ssl.SSLServerSocket;
  195. import javax.net.ssl.SSLSession;
  196. import javax.net.ssl.SSLSocket;
  197. import javax.net.ssl.SSLSocketFactory;
  198.  
  199. public class ReqResServer {
  200. private BufferedWriter w1 = null;
  201. private BufferedWriter w2 = null;
  202. private BufferedReader r1 = null;
  203. private BufferedReader r2 = null;
  204. private SSLSocket c1 = null;
  205. private SSLSocket c2 = null;
  206. private int port = 24910;
  207.  
  208. public void doClient(){
  209. SSLSocketFactory f = (SSLSocketFactory) SSLSocketFactory.getDefault();
  210. System.out.println("f created");
  211. try {
  212. c1 = (SSLSocket) f.createSocket("127.0.0.1", 10579);
  213. System.out.println("c1 created");
  214. printSocketInfo(c1);
  215. c1.startHandshake();
  216. w1 = new BufferedWriter(new OutputStreamWriter(c1.getOutputStream()));
  217. r1 = new BufferedReader(new InputStreamReader(c1.getInputStream()));
  218.  
  219. w1.write("hi");
  220. System.out.println("hi");
  221. String str = r1.readLine();
  222. System.out.println("before if " + str);
  223. if(str.equals("Connection OK")){
  224. //do something
  225. System.out.println(str);
  226. }
  227.  
  228. } catch (IOException e) {
  229. // TODO Auto-generated catch block
  230. e.printStackTrace();
  231. }
  232. }
  233.  
  234. private static void printSocketInfo(SSLSocket s) {
  235. System.out.println("Socket class: "+s.getClass());
  236. System.out.println(" Remote address = "
  237. +s.getInetAddress().toString());
  238. System.out.println(" Remote port = "+s.getPort());
  239. System.out.println(" Local socket address = "
  240. +s.getLocalSocketAddress().toString());
  241. System.out.println(" Local address = "
  242. +s.getLocalAddress().toString());
  243. System.out.println(" Local port = "+s.getLocalPort());
  244. System.out.println(" Need client authentication = "
  245. +s.getNeedClientAuth());
  246. SSLSession ss = s.getSession();
  247. System.out.println(" Cipher suite = "+ss.getCipherSuite());
  248. System.out.println(" Protocol = "+ss.getProtocol());
  249. }
  250.  
  251. public static void main(String args[]){
  252. ReqResServer req1 = new ReqResServer();
  253. req1.doClient();
  254. }
  255.  
  256. }
  257.  
  258. Server created on port 10579
  259.  
  260. Server socket class: class sun.security.ssl.SSLServerSocketImpl
  261. Socket address = /127.0.0.1
  262. Socket port = 10579
  263. Need client authentication = false
  264. Want client authentication = false
  265. Use client mode = false
  266. Socket class: class sun.security.ssl.SSLSocketImpl
  267. Remote address = /127.0.0.1
  268. Remote port = 51297
  269. Local socket address = /127.0.0.1:10579
  270. Local address = /127.0.0.1
  271. Local port = 10579
  272. Need client authentication = false
  273. Cipher suite = TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
  274. Protocol = TLSv1.2
  275. in m
  276. null
  277.  
  278. f created
  279. c1 created
  280. Socket class: class sun.security.ssl.SSLSocketImpl
  281. Remote address = /127.0.0.1
  282. Remote port = 10579
  283. Local socket address = /127.0.0.1:51297
  284. Local address = /127.0.0.1
  285. Local port = 51297
  286. Need client authentication = false
  287. Cipher suite = TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
  288. Protocol = TLSv1.2
  289. hi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement