no need to send the encrypted block twice

This commit is contained in:
Sebastian Hugentobler 2022-03-22 21:23:49 +01:00
parent 22fcf324e4
commit f78d2e8124
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0
2 changed files with 4 additions and 5 deletions

View File

@ -40,13 +40,12 @@ public class CTR {
*
* @param block Encrypted block (only lower 16 bits get looked at).
* @param idx Block index.
* @param y Y at index idx.
* @return Decrypted block (in the lower 16 bits of the int).
*/
public int decrypt(int block, int idx, int y) {
public int decrypt(int block, int idx) {
int e = (iv + idx) % (1 << BLOCK_LENGTH); // iv + i mod 2^16
e = spn.encryptBlock(key, e); // yes, we need the encryption function, as this is CTR
return y ^ e;
return block ^ e;
}
}

View File

@ -31,8 +31,8 @@ public class Decrypt {
var ctr = new CTR(iv, key);
for (int i = 0; i < data.length - 1; i++) {
int block = data[i];
int decryptedBlock = ctr.decrypt(block, i, data[i + 1]); // y is i + 1 because i = 0 is the iv
int block = data[i + 1]; // index is i + 1 because i = 0 is the iv, hence the encrypted block is offset by 1
int decryptedBlock = ctr.decrypt(block, i);
decryptedData[i * 2] = (byte) (decryptedBlock >>> 8); // get the upper half of the decrypted block
decryptedData[i * 2 + 1] = (byte) (decryptedBlock & 0xFF); // and the lower half