I'm writing some custom encryption code in Java for my server. The only problem is that in my encryption I'm using XOR. The data gets encrypted fine, but when I decrypt it doesn't decrypt correctly. I am not sure what is cuasing this problem.
Here is my code that I have so far:
// Transform (encrypt) data.
public long[] encryptData(long[] data) {
int dataSize = data.length;
long[] encryptedBytes = new long[dataSize];
long dataByte = 0;
int passwordIndex;
// Encrypt the data with the first 4 sets.
for(int dataIndex = 0; dataIndex < dataSize; dataIndex++) {
dataByte = data[dataIndex];
for(int primeOrderIndex = 0; primeOrderIndex < 4; primeOrderIndex++) {
for(int primeSetIndex = 0; primeSetIndex < 256; primeSetIndex++) {
dataByte += this.primes[this.orderOfSets[primeOrderIndex]][primeSetIndex];
}
}
encryptedBytes[dataIndex] = dataByte;
}
// Encrypt the data with the password.
passwordIndex = 0;
for(int dataIndex = 0; dataIndex < dataSize; dataIndex++) {
dataByte = encryptedBytes[dataIndex];
if(passwordIndex == this.password.length()) {
passwordIndex = 0;
}
dataByte = dataByte ^ (int)this.password.charAt(passwordIndex);
encryptedBytes[dataIndex] = dataByte;
}
// Encrypt the data with the last 4 sets.
for(int dataIndex = 0; dataIndex < dataSize; dataIndex++) {
dataByte = data[dataIndex];
for(int primeOrderIndex = 4; primeOrderIndex < 8; primeOrderIndex++) {
for(int primeSetIndex = 0; primeSetIndex < 256; primeSetIndex++) {
dataByte += this.primes[this.orderOfSets[primeOrderIndex]][primeSetIndex];
}
}
encryptedBytes[dataIndex] = dataByte;
}
return encryptedBytes;
}
// Decrypts the data.
public long[] decryptData(long[] encryptedData) {
long[] decryptedBytes = new long[encryptedData.length];
int dataSize = encryptedData.length;
long dataByte;
int passwordIndex;
// Decrypt the data with the last 4 sets.
for(int dataIndex = 0; dataIndex < dataSize; dataIndex++) {
dataByte = encryptedData[dataIndex];
for(int primeOrderIndex = 7; primeOrderIndex >= 4; primeOrderIndex--) {
for(int primeSetIndex = 255; primeSetIndex >= 0; primeSetIndex--) {
dataByte -= this.primes[this.orderOfSets[primeOrderIndex]][primeSetIndex];
}
}
decryptedBytes[dataIndex] = dataByte;
}
// Decrypt the data with the password.
passwordIndex = 0;
for(int dataIndex = 0; dataIndex < dataSize; dataIndex++) {
dataByte = decryptedBytes[dataIndex];
if(passwordIndex == this.password.length()) {
passwordIndex = 0;
}
dataByte = dataByte ^ (int)this.password.charAt(passwordIndex);
decryptedBytes[dataIndex] = dataByte;
}
// Decrypt the data with the first 4 prime sets.
for(int dataIndex = 0; dataIndex < dataSize; dataIndex++) {
dataByte = encryptedData[dataIndex];
for(int primeOrderIndex = 3; primeOrderIndex >= 0; primeOrderIndex--) {
for(int primeSetIndex = 255; primeSetIndex >= 0; primeSetIndex--) {
dataByte -= this.primes[this.orderOfSets[primeOrderIndex]][primeSetIndex];
}
}
decryptedBytes[dataIndex] = dataByte;
}
return decryptedBytes;
}
And here is some sample output from the program:
Original: 104 101 108 108 111
Encrypted: 38358030 38358027 38358034 38358034 38358037
Decrypted: 32656297 32656294 32656301 32656301 32656304