Reputation: 89
I am trying to implement Speck 64bit block / 128bit key cipher in java. I'm stuck at encryption / decryption algorithm. My decryption algorithm can't decrypt cipher text properly.
My implementation:
Encryption:
int m = 4; //key words
int T = 27; //rounds
int alpha = 8; //alpha
int beta = 3; //beta
int x,y;
int[] l = new int[2*T], k = new int[T];
/* *************** KEY EXTENSTION ***************** */
for(int i = 0; i < T-1; i++) {
l[i+m-1] = (k[i] + rotateRight(l[i], alpha)) ^ i;
k[i+1] = rotateLeft(k[i], beta) ^ l[i+m-1];
//System.out.println(k[i]);
}
/* *************** ENCRYPTION ********************* */
for(int i = 0; i < T; i++) {
x = (rotateLeft(x, alpha) + y) ^ k[i];
y = rotateRight(y, beta) ^ x;
//System.out.println(y);
}
Decryption:
/* *************** KEY EXTENSTION ***************** */
for(int i = 0; i < T-1; i++) {
l[i+m-1] = (k[i] + rotateRight(l[i], alpha)) ^ i;
k[i+1] = rotateLeft(k[i], beta) ^ l[i+m-1];
//System.out.println(k[i]);
}
/* *************** DECRYPTION ********************* */
for(int i = T-1; i >= 0; i--) {
y = rotateRight(y, beta) ^ x;
x = (rotateLeft(x, alpha) - y) ^ k[i];
//System.out.println(y);
}
x and y are initialized by function boxing(<- little bit weird):
x = boxing(plainText, 0, 1);
y = boxing(plainText, 1, 2);
public static int boxing(int[] content, int i, int count) {
int temp[] = new int[count];
temp[i] |= content[i*4] & 0xff;
temp[i] = temp[i] << 8 | content[i*4+1] & 0xff;
temp[i] = temp[i] << 8 | content[i*4+2] & 0xff;
temp[i] = temp[i] << 8 | content[i*4+3] & 0xff;
//System.out.println(temp[from]);
return temp[i];
}
Note that content is int array of 8 characters.
I put decryption right behind encryption, so I could see if this algorithm is really working, but it isn't and I don't know why. (I reset variables to their proper values, before I used decryption).
References
EDIT:
Rotate functions:
public static int rotateLeft(int number, int amount) {
return number << amount | number >>> (32-amount);
}
public static int rotateRight(int number, int amount) {
return number >>> amount | number << (32-amount);
}
Upvotes: 3
Views: 1365
Reputation: 89
Finally figured out. My decryption algorithm should look like this:
for(int i = T-1; i >= 0; i--) {
y = rotateRight(x ^ y, beta);
x = rotateLeft((x ^ k[i]) - y, alpha);
}
And I accidently swap rotate functions in encryption algorithm. This is correct form:
for(int i = 0; i < T; i++) {
x = (rotateRight(x, alpha) + y) ^ k[i];
y = rotateLeft(y, beta) ^ x;
}
Upvotes: 2