View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.mule.security;
8   
9   import org.mule.security.SecretKeyEncryptionStrategy;
10  import org.mule.security.SecretKeyFactory;
11  import org.mule.tck.junit4.AbstractMuleTestCase;
12  
13  import org.junit.Test;
14  
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertNotSame;
17  
18  public class SecretKeyEncryptionStrategyTestCase extends AbstractMuleTestCase
19  {
20  
21      @Test
22      public void testRoundTripEncryptionBlowfish() throws Exception
23      {
24          SecretKeyEncryptionStrategy ske = new SecretKeyEncryptionStrategy();
25          ske.setAlgorithm("Blowfish");
26          ske.setKey("shhhhh");
27          ske.initialise();
28  
29          byte[] b = ske.encrypt("hello".getBytes(), null);
30  
31          assertNotSame(new String(b), "hello");
32          String s = new String(ske.decrypt(b, null), "UTF-8");
33          assertEquals("hello", s);
34      }
35  
36      @Test
37      public void testRoundTripEncryptionBlowfishWithKeyFactory() throws Exception
38      {
39          SecretKeyEncryptionStrategy ske = new SecretKeyEncryptionStrategy();
40          ske.setAlgorithm("Blowfish");
41          ske.setKeyFactory(new SecretKeyFactory()
42          {
43              public byte[] getKey()
44              {
45                  return "shhhh".getBytes();
46              }
47          });
48          ske.initialise();
49  
50          byte[] b = ske.encrypt("hello".getBytes(), null);
51  
52          assertNotSame(new String(b), "hello");
53          String s = new String(ske.decrypt(b, null), "UTF-8");
54          assertEquals("hello", s);
55      }
56  
57      @Test
58      public void testRoundTripEncryptionTripleDES() throws Exception
59      {
60          SecretKeyEncryptionStrategy ske = new SecretKeyEncryptionStrategy();
61          ske.setAlgorithm("TripleDES");
62          ske.setKey("shhhhh");
63  
64          ske.initialise();
65  
66          byte[] b = ske.encrypt("hello".getBytes(), null);
67  
68          assertNotSame(new String(b), "hello");
69          String s = new String(ske.decrypt(b, null), "UTF-8");
70          assertEquals("hello", s);
71      }
72  
73  }