Gridarta Editor
Xtea.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils;
21 
22 import org.jetbrains.annotations.NotNull;
23 
28 public class Xtea {
29 
33  private static final int NUM_ROUNDS = 32;
34 
38  private static final int DELTA = 0x9E3779B9;
39 
43  private final int @NotNull [] k = new int[4];
44 
49  public Xtea(final byte @NotNull [] key) {
50  if (key.length != 16) {
51  throw new IllegalArgumentException("invalid key length");
52  }
53 
54  k[0] = getInt(key, 0);
55  k[1] = getInt(key, 4);
56  k[2] = getInt(key, 8);
57  k[3] = getInt(key, 12);
58  }
59 
65  public void encrypt(final byte @NotNull [] plaintext, final byte @NotNull [] ciphertext) {
66  if (plaintext.length != 8) {
67  throw new IllegalArgumentException("invalid plaintext length: " + plaintext.length);
68  }
69  if (ciphertext.length != 8) {
70  throw new IllegalArgumentException("invalid ciphertext length: " + ciphertext.length);
71  }
72  int v0 = getInt(plaintext, 0);
73  int v1 = getInt(plaintext, 4);
74  int sum = 0;
75  for (int i = 0; i < NUM_ROUNDS; i++) {
76  v0 += (((v1 << 4) ^ (v1 >>> 5)) + v1) ^ (sum + k[sum & 3]);
77  sum += DELTA;
78  v1 += (((v0 << 4) ^ (v0 >>> 5)) + v0) ^ (sum + k[(sum >>> 11) & 3]);
79  }
80  putInt(v0, ciphertext, 0);
81  putInt(v1, ciphertext, 4);
82  }
83 
89  public void decrypt(final byte @NotNull [] ciphertext, final byte @NotNull [] plaintext) {
90  if (ciphertext.length != 8) {
91  throw new IllegalArgumentException("invalid ciphertext length: " + ciphertext.length);
92  }
93  if (plaintext.length != 8) {
94  throw new IllegalArgumentException("invalid plaintext length: " + plaintext.length);
95  }
96  int v0 = getInt(ciphertext, 0);
97  int v1 = getInt(ciphertext, 4);
98  //noinspection NumericOverflow
99  int sum = DELTA * NUM_ROUNDS;
100  for (int i = 0; i < NUM_ROUNDS; i++) {
101  v1 -= (((v0 << 4) ^ (v0 >>> 5)) + v0) ^ (sum + k[(sum >>> 11) & 3]);
102  sum -= DELTA;
103  v0 -= (((v1 << 4) ^ (v1 >>> 5)) + v1) ^ (sum + k[sum & 3]);
104  }
105  putInt(v0, plaintext, 0);
106  putInt(v1, plaintext, 4);
107  }
108 
115  private static int getInt(final byte @NotNull [] data, final int offset) {
116  return ((data[offset + 3] & 255) << 24) | ((data[offset + 2] & 255) << 16) | ((data[offset + 1] & 255) << 8) | data[offset] & 255;
117  }
118 
125  private static void putInt(final int value, final byte @NotNull [] data, final int offset) {
126  data[offset + 3] = (byte) (value >>> 24);
127  data[offset + 2] = (byte) (value >>> 16);
128  data[offset + 1] = (byte) (value >>> 8);
129  data[offset] = (byte) value;
130  }
131 
132 }
net.sf.gridarta.utils.Xtea.k
final int[] k
The state.
Definition: Xtea.java:43
net.sf.gridarta.utils.Xtea.encrypt
void encrypt(final byte @NotNull[] plaintext, final byte @NotNull[] ciphertext)
Encrypts a data block.
Definition: Xtea.java:65
net.sf.gridarta.utils.Xtea.decrypt
void decrypt(final byte @NotNull[] ciphertext, final byte @NotNull[] plaintext)
Decrypts a data block.
Definition: Xtea.java:89
net.sf.gridarta.utils.Xtea.DELTA
static final int DELTA
The magic constant (1<<32)/PHI.
Definition: Xtea.java:38
net.sf.gridarta.utils.Xtea.putInt
static void putInt(final int value, final byte @NotNull[] data, final int offset)
Converts an.
Definition: Xtea.java:125
net.sf.gridarta.utils.Xtea.Xtea
Xtea(final byte @NotNull[] key)
Creates a new instance.
Definition: Xtea.java:49
net.sf.gridarta.utils.Xtea.getInt
static int getInt(final byte @NotNull[] data, final int offset)
Converts 4 data bytes to an.
Definition: Xtea.java:115
net.sf.gridarta.utils.Xtea
Implements the XTEA algorithm.
Definition: Xtea.java:28
net.sf.gridarta.utils.Xtea.NUM_ROUNDS
static final int NUM_ROUNDS
The number of rounds.
Definition: Xtea.java:33