Amphoru
Amphoru

Reputation: 89

Speck algorithm not working

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:

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:

Upvotes: 3

Views: 1365

Answers (1)

Amphoru
Amphoru

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

Related Questions