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-2015 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  @NotNull
44  private final int[] k = new int[4];
45 
50  public Xtea(@NotNull final byte[] key) {
51  if (key.length != 16) {
52  throw new IllegalArgumentException("invalid key length");
53  }
54 
55  k[0] = getInt(key, 0);
56  k[1] = getInt(key, 4);
57  k[2] = getInt(key, 8);
58  k[3] = getInt(key, 12);
59  }
60 
66  public void encrypt(@NotNull final byte[] plaintext, @NotNull final byte[] ciphertext) {
67  if (plaintext.length != 8) {
68  throw new IllegalArgumentException();
69  }
70  if (ciphertext.length != 8) {
71  throw new IllegalArgumentException();
72  }
73  int v0 = getInt(plaintext, 0);
74  int v1 = getInt(plaintext, 4);
75  int sum = 0;
76  for (int i = 0; i < NUM_ROUNDS; i++) {
77  v0 += (((v1 << 4) ^ (v1 >>> 5)) + v1) ^ (sum + k[sum & 3]);
78  sum += DELTA;
79  v1 += (((v0 << 4) ^ (v0 >>> 5)) + v0) ^ (sum + k[(sum >>> 11) & 3]);
80  }
81  putInt(v0, ciphertext, 0);
82  putInt(v1, ciphertext, 4);
83  }
84 
90  public void decrypt(@NotNull final byte[] ciphertext, @NotNull final byte[] plaintext) {
91  if (ciphertext.length != 8) {
92  throw new IllegalArgumentException();
93  }
94  if (plaintext.length != 8) {
95  throw new IllegalArgumentException();
96  }
97  int v0 = getInt(ciphertext, 0);
98  int v1 = getInt(ciphertext, 4);
99  //noinspection NumericOverflow
100  int sum = DELTA * NUM_ROUNDS;
101  for (int i = 0; i < NUM_ROUNDS; i++) {
102  v1 -= (((v0 << 4) ^ (v0 >>> 5)) + v0) ^ (sum + k[(sum >>> 11) & 3]);
103  sum -= DELTA;
104  v0 -= (((v1 << 4) ^ (v1 >>> 5)) + v1) ^ (sum + k[sum & 3]);
105  }
106  putInt(v0, plaintext, 0);
107  putInt(v1, plaintext, 4);
108  }
109 
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;
118  }
119 
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;
131  }
132 
133 }
void encrypt(@NotNull final byte[] plaintext, @NotNull final byte[] ciphertext)
Encrypts a data block.
Definition: Xtea.java:66
void decrypt(@NotNull final byte[] ciphertext, @NotNull final byte[] plaintext)
Decrypts a data block.
Definition: Xtea.java:90
static void putInt(final int value, @NotNull final byte[] data, final int offset)
Converts an.
Definition: Xtea.java:126
Implements the XTEA algorithm.
Definition: Xtea.java:28
static final int NUM_ROUNDS
The number of rounds.
Definition: Xtea.java:33
static final int DELTA
The magic constant (1<<32)/PHI.
Definition: Xtea.java:38
Xtea(@NotNull final byte[] key)
Creates a new instance.
Definition: Xtea.java:50
static int getInt(@NotNull final byte[] data, final int offset)
Converts 4 data bytes to an.
Definition: Xtea.java:116
final int [] k
The state.
Definition: Xtea.java:44