我有一个用c#编写的服务器和一个用Java编写的客户端。
对于加密解密部分,我正在使用此方法
public byte[] EncryptAES(String strText) throws Exception {
objCipher.init(Cipher.ENCRYPT_MODE, objSecretKey);
Log.d("datazz", String.valueOf(strText.getBytes("UTF8")));
return objCipher.doFinal(strText.getBytes("UTF8"));
}
public byte[] DecryptAES(byte[] tData) throws Exception {
objCipher.init(Cipher.DECRYPT_MODE, objSecretKey);
return objCipher.doFinal(tData);
}
它没有任何问题,但是当我尝试以更快的速度发送数据时,我开始收到这些错误
javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
java.net.SocketException: sendto failed: EPIPE (Broken pipe)
org.json.JSONException: Unterminated object at character 257
有点难以追踪问题,因为当我试图突破它时,它运行正常,只有在我尝试快速发送数据时才会发生(EG:在一秒钟内向服务器发送5个请求) )它似乎随机崩溃
任何人都知道是什么原因引起的?非常感谢帮助,提前谢谢
这是接收部分
public void run() {
tBuffer = null;
objSocket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(strServerIP, iServerPort);
bTerminate = false;
while(!bTerminate){
try {
if(!objSocket.isConnected()) { // reconnect feature
iNumConnection++;
objSocket.connect(socketAddress);
// successful connect, reset the connection has be make back to zero
iNumConnection = 0;
if(objSocketClientListener != null)
objSocketClientListener.Connected();
}
else {
int numCount = objSocket.getInputStream().available();
if(numCount>0) {
byte[] receivedData = new byte[numCount];
//bytesReceived += objIn.read(tBuffer, bytesReceived, tBuffer.length - bytesReceived);
objSocket.getInputStream().read(receivedData);
//for first time initial
if (tBuffer == null) {
tBuffer = new byte[numCount];
System.arraycopy(receivedData, 0, tBuffer, 0, receivedData.length);
} else {
byte[] newByteArray = new byte[bytesReceived + numCount];
System.arraycopy(tBuffer, 0, newByteArray, 0, bytesReceived);
System.arraycopy(receivedData, 0, newByteArray, bytesReceived, receivedData.length);
tBuffer = new byte[bytesReceived + numCount];
System.arraycopy(newByteArray, 0, tBuffer, 0, newByteArray.length);
}
bytesReceived += numCount;
//bytesReceived += objIn.read(tBuffer, bytesReceived, tBuffer.length - bytesReceived);
while (bytesReceived > 0 && ((iPos = ArrayHelper.IndexOf(tBuffer, bytesReceived, tDelimiter)) >= 0)) {
int iSize = iPos + 1;
//byte[] tEncrypted = Base64.decode(Arrays.copyOf(tBuffer, iPos), Base64.DEFAULT);
byte[] tEncrypted = Base64.decode(Arrays.copyOf(tBuffer, iSize), Base64.DEFAULT);
// remove tBuffer that has be decrypted successful.
//System.arraycopy(tBuffer, iSize, tBuffer, 0, (tBuffer.length - iSize));
System.arraycopy(tBuffer, iSize, tBuffer, 0, (bytesReceived - iSize));
bytesReceived -= iSize;
if (tEncrypted != null && tEncrypted.length > 0) {
byte[] tDecrypted = objCrypto.DecryptAES(tEncrypted);
if (objSocketClientListener != null) {
// call the method messageReceived from Activity class
Log.d("DataZZZZZZZZZZZZZZZZZZZZZz", new String(tDecrypted, "UTF8") + " ");
objSocketClientListener.Received(new String(tDecrypted, "UTF8"));
}
}
}
}
else
{
Thread.sleep(500);
}
}
} catch (IOException e) {
Crittercism.logHandledException(e);
// TODO Auto-generated catch block
resetBuffer();
e.printStackTrace();
if (objSocketClientListener != null)
objSocketClientListener.SocketException(e);
Stop();
} catch (Exception e) {
Crittercism.logHandledException(e);
// TODO Auto-generated catch block
resetBuffer();
e.printStackTrace();
if (objSocketClientListener != null)
objSocketClientListener.GeneralSocketException(e);
Stop();
}
}
}