View Javadoc

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  import ch.elca.el4j.services.security.encryption.RSACipher;
31  
32  
33  /**
34   * 
35   * This class tests the encryption and decryption using RSA.
36   *
37   * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$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/RSAEncryptionTest.java $
38   *
39   * @author Dominik Zindel (DZI)
40   */
41  public class RSAEncryptionTest {
42  
43  	/**
44  	 * The cipher used for en- and decryption.
45  	 */
46  	private RSACipher m_cipher;
47  	
48  	
49  	/**
50  	 * The text that will be en- and decrypted.
51  	 */
52  	private String m_text = "myTopSecretPassword";
53  	
54  	/**
55  	 * Generates a cipher with a key.
56  	 */
57  	@Before
58  	public void setUp() throws Exception {
59  		m_cipher = new RSACipher(512);
60  	}
61  	
62  	/**
63  	 * Encrypts and then decrypts a text and checks if the result of the decryption
64  	 * equals the input of the encryption (= the original text).
65  	 */
66  	@Test
67  	public void testEnDecryption() {
68  		String encrypted = m_cipher.encrypt(m_text);
69  		String decrypted = m_cipher.decrypt(encrypted);
70  		assertEquals(m_text, decrypted);
71  	}
72  	
73  	/**
74  	 * Encrypts a text, modifies the encrypted text, decrypts it and
75  	 * checks if the result is really different from the original input.
76  	 */
77  	@Test
78  	public void testDiffEnDecryption() {
79  		String encrypted = (m_cipher.encrypt(m_text)).substring(1) + "9";
80  		String decrypted = m_cipher.decrypt(encrypted);
81  		assertFalse(m_text.equals(decrypted));
82  	}
83  	
84  }