Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | package lab.scd.securecomm; |
2 | import java.net.*; | ||
3 | import java.security.KeyPair; | ||
4 | import java.security.KeyPairGenerator; | ||
5 | import java.security.NoSuchAlgorithmException; | ||
6 | import java.security.NoSuchProviderException; | ||
7 | import java.security.PrivateKey; | ||
8 | import java.security.PublicKey; | ||
9 | import java.security.Security; | ||
10 | import java.util.Arrays; | ||
11 | import java.io.*; | ||
12 | |||
13 | import javax.crypto.Cipher; | ||
14 | import javax.crypto.NoSuchPaddingException; | ||
15 | |||
16 | import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
17 | public class SClient { | ||
18 | |||
19 | static KeyPairGenerator rsaKeyPairGen; | ||
20 | static KeyPair rsaKeyPair; | ||
21 | static PublicKey rsaPub; | ||
22 | static PrivateKey rsaPriv; | ||
23 | static Cipher cp; | ||
24 | |||
25 | public static String initEncrypt(){ | ||
26 | // Incarca BoncyCastle security provider | ||
27 | try { | ||
28 | Security.addProvider(new BouncyCastleProvider()); | ||
29 | } | ||
30 | catch(Exception e) { | ||
31 | System.err.println("Eraoare incarcare security provider (" +e.getMessage() + ")"); | ||
32 | } | ||
33 | |||
34 | // Genereaza perechea cheie publica\privata | ||
35 | try { | ||
36 | rsaKeyPairGen = KeyPairGenerator.getInstance("RSA"); | ||
37 | } catch (NoSuchAlgorithmException ex) { | ||
38 | System.out.println("Eroare initializare generator chei."); | ||
39 | return "cryp error"; | ||
40 | } | ||
41 | |||
42 | // initializeaza generatorul de chei | ||
43 | rsaKeyPairGen.initialize(1024); | ||
44 | |||
45 | //genereaza perechea de chei | ||
46 | rsaKeyPair = rsaKeyPairGen.generateKeyPair(); | ||
47 | if (rsaKeyPair == null) { | ||
48 | System.out.println("Eroare generare pereche cheie publica - privata"); | ||
49 | return "cryp error"; | ||
50 | } | ||
51 | |||
52 | // cheia publica RSA | ||
53 | rsaPub = rsaKeyPair.getPublic(); | ||
54 | |||
55 | // cheia privata RSA | ||
56 | rsaPriv = rsaKeyPair.getPrivate(); | ||
57 | |||
58 | return ""; | ||
59 | } | ||
60 | |||
61 | public static String encrypt(String text){ | ||
62 | |||
63 | |||
64 | byte[] cipherText = null, newPlainText = null; | ||
65 | |||
66 | |||
67 | |||
68 | // initializeaza obiectul de tip Cipher folosit pentru criptare decriptare | ||
69 | try { | ||
70 | cp = Cipher.getInstance("RSA"); | ||
71 | } catch (NoSuchAlgorithmException ex) { | ||
72 | System.out.println("Algoritmul de criptare RSA nu exista."); | ||
73 | return "cryp error"; | ||
74 | } catch (NoSuchPaddingException ex) { | ||
75 | |||
76 | System.out.println("Eroare initializare Cipher."); | ||
77 | return "cryp error"; | ||
78 | } | ||
79 | |||
80 | // Criptare | ||
81 | try { | ||
82 | cp.init(Cipher.ENCRYPT_MODE, rsaPub); | ||
83 | cipherText = cp.doFinal(text.getBytes()); | ||
84 | // System.out.println("Text criptat:"+new String(cipherText)); | ||
85 | return new String(cipherText); | ||
86 | } catch (Exception ex) { | ||
87 | System.out.println("Eroare criptare: " + ex.getMessage()); | ||
88 | return "cryp error"; | ||
89 | } | ||
90 | |||
91 | } | ||
92 | |||
93 | static BufferedReader line = new BufferedReader(new InputStreamReader(System.in)); | ||
94 | public static String readKeyboard() throws Exception{ | ||
95 | return line.readLine(); | ||
96 | } | ||
97 | |||
98 | public static void main(String[] args) throws Exception{ | ||
99 | |||
100 | initEncrypt(); | ||
101 | Socket socket=null; | ||
102 | try { | ||
103 | //creare obiect address care identifica adresa serverului | ||
104 | InetAddress server_address =InetAddress.getByName("localhost"); | ||
105 | //se putea utiliza varianta alternativa: InetAddress.getByName("127.0.0.1") | ||
106 | |||
107 | socket = new Socket(server_address,1900); | ||
108 | |||
109 | //construieste fluxul de intrare prin care sunt receptionate datele de la server | ||
110 | BufferedReader in = | ||
111 | new BufferedReader( | ||
112 | new InputStreamReader( | ||
113 | socket.getInputStream())); | ||
114 | |||
115 | //construieste fluxul de iesire prin care datele sunt trimise catre server | ||
116 | // Output is automatically flushed | ||
117 | // by PrintWriter: | ||
118 | PrintWriter out = | ||
119 | new PrintWriter( | ||
120 | new BufferedWriter( | ||
121 | new OutputStreamWriter( | ||
122 | socket.getOutputStream())),true); | ||
123 | |||
124 | |||
125 | for(int i = 0; i < 10; i ++) { | ||
126 | String txt = SClient.readKeyboard(); | ||
127 | txt = SClient.encrypt(txt); | ||
128 | out.print(txt); | ||
129 | out.flush(); | ||
130 | } | ||
131 | |||
132 | out.println("END"); //trimite mesaj care determina serverul sa inchida conexiunea | ||
133 | |||
134 | } | ||
135 | catch (Exception ex) {ex.printStackTrace();} | ||
136 | finally{ | ||
137 | socket.close(); | ||
138 | } | ||
139 | |||
140 | |||
141 | } | ||
142 | |||
143 | } |