Gridarta Editor
XteaTest.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 java.util.Arrays;
23 import org.jetbrains.annotations.NotNull;
24 import org.junit.Assert;
25 import org.junit.Test;
26 
31 public class XteaTest {
32 
36  private static final byte @NotNull [] KEY = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc, (byte) 0xde, (byte) 0xf0, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef };
37 
41  private static final byte @NotNull [] PLAINTEXT = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08 };
42 
47  private static final byte @NotNull [] IPHERTEXT = { (byte) 0x06, (byte) 0x6a, (byte) 0xb0, (byte) 0x51, (byte) 0xf8, (byte) 0xa7, (byte) 0xa3, (byte) 0xc3 };
48 
52  @Test
53  public void test() {
54  final Xtea xtea = new Xtea(KEY);
55  final byte[][] tmp = new byte[2][8];
56  xtea.encrypt(PLAINTEXT, tmp[0]);
57  xtea.decrypt(tmp[0], tmp[1]);
58 
59  if (!Arrays.equals(tmp[0], IPHERTEXT)) {
60  Assert.fail("encrypt failure");
61  }
62 
63  if (!Arrays.equals(tmp[1], PLAINTEXT)) {
64  Assert.fail("decrypt failure");
65  }
66  }
67 
68 }
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.XteaTest
Regression tests for Xtea.
Definition: XteaTest.java:31
net.sf.gridarta.utils.Xtea
Implements the XTEA algorithm.
Definition: Xtea.java:28
net.sf.gridarta.utils.XteaTest.KEY
static final byte[] KEY
The key for the test.
Definition: XteaTest.java:36
net.sf.gridarta.utils.XteaTest.PLAINTEXT
static final byte[] PLAINTEXT
The plaintext for the test.
Definition: XteaTest.java:41
net.sf.gridarta.utils.XteaTest.test
void test()
Checks that basic encryption and decryption works.
Definition: XteaTest.java:53
net.sf.gridarta.utils.XteaTest.IPHERTEXT
static final byte[] IPHERTEXT
The expected ciphertext corresponding to KEY and {}.
Definition: XteaTest.java:47