20 package net.sf.gridarta.utils;
22 import org.jetbrains.annotations.NotNull;
38 private static final int DELTA = 0x9E3779B9;
44 private final int[]
k =
new int[4];
50 public Xtea(@NotNull
final byte[] key) {
51 if (key.length != 16) {
52 throw new IllegalArgumentException(
"invalid key length");
66 public void encrypt(@NotNull
final byte[] plaintext, @NotNull
final byte[] ciphertext) {
67 if (plaintext.length != 8) {
68 throw new IllegalArgumentException();
70 if (ciphertext.length != 8) {
71 throw new IllegalArgumentException();
73 int v0 =
getInt(plaintext, 0);
74 int v1 =
getInt(plaintext, 4);
77 v0 += (((v1 << 4) ^ (v1 >>> 5)) + v1) ^ (sum + k[sum & 3]);
79 v1 += (((v0 << 4) ^ (v0 >>> 5)) + v0) ^ (sum + k[(sum >>> 11) & 3]);
90 public void decrypt(@NotNull
final byte[] ciphertext, @NotNull
final byte[] plaintext) {
91 if (ciphertext.length != 8) {
92 throw new IllegalArgumentException();
94 if (plaintext.length != 8) {
95 throw new IllegalArgumentException();
97 int v0 =
getInt(ciphertext, 0);
98 int v1 =
getInt(ciphertext, 4);
102 v1 -= (((v0 << 4) ^ (v0 >>> 5)) + v0) ^ (sum + k[(sum >>> 11) & 3]);
104 v0 -= (((v1 << 4) ^ (v1 >>> 5)) + v1) ^ (sum + k[sum & 3]);
116 private static int getInt(@NotNull
final byte[] data,
final int offset) {
117 return ((data[offset + 3] & 255) << 24) | ((data[offset + 2] & 255) << 16) | ((data[offset + 1] & 255) << 8) | data[offset] & 255;
126 private static void putInt(
final int value, @NotNull
final byte[] data,
final int offset) {
127 data[offset + 3] = (byte) (value >>> 24);
128 data[offset + 2] = (byte) (value >>> 16);
129 data[offset + 1] = (byte) (value >>> 8);
130 data[offset] = (byte) value;
void encrypt(@NotNull final byte[] plaintext, @NotNull final byte[] ciphertext)
Encrypts a data block.
void decrypt(@NotNull final byte[] ciphertext, @NotNull final byte[] plaintext)
Decrypts a data block.
static void putInt(final int value, @NotNull final byte[] data, final int offset)
Converts an.
Implements the XTEA algorithm.
static final int NUM_ROUNDS
The number of rounds.
static final int DELTA
The magic constant (1<<32)/PHI.
Xtea(@NotNull final byte[] key)
Creates a new instance.
static int getInt(@NotNull final byte[] data, final int offset)
Converts 4 data bytes to an.