View Javadoc

1   /*
2    * $Id: SecretKeyEncryptionStrategyTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
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.AbstractMuleTestCase;
16  
17  public class SecretKeyEncryptionStrategyTestCase extends AbstractMuleTestCase
18  {
19  
20      public void testRoundTripEncryptionBlowfish() throws Exception
21      {
22          SecretKeyEncryptionStrategy ske = new SecretKeyEncryptionStrategy();
23          ske.setAlgorithm("Blowfish");
24          ske.setKey("shhhhh");
25          ske.initialise();
26  
27          byte[] b = ske.encrypt("hello".getBytes(), null);
28  
29          assertNotSame(new String(b), "hello");
30          String s = new String(ske.decrypt(b, null), "UTF-8");
31          assertEquals("hello", s);
32      }
33  
34      public void testRoundTripEncryptionBlowfishWithKeyFactory() throws Exception
35      {
36          SecretKeyEncryptionStrategy ske = new SecretKeyEncryptionStrategy();
37          ske.setAlgorithm("Blowfish");
38          ske.setKeyFactory(new SecretKeyFactory()
39          {
40              public byte[] getKey()
41              {
42                  return "shhhh".getBytes();
43              }
44          });
45          ske.initialise();
46  
47          byte[] b = ske.encrypt("hello".getBytes(), null);
48  
49          assertNotSame(new String(b), "hello");
50          String s = new String(ske.decrypt(b, null), "UTF-8");
51          assertEquals("hello", s);
52      }
53  
54      public void testRoundTripEncryptionTripleDES() throws Exception
55      {
56          SecretKeyEncryptionStrategy ske = new SecretKeyEncryptionStrategy();
57          ske.setAlgorithm("TripleDES");
58          ske.setKey("shhhhh");
59  
60          ske.initialise();
61  
62          byte[] b = ske.encrypt("hello".getBytes(), null);
63  
64          assertNotSame(new String(b), "hello");
65          String s = new String(ske.decrypt(b, null), "UTF-8");
66          assertEquals("hello", s);
67      }
68  
69  }