View Javadoc
1   package org.jastacry.test;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.io.UnsupportedEncodingException;
6   import java.security.AlgorithmParameters;
7   import java.security.InvalidAlgorithmParameterException;
8   import java.security.InvalidKeyException;
9   import java.security.NoSuchAlgorithmException;
10  import java.security.spec.InvalidKeySpecException;
11  import java.security.spec.InvalidParameterSpecException;
12  
13  import javax.crypto.BadPaddingException;
14  import javax.crypto.Cipher;
15  import javax.crypto.IllegalBlockSizeException;
16  import javax.crypto.NoSuchPaddingException;
17  import javax.crypto.SecretKey;
18  import javax.crypto.spec.IvParameterSpec;
19  import javax.crypto.spec.SecretKeySpec;
20  
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Test of export encryption features.
25   *
26   * @author Kai Kretschmann
27   *
28   */
29  public class TestExportEncryption
30  {
31      /**
32       * Testdata to play with.
33       */
34      private final String testdata = "The quick brown fox jumps over the lazy dog.";
35  
36      /**
37       * Testcase get java version.
38       *
39       */
40      @Test
41      public void testJavaVersion()
42      {
43          String version = System.getProperty("java.version");
44          System.out.println("Version=" + version);
45      }
46  
47      /**
48       * Testcase get max key length.
49       * 
50       * @throws NoSuchAlgorithmException
51       *             in case of export restrictions
52       *
53       */
54      @Test
55      public void testMaxKeylength() throws NoSuchAlgorithmException
56      {
57          int maxlength = Cipher.getMaxAllowedKeyLength("AES/CBC/PKCS5Padding");
58          System.out.println("MaxAllowedKeyLength=" + maxlength);
59          assertEquals("Encryption key unrestricted", Integer.MAX_VALUE, maxlength);
60      }
61  
62      /**
63       * Testcase testAes128.
64       *
65       */
66      @Test
67      public void testAes128()
68      {
69          try
70          {
71              helperEncryptDecrypt(128);
72          }
73          catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
74                  | InvalidParameterSpecException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException
75                  | InvalidAlgorithmParameterException e)
76          {
77              org.junit.Assert.fail("exception " + e.getLocalizedMessage());
78          }
79      }
80  
81      /**
82       * Testcase testAes256.
83       *
84       */
85      @Test
86      public void testAes256()
87      {
88          try
89          {
90              helperEncryptDecrypt(256);
91          }
92          catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
93                  | InvalidParameterSpecException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException
94                  | InvalidAlgorithmParameterException e)
95          {
96              org.junit.Assert.fail("exception " + e.getLocalizedMessage());
97          }
98      }
99  
100     /**
101      * Helper function.
102      *
103      * @throws NoSuchAlgorithmException
104      *             on error
105      * @throws InvalidKeySpecException
106      *             on error
107      * @throws NoSuchPaddingException
108      *             on error
109      * @throws InvalidKeyException
110      *             on error
111      * @throws InvalidParameterSpecExceptio
112      *             on errorn
113      * @throws UnsupportedEncodingException
114      *             on error
115      * @throws BadPaddingException
116      *             on errorv
117      * @throws IllegalBlockSizeException
118      *             on error
119      * @throws InvalidAlgorithmParameterException
120      *             on error
121      */
122     private void helperEncryptDecrypt(final int bitsize) throws NoSuchAlgorithmException, InvalidKeySpecException,
123             NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException,
124             BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException
125     {
126         int bytesize = bitsize / 8;
127         System.out.println("key size " + bytesize);
128         byte[] aesKey = new byte[bytesize];
129         for (int i = 0; i < aesKey.length; ++i)
130         {
131             aesKey[i] = (byte) i;
132         }
133         SecretKey secret = new SecretKeySpec(aesKey, "AES");
134 
135         /* Encrypt the message. */
136         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
137         cipher.init(Cipher.ENCRYPT_MODE, secret);
138         AlgorithmParameters params = cipher.getParameters();
139         byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
140         byte[] ciphertext = cipher.doFinal(testdata.getBytes("UTF-8"));
141         System.out.println(ciphertext.length);
142 
143         /* Decrypt the message, given derived key and initialisation vector. */
144         cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
145         cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
146         String plaintext = new String(cipher.doFinal(ciphertext), "UTF-8");
147         System.out.println(plaintext);
148 
149         assertEquals("Message content equal", testdata, plaintext);
150     }
151 }