I am trying to send some encrypted integers between Java as client and C as Server, using symmetric key and here is the code .... The problem is that I can Not read the numbers or the integers!!!! I am using writeInt function on Java side to send the number of digits first to use it in a loop to encrypt/decrypt each number separately, then I use the symmetric key to encrypt/decrypt the whole number. Any idea how to make it work ?
This the java code:
public void sendData(int data) throws IOException
{
int dataInInt = data, numberOfDigits = 0;
System.out.println("DATAININT = " + dataInInt);
while(dataInInt != 0)
{
dataInInt /= 10;
numberOfDigits++;
}
dataInInt = data;
System.out.println("Number of digits to be sent = " + numberOfDigits);
dout.writeInt(numberOfDigits);
int [] dataInDigits = new int [numberOfDigits];
for (int j = (numberOfDigits - 1); j >= 0; j--)
{
dataInDigits[j] = dataInInt % 10;
dataInInt /= 10;
}
int [] dataToSendInt = new int [numberOfDigits];
for (int i = 0; i < dataToSendInt.length; i++)
{
dataToSendInt[i] = dataInDigits[i] ^ symmetricKeyInDigits[i % symmetricKeyInDigits.length];
System.out.println("TOSEND = " + dataToSendInt[i]);
dout.writeInt(dataToSendInt[i]);
}
}
This is the C side:
char *receiveData(int sock)
{
int counter = 0, i = 0, j = 0;
char buffer[256];
bzero(buffer, 256);
int tempKey = symmetricKeyInt, KeyDigitsNumber = 0;
while (tempKey > 0)
{
tempKey /= 10;
KeyDigitsNumber++;
}
tempKey = symmetricKeyInt;
int KeyArr[KeyDigitsNumber];
for (j = KeyDigitsNumber - 1; j >= 0; j--)
{
KeyArr[j] = tempKey % 10;
tempKey = tempKey / 10;
}
read(sock, buffer, 255);
int bufferSize = ntohl(*((int*)&buffer))
char *temp;
char *decrypted = malloc(bufferSize);
char numBuffer[20];
int testInt;
int encInt;
for (j = 0; j < bufferSize; j++)
{
bzero(numBuffer, 20);
read(sock, numBuffer, 19);
testInt = ntohl(*((int*)&numBuffer)) ^ KeyArr[j %KeyDigitsNumber];
sprintf(temp, "%d", testInt);
decrypted[j] = temp;
}
printf("THis from inside receiveData function: %s\n", (char *)decrypted);
return decrypted;
}
Instead of sending them as integers and using writeInt, rather send the integers as a string and convert them on either side. Also use a printWriter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.