View Javadoc

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