我不是经验丰富的Java和Android从业者,但是,我会尝试清楚地解释我的问题。
对所需内容的非描述性解释:
客户端:Android应用,服务器:Localhost
客户端--->服务器,即客户端向服务器发送内容。
客户< ---服务器
客户端--->服务器
客户< ---服务器
基本上应该发生的是由加密的AES密钥交换引导的RSA密钥交换。
描述性解释:
客户端和服务器都在生成RSA密钥对,并且他们打算在本地主机环境中使用客户端 - 服务器模型在我的Android上单击按钮时将公钥(转换为字符串后)发送到彼此应用
这是我的代码,它似乎在Telnet中工作,但是,它涉及到Android时不一样。换句话说,它正在打印我用来验证失败的陈述。
MainActivity.java文件:
package com.avineshwar.secureclient;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
public class MainActivity extends AppCompatActivity {
private Socket client;
private PrintWriter printwriter;
private Button button;
private TextView textView;
private String messsage;
SecureRandom random;
KeyPairGenerator keyPairGenerator;
KeyPair keyPair;
PrivateKey privateKey;
PublicKey publicKey;
InputStreamReader inputStreamReader;
BufferedReader bufferedReader = null;
String server = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
textView = (TextView) findViewById(R.id.textView);
// Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
random = new SecureRandom();
keyPairGenerator = null;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
keyPairGenerator.initialize(2048, random);
keyPair = keyPairGenerator.generateKeyPair();
privateKey = keyPair.getPrivate();
publicKey = keyPair.getPublic();
messsage = String.valueOf(publicKey);
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
if (server == null || server == "null") {
server = "null";
textView.setText(server);
} else {
textView.setText(server);
}
}
}
);
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
try {
client = new Socket("192.168.1.169", 4444); // connect to the server
} catch (IOException e) {
e.printStackTrace();
}
printwriter = null;
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage);
inputStreamReader = null;
inputStreamReader = new InputStreamReader(client.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
server = bufferedReader.readLine();
StringBuilder stringBuilder = new StringBuilder();
while ((server = bufferedReader.readLine()) != null) {
stringBuilder.append(server + "\n");
}
server = stringBuilder.toString();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
//@Override
//public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.slimple_text_client, menu);
// return true;
// }
}
SecureServer.java文件:
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import javax.crypto.KeyGeneratorSpi;
import sun.nio.cs.Surrogate;
public class SecureServer {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args) throws NoSuchAlgorithmException {
SecureRandom random = new SecureRandom();
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048, random);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
try {
serverSocket = new ServerSocket(4444); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started listening on the port 4444");
while (true) {
try {
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = null;
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
message = bufferedReader.readLine();
if (message == null || message == "null") {
message = "null";
}
System.out.println(message);
PrintWriter out = null;
out = new PrintWriter(clientSocket.getOutputStream(), true);
message = publicKey.toString();
out.write(message);
System.out.println(message);
out.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
activity_main.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to send RSA key"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.avineshwar.secureclient">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我确实理解一件事但是,我不确定它是否正确,直到我在Android应用中关闭 PrintWriter对象,数据不是然而,即使我这样做,它也只是解决了场景的一部分,即从Android到服务器的数据。我不确定收集从服务器发送的数据的具体方式虽然它适用于Telnet,即来回传播。
在 SecureServer.java 文件中,此语句
inputStreamReader.close();
如果放在这些陈述之前,
out = new PrintWriter(clientSocket.getOutputStream(), true);
message = publicKey.toString();
out.write(message);
System.out.println(message);
正在抛出一个处理过的IOException,它在消息读取中显示问题。
除此之外,我观察到的是,如果我们正在向套接字写入任何内容,&#34;输出到套接字&#34; stream 也必须立即关闭,即在此流关闭之前,我们无法处理与该套接字相关的任何其他流。
更高级别的观点,我对此问题的体验和处理方法: