java - Speck algorithm not working -
i trying implement speck 64bit block / 128bit key cipher in java. i'm stuck @ encryption / decryption algorithm. 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 = 0; < 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 = 0; < t; i++) { x = (rotateleft(x, alpha) + y) ^ k[i]; y = rotateright(y, beta) ^ x; //system.out.println(y); }
decryption:
/* *************** key extenstion ***************** */ for(int = 0; < 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 = t-1; >= 0; i--) { y = rotateright(y, beta) ^ x; x = (rotateleft(x, alpha) - y) ^ k[i]; //system.out.println(y); }
x , y initialized 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 content int array of 8 characters.
i put decryption right behind encryption, see if algorithm working, isn't , don't know why. (i reset variables proper values, before used decryption).
references
- the simon , speck families of lightweight block ciphers https://eprint.iacr.org/2013/404
- speck implementation (not mine)
https://github.com/timw/bc-java/blob/feature/simon-speck/core/src/main/java/org/bouncycastle/crypto/engines/speckengine.java
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); }
finally figured out. decryption algorithm should this:
for(int = t-1; >= 0; i--) { y = rotateright(x ^ y, beta); x = rotateleft((x ^ k[i]) - y, alpha); }
and accidently swap rotate functions in encryption algorithm. correct form:
for(int = 0; < t; i++) { x = (rotateright(x, alpha) + y) ^ k[i]; y = rotateleft(y, beta) ^ x; }
Comments
Post a Comment