1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
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 Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.tests.services.security;
18
19
20 import javax.crypto.KeyGenerator;
21 import javax.crypto.SecretKey;
22
23 import org.apache.commons.codec.binary.Base64;
24 import org.junit.Before;
25 import org.junit.Test;
26
27 import static org.junit.Assert.*;
28
29 import ch.elca.el4j.services.security.encryption.AESCipher;
30
31
32 /**
33 *
34 * This class tests the encryption and decryption using AES.
35 *
36 * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/security/src/test/java/ch/elca/el4j/tests/services/security/AESEncryptionTest.java $
37 *
38 * @author Dominik Zindel (DZI)
39 */
40 public class AESEncryptionTest {
41
42 /**
43 * The cipher used for en- and decryption.
44 */
45 private AESCipher m_cipher;
46
47 /**
48 * The generated key.
49 */
50 private String m_key;
51
52 /**
53 * The text that will be en- and decrypted.
54 */
55 private String m_text = "myTopSecretPassword";
56
57 /**
58 * Generates a cipher with a key.
59 */
60 @Before
61 public void setUp() throws Exception {
62 KeyGenerator kgen = KeyGenerator.getInstance("AES");
63 kgen.init(128);
64
65 // Generate the secret key specs.
66 SecretKey skey = kgen.generateKey();
67 byte[] raw = skey.getEncoded();
68
69 m_key = new String(Base64.encodeBase64(raw));
70 m_cipher = new AESCipher(m_key);
71 }
72
73 /**
74 * Encrypts and then decrypts a text and checks if the result of the decryption
75 * equals the input of the encryption (= the original text).
76 */
77 @Test
78 public void testEnDecryption() {
79 String encrypted = m_cipher.encrypt(m_text);
80 String decrypted = m_cipher.decrypt(encrypted);
81 assertEquals(m_text, decrypted);
82 }
83
84
85 /**
86 * Tests the encryption of a text.
87 */
88 @Test
89 public void testEncryption() {
90 String key = "wNGjAjybVC1FfQn628cU0w==";
91 String expectedEncrypted = "{AES-128}kmzDaaD2WTU4MblFz5396rxOHw9VthnIFiJmESbvn08=";
92 AESCipher cipher = new AESCipher(key);
93
94 String encrypted = cipher.encrypt(m_text);
95
96 assertEquals(expectedEncrypted, encrypted);
97 }
98
99 /**
100 * Tests the decryption of an encrypted text.
101 */
102 @Test
103 public void testDecryption() {
104 String key = "wNGjAjybVC1FfQn628cU0w==";
105 String encrypted = "{AES-128}kmzDaaD2WTU4MblFz5396rxOHw9VthnIFiJmESbvn08=";
106 AESCipher cipher = new AESCipher(key);
107
108 String decrypted = cipher.decrypt(encrypted);
109 assertEquals(m_text, decrypted);
110 }
111
112 }