1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
| package com.example.myapplicationna;
import android.app.Activity;
import android.app.Application;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class MainActivity extends AppCompatActivity {
//Button button= (Button) findViewById(R.id.button);
public class Echo {
public void start() {
try {
ServerSocket s = new ServerSocket(9999);
while (true) {
// System.out.println("Ready!");
Socket client = s.accept();
InputStream is = client.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = br.readLine();
OutputStream os = client.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(str);
// System.out.println("Ready!");
client.close();
}
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
public class Echo_client {
public void start() {
try {
EditText editText = (EditText) findViewById(R.id.editText);
Socket sc=new Socket("localhost",9999 );
while (true) {
// System.out.println("Ready!");
// Socket client = s.accept();
InputStream is = sc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = br.readLine();
OutputStream os = sc.getOutputStream();
editText.setText(str);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(str);
// System.out.println("Ready!");
sc.close();
}
} catch (IOException e) {
EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(e.getMessage());
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
////////
private TextView tvClientMsg,tvServerIP,tvServerPort;
class ServerAsyncTask extends AsyncTask<Socket, Void, String> {
//Background task which serve for the client
@Override
protected String doInBackground(Socket... params) {
String result = null;
//Get the accepted socket object
Socket mySocket = params[0];
try {
//Get the data input stream comming from the client
InputStream is = mySocket.getInputStream();
//Get the output stream to the client
PrintWriter out = new PrintWriter(
mySocket.getOutputStream(), true);
//Write data to the data output stream
out.println("Hello from server");
//Buffer the data input stream
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
//Read the contents of the data buffer
result = br.readLine();
//Close the client connection
mySocket.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
//After finishing the execution of background task data will be write the text view
tvClientMsg.setText(s);
}
}
////////
class Srv extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Echo srv=new Echo();
srv.start();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
class Cln extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
try {
Echo_client cl=new Echo_client();
cl.start();
} catch (Exception e) {
e.printStackTrace();
}
return "Stt";
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final EditText editText = (EditText) findViewById(R.id.editText4);
}
public void onMyButtonClick(View view)
{
SecretKeySpec key=genaeskey("hackerisnotacriminal");
byte[] enc= encrypt("qwertyqwertyqwertyuiopqwertyuiop", key);
Toast.makeText(this, decrypt(enc,key), Toast.LENGTH_SHORT).show();
Key[] rsa_keys=genrsakeys(2048);
byte[] t= rsa_encrypt(Base64.decode("popirosa", Base64.DEFAULT), rsa_keys[0]);
String dcr=Base64.encodeToString( rsa_decrypt(t,rsa_keys[1]), Base64.DEFAULT );
Toast.makeText(this,dcr, Toast.LENGTH_SHORT).show();
Srv s=new Srv();
s.execute();
Cln c=new Cln();
c.execute();
c.doInBackground(null);
ServerAsyncTask ssss=new ServerAsyncTask();
ssss.execute();
}
} |