Java 从客户端(Android)发送的服务器端RSA公钥加密数据解密失败

Java 从客户端(Android)发送的服务器端RSA公钥加密数据解密失败,java,android,encryption,Java,Android,Encryption,服务器向客户端发送公钥加密数据,客户端也向服务器发送公钥加密数据 客户端解密很好。服务器端不可用 同样的解密程序似乎在Android上也能工作,但在服务器端显然不行 这是我的服务器端解密代码: int bytesRead; int current = 0; FileOutputStream fos = null; byte[] EncryptedClientAES = new byte[10000]; InputStream is = clientSocket.getInputStream();

服务器向客户端发送公钥加密数据,客户端也向服务器发送公钥加密数据

客户端解密很好。服务器端不可用

同样的解密程序似乎在Android上也能工作,但在服务器端显然不行

这是我的服务器端解密代码:

int bytesRead;
int current = 0;
FileOutputStream fos = null;
byte[] EncryptedClientAES = new byte[10000];
InputStream is = clientSocket.getInputStream();
fos = new FileOutputStream("ClientAESFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedClientAES, 0, EncryptedClientAES.length);
current = bytesRead;
System.out.println("Size of read data: " + current);

bos.write(EncryptedClientAES, 0, current);

bos.flush();
bos.close();
fos.close();
clientSocket.close();

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedAESKeyOfClient = null;
decryptedAESKeyOfClient = cipher.doFinal(EncryptedClientAES);
这失败了,IllegalBlockSizeException说数据不应该超过64字节,这使我将字节数组EncryptedClientes的大小更改为64,因为读取数据的大小也是64。向前看,这给了我一个BadPaddingException,然而,同样的过程也适用于Android端

以下是我从Android发送加密数据的代码:

SecureRandom secureRandom = new SecureRandom();
KeyGenerator kg = null;
kg = KeyGenerator.getInstance("AES");
kg.init(128, secureRandom);
SecretKey ClientSecretKey = kg.generateKey(); // Client AES is generated here.

String ServerModulus = ServerRSAPublicKey.substring(40, 194);
String ServerExponent = ServerRSAPublicKey.substring(214, 219);
BigInteger bigInteger = new BigInteger(ServerModulus, 10);
BigInteger bigInteger1 = new BigInteger(ServerExponent);

RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(bigInteger, bigInteger1);
KeyFactory keyFactory = null;
try {
    keyFactory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}
RSAPublicKey publicKeyOfServer = null;
try {
    assert keyFactory != null;
    publicKeyOfServer = (RSAPublicKey) keyFactory.generatePublic(rsaPublicKeySpec);
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}

Cipher c = null;
try {
    c = Cipher.getInstance("RSA");
} catch (NoSuchPaddingException e) {
    e.printStackTrace();
}
try {
    assert c != null;
    c.init(Cipher.ENCRYPT_MODE, publicKeyOfServer);
} catch (InvalidKeyException e) {
    e.printStackTrace();
}

byte[] encryptedBytes = null;
try {
    encryptedBytes = c.doFinal(ClientSecretKey.getEncoded());
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
OutputStream outputStream;
outputStream = client.getOutputStream();
assert encryptedBytes != null;
outputStream.write(encryptedBytes, 0, encryptedBytes.length);

outputStream.flush();
outputStream.close();
client.close(); // closing the connection
我在Android端工作的解密代码是

Socket client = new Socket("192.168.1.169", 4444);

int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
current = bytesRead;
do {
    bytesRead =
            is.read(EncryptedServerAES, current, (EncryptedServerAES.length - current));
    if (bytesRead >= 0) current += bytesRead;
} while (bytesRead > -1);

bos.write(EncryptedServerAES, 0, current);
bos.flush();
fos.close();
bos.close();
client.close(); // closing the connection

// try to decrypt and get AES key back.
BigInteger Modulus;
BigInteger publicExponent;
BigInteger privateExponent;
BigInteger primeP;
BigInteger primeQ;
BigInteger primeExpP;
BigInteger primeExpQ;
BigInteger crtCoeff;
Modulus = new BigInteger(ClientPrivateKey.substring(32, 161));
publicExponent = new BigInteger("10001");
privateExponent = new BigInteger(ClientPrivateKey.substring(198, 327));
primeP = new BigInteger(ClientPrivateKey.substring(334, 399));
primeQ = new BigInteger(ClientPrivateKey.substring(406, 471));
primeExpP = new BigInteger(ClientPrivateKey.substring(486, 551));
primeExpQ = new BigInteger(ClientPrivateKey.substring(566, 631));
crtCoeff = new BigInteger(ClientPrivateKey.substring(646, 711));

Cipher cipher = Cipher.getInstance("RSA");
// realise client's private key string as private key.
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(Modulus, publicExponent, privateExponent, primeP, primeQ, primeExpP, primeExpQ, crtCoeff);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey ClientPrivateKEY = null;
try {
    ClientPrivateKEY = (RSAPrivateKey) keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}
try {
    cipher.init(Cipher.DECRYPT_MODE, ClientPrivateKEY);
} catch (InvalidKeyException e) {
    e.printStackTrace();
}
try {
    decryptedAESKeyOfServer = cipher.doFinal(EncryptedServerAES);
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
Cipher c = null;
try {
    c = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
    Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
}
 {
    try {
        c.init(Cipher.ENCRYPT_MODE, publicKeyofClient);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
byte[] encryptedBytes = null;
 {
    try {
        encryptedBytes = c.doFinal(ServerSecretKey.getEncoded());
    } catch (IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

OutputStream outputStream;
outputStream = clientSocket.getOutputStream();
outputStream.write(encryptedBytes, 0, encryptedBytes.length);

outputStream.flush();
outputStream.close();
clientSocket.close(); // closing the connection
Cipher ciphertest = null;
ciphertest = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
ciphertest.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = null;
encryptedBytes = ciphertest.doFinal(Bytes_to_encrypt);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-1"), PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParams);
byte[] decryptedAESKeyOfServer = null;
decryptedAESKeyOfServer = cipher.doFinal(EncryptedClientAES);
int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
data_size = bytesRead; // data_size is 0.
06-15 15:18:03.619 23489-23927/com.avineshwar.secureclient W/System.err: java.io.FileNotFoundException: ServerAESKeyFile: open failed: EROFS (Read-only file system)
06-15 15:18:03.620 23489-23927/com.avineshwar.secureclient W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
06-15 15:18:03.622 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:128)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:115)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.lang.Thread.run(Thread.java:818)
private class SendMessage extends AsyncTask<Void, Void, Void> {
InputStream is = client.getInputStream();
对于Android(客户端)端的上述成功解密,服务器发送了此数据

Socket client = new Socket("192.168.1.169", 4444);

int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
current = bytesRead;
do {
    bytesRead =
            is.read(EncryptedServerAES, current, (EncryptedServerAES.length - current));
    if (bytesRead >= 0) current += bytesRead;
} while (bytesRead > -1);

bos.write(EncryptedServerAES, 0, current);
bos.flush();
fos.close();
bos.close();
client.close(); // closing the connection

// try to decrypt and get AES key back.
BigInteger Modulus;
BigInteger publicExponent;
BigInteger privateExponent;
BigInteger primeP;
BigInteger primeQ;
BigInteger primeExpP;
BigInteger primeExpQ;
BigInteger crtCoeff;
Modulus = new BigInteger(ClientPrivateKey.substring(32, 161));
publicExponent = new BigInteger("10001");
privateExponent = new BigInteger(ClientPrivateKey.substring(198, 327));
primeP = new BigInteger(ClientPrivateKey.substring(334, 399));
primeQ = new BigInteger(ClientPrivateKey.substring(406, 471));
primeExpP = new BigInteger(ClientPrivateKey.substring(486, 551));
primeExpQ = new BigInteger(ClientPrivateKey.substring(566, 631));
crtCoeff = new BigInteger(ClientPrivateKey.substring(646, 711));

Cipher cipher = Cipher.getInstance("RSA");
// realise client's private key string as private key.
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(Modulus, publicExponent, privateExponent, primeP, primeQ, primeExpP, primeExpQ, crtCoeff);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey ClientPrivateKEY = null;
try {
    ClientPrivateKEY = (RSAPrivateKey) keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}
try {
    cipher.init(Cipher.DECRYPT_MODE, ClientPrivateKEY);
} catch (InvalidKeyException e) {
    e.printStackTrace();
}
try {
    decryptedAESKeyOfServer = cipher.doFinal(EncryptedServerAES);
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
Cipher c = null;
try {
    c = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
    Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
}
 {
    try {
        c.init(Cipher.ENCRYPT_MODE, publicKeyofClient);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
byte[] encryptedBytes = null;
 {
    try {
        encryptedBytes = c.doFinal(ServerSecretKey.getEncoded());
    } catch (IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

OutputStream outputStream;
outputStream = clientSocket.getOutputStream();
outputStream.write(encryptedBytes, 0, encryptedBytes.length);

outputStream.flush();
outputStream.close();
clientSocket.close(); // closing the connection
Cipher ciphertest = null;
ciphertest = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
ciphertest.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = null;
encryptedBytes = ciphertest.doFinal(Bytes_to_encrypt);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-1"), PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParams);
byte[] decryptedAESKeyOfServer = null;
decryptedAESKeyOfServer = cipher.doFinal(EncryptedClientAES);
int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
data_size = bytesRead; // data_size is 0.
06-15 15:18:03.619 23489-23927/com.avineshwar.secureclient W/System.err: java.io.FileNotFoundException: ServerAESKeyFile: open failed: EROFS (Read-only file system)
06-15 15:18:03.620 23489-23927/com.avineshwar.secureclient W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
06-15 15:18:03.622 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:128)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:115)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.lang.Thread.run(Thread.java:818)
private class SendMessage extends AsyncTask<Void, Void, Void> {
InputStream is = client.getInputStream();
这就是我在两侧生成RSA密钥的方式:

我想我是在这里装傻


更新1

Socket client = new Socket("192.168.1.169", 4444);

int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
current = bytesRead;
do {
    bytesRead =
            is.read(EncryptedServerAES, current, (EncryptedServerAES.length - current));
    if (bytesRead >= 0) current += bytesRead;
} while (bytesRead > -1);

bos.write(EncryptedServerAES, 0, current);
bos.flush();
fos.close();
bos.close();
client.close(); // closing the connection

// try to decrypt and get AES key back.
BigInteger Modulus;
BigInteger publicExponent;
BigInteger privateExponent;
BigInteger primeP;
BigInteger primeQ;
BigInteger primeExpP;
BigInteger primeExpQ;
BigInteger crtCoeff;
Modulus = new BigInteger(ClientPrivateKey.substring(32, 161));
publicExponent = new BigInteger("10001");
privateExponent = new BigInteger(ClientPrivateKey.substring(198, 327));
primeP = new BigInteger(ClientPrivateKey.substring(334, 399));
primeQ = new BigInteger(ClientPrivateKey.substring(406, 471));
primeExpP = new BigInteger(ClientPrivateKey.substring(486, 551));
primeExpQ = new BigInteger(ClientPrivateKey.substring(566, 631));
crtCoeff = new BigInteger(ClientPrivateKey.substring(646, 711));

Cipher cipher = Cipher.getInstance("RSA");
// realise client's private key string as private key.
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(Modulus, publicExponent, privateExponent, primeP, primeQ, primeExpP, primeExpQ, crtCoeff);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey ClientPrivateKEY = null;
try {
    ClientPrivateKEY = (RSAPrivateKey) keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}
try {
    cipher.init(Cipher.DECRYPT_MODE, ClientPrivateKEY);
} catch (InvalidKeyException e) {
    e.printStackTrace();
}
try {
    decryptedAESKeyOfServer = cipher.doFinal(EncryptedServerAES);
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
Cipher c = null;
try {
    c = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
    Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
}
 {
    try {
        c.init(Cipher.ENCRYPT_MODE, publicKeyofClient);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
byte[] encryptedBytes = null;
 {
    try {
        encryptedBytes = c.doFinal(ServerSecretKey.getEncoded());
    } catch (IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

OutputStream outputStream;
outputStream = clientSocket.getOutputStream();
outputStream.write(encryptedBytes, 0, encryptedBytes.length);

outputStream.flush();
outputStream.close();
clientSocket.close(); // closing the connection
Cipher ciphertest = null;
ciphertest = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
ciphertest.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = null;
encryptedBytes = ciphertest.doFinal(Bytes_to_encrypt);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-1"), PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParams);
byte[] decryptedAESKeyOfServer = null;
decryptedAESKeyOfServer = cipher.doFinal(EncryptedClientAES);
int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
data_size = bytesRead; // data_size is 0.
06-15 15:18:03.619 23489-23927/com.avineshwar.secureclient W/System.err: java.io.FileNotFoundException: ServerAESKeyFile: open failed: EROFS (Read-only file system)
06-15 15:18:03.620 23489-23927/com.avineshwar.secureclient W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
06-15 15:18:03.622 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:128)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:115)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.lang.Thread.run(Thread.java:818)
private class SendMessage extends AsyncTask<Void, Void, Void> {
InputStream is = client.getInputStream();
它仍在失败

这是我的(Android)客户端加密代码

Socket client = new Socket("192.168.1.169", 4444);

int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
current = bytesRead;
do {
    bytesRead =
            is.read(EncryptedServerAES, current, (EncryptedServerAES.length - current));
    if (bytesRead >= 0) current += bytesRead;
} while (bytesRead > -1);

bos.write(EncryptedServerAES, 0, current);
bos.flush();
fos.close();
bos.close();
client.close(); // closing the connection

// try to decrypt and get AES key back.
BigInteger Modulus;
BigInteger publicExponent;
BigInteger privateExponent;
BigInteger primeP;
BigInteger primeQ;
BigInteger primeExpP;
BigInteger primeExpQ;
BigInteger crtCoeff;
Modulus = new BigInteger(ClientPrivateKey.substring(32, 161));
publicExponent = new BigInteger("10001");
privateExponent = new BigInteger(ClientPrivateKey.substring(198, 327));
primeP = new BigInteger(ClientPrivateKey.substring(334, 399));
primeQ = new BigInteger(ClientPrivateKey.substring(406, 471));
primeExpP = new BigInteger(ClientPrivateKey.substring(486, 551));
primeExpQ = new BigInteger(ClientPrivateKey.substring(566, 631));
crtCoeff = new BigInteger(ClientPrivateKey.substring(646, 711));

Cipher cipher = Cipher.getInstance("RSA");
// realise client's private key string as private key.
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(Modulus, publicExponent, privateExponent, primeP, primeQ, primeExpP, primeExpQ, crtCoeff);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey ClientPrivateKEY = null;
try {
    ClientPrivateKEY = (RSAPrivateKey) keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}
try {
    cipher.init(Cipher.DECRYPT_MODE, ClientPrivateKEY);
} catch (InvalidKeyException e) {
    e.printStackTrace();
}
try {
    decryptedAESKeyOfServer = cipher.doFinal(EncryptedServerAES);
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
Cipher c = null;
try {
    c = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
    Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
}
 {
    try {
        c.init(Cipher.ENCRYPT_MODE, publicKeyofClient);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
byte[] encryptedBytes = null;
 {
    try {
        encryptedBytes = c.doFinal(ServerSecretKey.getEncoded());
    } catch (IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

OutputStream outputStream;
outputStream = clientSocket.getOutputStream();
outputStream.write(encryptedBytes, 0, encryptedBytes.length);

outputStream.flush();
outputStream.close();
clientSocket.close(); // closing the connection
Cipher ciphertest = null;
ciphertest = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
ciphertest.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = null;
encryptedBytes = ciphertest.doFinal(Bytes_to_encrypt);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-1"), PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParams);
byte[] decryptedAESKeyOfServer = null;
decryptedAESKeyOfServer = cipher.doFinal(EncryptedClientAES);
int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
data_size = bytesRead; // data_size is 0.
06-15 15:18:03.619 23489-23927/com.avineshwar.secureclient W/System.err: java.io.FileNotFoundException: ServerAESKeyFile: open failed: EROFS (Read-only file system)
06-15 15:18:03.620 23489-23927/com.avineshwar.secureclient W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
06-15 15:18:03.622 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:128)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:115)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.lang.Thread.run(Thread.java:818)
private class SendMessage extends AsyncTask<Void, Void, Void> {
InputStream is = client.getInputStream();
这是我的服务器端解密代码

Socket client = new Socket("192.168.1.169", 4444);

int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
current = bytesRead;
do {
    bytesRead =
            is.read(EncryptedServerAES, current, (EncryptedServerAES.length - current));
    if (bytesRead >= 0) current += bytesRead;
} while (bytesRead > -1);

bos.write(EncryptedServerAES, 0, current);
bos.flush();
fos.close();
bos.close();
client.close(); // closing the connection

// try to decrypt and get AES key back.
BigInteger Modulus;
BigInteger publicExponent;
BigInteger privateExponent;
BigInteger primeP;
BigInteger primeQ;
BigInteger primeExpP;
BigInteger primeExpQ;
BigInteger crtCoeff;
Modulus = new BigInteger(ClientPrivateKey.substring(32, 161));
publicExponent = new BigInteger("10001");
privateExponent = new BigInteger(ClientPrivateKey.substring(198, 327));
primeP = new BigInteger(ClientPrivateKey.substring(334, 399));
primeQ = new BigInteger(ClientPrivateKey.substring(406, 471));
primeExpP = new BigInteger(ClientPrivateKey.substring(486, 551));
primeExpQ = new BigInteger(ClientPrivateKey.substring(566, 631));
crtCoeff = new BigInteger(ClientPrivateKey.substring(646, 711));

Cipher cipher = Cipher.getInstance("RSA");
// realise client's private key string as private key.
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(Modulus, publicExponent, privateExponent, primeP, primeQ, primeExpP, primeExpQ, crtCoeff);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey ClientPrivateKEY = null;
try {
    ClientPrivateKEY = (RSAPrivateKey) keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}
try {
    cipher.init(Cipher.DECRYPT_MODE, ClientPrivateKEY);
} catch (InvalidKeyException e) {
    e.printStackTrace();
}
try {
    decryptedAESKeyOfServer = cipher.doFinal(EncryptedServerAES);
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
Cipher c = null;
try {
    c = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
    Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
}
 {
    try {
        c.init(Cipher.ENCRYPT_MODE, publicKeyofClient);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
byte[] encryptedBytes = null;
 {
    try {
        encryptedBytes = c.doFinal(ServerSecretKey.getEncoded());
    } catch (IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

OutputStream outputStream;
outputStream = clientSocket.getOutputStream();
outputStream.write(encryptedBytes, 0, encryptedBytes.length);

outputStream.flush();
outputStream.close();
clientSocket.close(); // closing the connection
Cipher ciphertest = null;
ciphertest = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
ciphertest.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = null;
encryptedBytes = ciphertest.doFinal(Bytes_to_encrypt);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-1"), PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParams);
byte[] decryptedAESKeyOfServer = null;
decryptedAESKeyOfServer = cipher.doFinal(EncryptedClientAES);
int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
data_size = bytesRead; // data_size is 0.
06-15 15:18:03.619 23489-23927/com.avineshwar.secureclient W/System.err: java.io.FileNotFoundException: ServerAESKeyFile: open failed: EROFS (Read-only file system)
06-15 15:18:03.620 23489-23927/com.avineshwar.secureclient W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
06-15 15:18:03.622 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:128)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:115)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.lang.Thread.run(Thread.java:818)
private class SendMessage extends AsyncTask<Void, Void, Void> {
InputStream is = client.getInputStream();
错误要么是IllegalBlockSizeException(如果decryptedAESKeyOfServer的大小为随机大字节),要么是BadPaddingException(如果decryptedAESKeyOfServer的大小为256字节;从客户端接收的数据的大小为这么多字节)


更新2

  • 我已经修复了从Android发送到服务器的数据。 在此之前,客户端正在发送null
  • Android正在从服务器读取null,即使服务器正在发送256字节的数据
从服务器读取空值的客户端代码

Socket client = new Socket("192.168.1.169", 4444);

int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
current = bytesRead;
do {
    bytesRead =
            is.read(EncryptedServerAES, current, (EncryptedServerAES.length - current));
    if (bytesRead >= 0) current += bytesRead;
} while (bytesRead > -1);

bos.write(EncryptedServerAES, 0, current);
bos.flush();
fos.close();
bos.close();
client.close(); // closing the connection

// try to decrypt and get AES key back.
BigInteger Modulus;
BigInteger publicExponent;
BigInteger privateExponent;
BigInteger primeP;
BigInteger primeQ;
BigInteger primeExpP;
BigInteger primeExpQ;
BigInteger crtCoeff;
Modulus = new BigInteger(ClientPrivateKey.substring(32, 161));
publicExponent = new BigInteger("10001");
privateExponent = new BigInteger(ClientPrivateKey.substring(198, 327));
primeP = new BigInteger(ClientPrivateKey.substring(334, 399));
primeQ = new BigInteger(ClientPrivateKey.substring(406, 471));
primeExpP = new BigInteger(ClientPrivateKey.substring(486, 551));
primeExpQ = new BigInteger(ClientPrivateKey.substring(566, 631));
crtCoeff = new BigInteger(ClientPrivateKey.substring(646, 711));

Cipher cipher = Cipher.getInstance("RSA");
// realise client's private key string as private key.
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(Modulus, publicExponent, privateExponent, primeP, primeQ, primeExpP, primeExpQ, crtCoeff);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey ClientPrivateKEY = null;
try {
    ClientPrivateKEY = (RSAPrivateKey) keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}
try {
    cipher.init(Cipher.DECRYPT_MODE, ClientPrivateKEY);
} catch (InvalidKeyException e) {
    e.printStackTrace();
}
try {
    decryptedAESKeyOfServer = cipher.doFinal(EncryptedServerAES);
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
Cipher c = null;
try {
    c = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
    Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
}
 {
    try {
        c.init(Cipher.ENCRYPT_MODE, publicKeyofClient);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
byte[] encryptedBytes = null;
 {
    try {
        encryptedBytes = c.doFinal(ServerSecretKey.getEncoded());
    } catch (IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

OutputStream outputStream;
outputStream = clientSocket.getOutputStream();
outputStream.write(encryptedBytes, 0, encryptedBytes.length);

outputStream.flush();
outputStream.close();
clientSocket.close(); // closing the connection
Cipher ciphertest = null;
ciphertest = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
ciphertest.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = null;
encryptedBytes = ciphertest.doFinal(Bytes_to_encrypt);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-1"), PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParams);
byte[] decryptedAESKeyOfServer = null;
decryptedAESKeyOfServer = cipher.doFinal(EncryptedClientAES);
int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
data_size = bytesRead; // data_size is 0.
06-15 15:18:03.619 23489-23927/com.avineshwar.secureclient W/System.err: java.io.FileNotFoundException: ServerAESKeyFile: open failed: EROFS (Read-only file system)
06-15 15:18:03.620 23489-23927/com.avineshwar.secureclient W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
06-15 15:18:03.622 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:128)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:115)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.lang.Thread.run(Thread.java:818)
private class SendMessage extends AsyncTask<Void, Void, Void> {
InputStream is = client.getInputStream();
Idk how,但是,客户端仍在(尝试)解密它,并且没有崩溃

我的堆栈跟踪

Socket client = new Socket("192.168.1.169", 4444);

int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
current = bytesRead;
do {
    bytesRead =
            is.read(EncryptedServerAES, current, (EncryptedServerAES.length - current));
    if (bytesRead >= 0) current += bytesRead;
} while (bytesRead > -1);

bos.write(EncryptedServerAES, 0, current);
bos.flush();
fos.close();
bos.close();
client.close(); // closing the connection

// try to decrypt and get AES key back.
BigInteger Modulus;
BigInteger publicExponent;
BigInteger privateExponent;
BigInteger primeP;
BigInteger primeQ;
BigInteger primeExpP;
BigInteger primeExpQ;
BigInteger crtCoeff;
Modulus = new BigInteger(ClientPrivateKey.substring(32, 161));
publicExponent = new BigInteger("10001");
privateExponent = new BigInteger(ClientPrivateKey.substring(198, 327));
primeP = new BigInteger(ClientPrivateKey.substring(334, 399));
primeQ = new BigInteger(ClientPrivateKey.substring(406, 471));
primeExpP = new BigInteger(ClientPrivateKey.substring(486, 551));
primeExpQ = new BigInteger(ClientPrivateKey.substring(566, 631));
crtCoeff = new BigInteger(ClientPrivateKey.substring(646, 711));

Cipher cipher = Cipher.getInstance("RSA");
// realise client's private key string as private key.
RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(Modulus, publicExponent, privateExponent, primeP, primeQ, primeExpP, primeExpQ, crtCoeff);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey ClientPrivateKEY = null;
try {
    ClientPrivateKEY = (RSAPrivateKey) keyFactory.generatePrivate(rsaPrivateCrtKeySpec);
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}
try {
    cipher.init(Cipher.DECRYPT_MODE, ClientPrivateKEY);
} catch (InvalidKeyException e) {
    e.printStackTrace();
}
try {
    decryptedAESKeyOfServer = cipher.doFinal(EncryptedServerAES);
} catch (IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
}
Cipher c = null;
try {
    c = Cipher.getInstance("RSA");
} catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
    Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
}
 {
    try {
        c.init(Cipher.ENCRYPT_MODE, publicKeyofClient);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}
byte[] encryptedBytes = null;
 {
    try {
        encryptedBytes = c.doFinal(ServerSecretKey.getEncoded());
    } catch (IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(SecureServer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

OutputStream outputStream;
outputStream = clientSocket.getOutputStream();
outputStream.write(encryptedBytes, 0, encryptedBytes.length);

outputStream.flush();
outputStream.close();
clientSocket.close(); // closing the connection
Cipher ciphertest = null;
ciphertest = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
ciphertest.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = null;
encryptedBytes = ciphertest.doFinal(Bytes_to_encrypt);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
OAEPParameterSpec oaepParams = new OAEPParameterSpec("SHA-256", "MGF1", new MGF1ParameterSpec("SHA-1"), PSource.PSpecified.DEFAULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey, oaepParams);
byte[] decryptedAESKeyOfServer = null;
decryptedAESKeyOfServer = cipher.doFinal(EncryptedClientAES);
int bytesRead;
int current = 0;
FileOutputStream fos;
byte[] EncryptedServerAES = new byte[100000];
InputStream is = client.getInputStream();
fos = new FileOutputStream("ServerAESKeyFile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(EncryptedServerAES, 0, EncryptedServerAES.length);
data_size = bytesRead; // data_size is 0.
06-15 15:18:03.619 23489-23927/com.avineshwar.secureclient W/System.err: java.io.FileNotFoundException: ServerAESKeyFile: open failed: EROFS (Read-only file system)
06-15 15:18:03.620 23489-23927/com.avineshwar.secureclient W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
06-15 15:18:03.622 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:128)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at com.avineshwar.secureclient.Part3$SendMessage.doInBackground(Part3.java:115)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-15 15:18:03.623 23489-23927/com.avineshwar.secureclient W/System.err:     at java.lang.Thread.run(Thread.java:818)
private class SendMessage extends AsyncTask<Void, Void, Void> {
InputStream is = client.getInputStream();

您在服务器上使用相同的rsa公钥吗?C和S使用不同的密钥对。公钥作为字符串传输,稍后我将这些字符串转换为两边的公钥。一般建议:始终使用完全限定的密码字符串
Cipher.getInstance(“RSA”)可能会产生不同的密码。现在,您应该使用OAEP而不是默认的PKCS#1 v1.5填充。因此,您可能应该使用Cipher.getInstance(“RSA/ECB/OAEPWithSHA-256和mgf1padding”)@ArtjomB。听起来很周到。我很快会更新。你是如何发送数据的?您是否可能得到不同的字符串(您是否尝试过比较发送和接收的数据)?