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
25
26
27
28
29 public class TestExportEncryption
30 {
31
32
33
34 private final String testdata = "The quick brown fox jumps over the lazy dog.";
35
36
37
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
49
50
51
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
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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
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
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 }