* *
* *
******************************************************************************/
+
package org.onap.aaf.cadi.test;
import static org.hamcrest.CoreMatchers.*;
import org.onap.aaf.cadi.Symm;
public class JU_AES {
- private AES aes;
- private ByteArrayInputStream baisEncrypt;
- private ByteArrayInputStream baisDecrypt;
- private ByteArrayOutputStream baosEncrypt;
- private ByteArrayOutputStream baosDecrypt;
-
- private ByteArrayOutputStream errStream;
-
- @Before
- public void setup() throws Exception {
- byte[] keyBytes = new byte[AES.AES_KEY_SIZE/8];
- char[] codeset = Symm.base64.codeset;
- int offset = (Math.abs(codeset[0]) + 47) % (codeset.length - keyBytes.length);
- for(int i = 0; i < keyBytes.length; ++i) {
- keyBytes[i] = (byte)codeset[i+offset];
- }
- aes = new AES(keyBytes, 0, keyBytes.length);
-
- errStream = new ByteArrayOutputStream();
- System.setErr(new PrintStream(errStream));
- }
-
- @After
- public void tearDown() {
- System.setErr(System.err);
- }
-
- @Test
- public void newKeyTest() throws Exception {
- SecretKey secretKey = AES.newKey();
- assertThat(secretKey.getAlgorithm(), is(AES.class.getSimpleName()));
- }
-
- @Test
- public void encryptDecrpytFromBytes() throws Exception {
- String orig = "I'm a password, really";
- byte[] encrypted = aes.encrypt(orig.getBytes());
- byte[] decrypted = aes.decrypt(encrypted);
- assertThat(new String(decrypted), is(orig));
+ private AES aes;
+ private ByteArrayInputStream baisEncrypt;
+ private ByteArrayInputStream baisDecrypt;
+ private ByteArrayOutputStream baosEncrypt;
+ private ByteArrayOutputStream baosDecrypt;
+
+ private ByteArrayOutputStream errStream;
+
+ @Before
+ public void setup() throws Exception {
+ byte[] keyBytes = new byte[AES.AES_KEY_SIZE/8];
+ char[] codeset = Symm.base64.codeset;
+ int offset = (Math.abs(codeset[0]) + 47) % (codeset.length - keyBytes.length);
+ for (int i = 0; i < keyBytes.length; ++i) {
+ keyBytes[i] = (byte)codeset[i+offset];
+ }
+ aes = new AES(keyBytes, 0, keyBytes.length);
+
+ errStream = new ByteArrayOutputStream();
+ System.setErr(new PrintStream(errStream));
+ }
+
+ @After
+ public void tearDown() {
+ System.setErr(System.err);
+ }
+
+ @Test
+ public void newKeyTest() throws Exception {
+ SecretKey secretKey = AES.newKey();
+ assertThat(secretKey.getAlgorithm(), is(AES.class.getSimpleName()));
+ }
+
+ @Test
+ public void encryptDecrpytFromBytes() throws Exception {
+ String orig = "I'm a password, really";
+ byte[] encrypted = aes.encrypt(orig.getBytes());
+ byte[] decrypted = aes.decrypt(encrypted);
+ assertThat(new String(decrypted), is(orig));
- Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
- aeskeySpec_field.setAccessible(true);
- aeskeySpec_field.set(aes, null);
-
- try {
- aes.encrypt(orig.getBytes());
- fail("Should have thrown an exception");
- } catch (CadiException e) {
- }
- try {
- aes.decrypt(encrypted);
- fail("Should have thrown an exception");
- } catch (CadiException e) {
- }
- }
-
- @Test
- public void saveToFileTest() throws Exception {
- String filePath = "test/output_key";
- File keyfile = new File(filePath);
- aes.save(keyfile);
- assertTrue(Files.isReadable(Paths.get(filePath)));
- assertFalse(Files.isWritable(Paths.get(filePath)));
- assertFalse(Files.isExecutable(Paths.get(filePath)));
- keyfile.delete();
- }
-
- @Test
- public void encryptDecryptFromInputStream() throws Exception {
- String orig = "I'm a password, really";
- byte[] b64encrypted;
- String output;
-
- CipherInputStream cisEncrypt;
- CipherInputStream cisDecrypt;
-
- // Test CipherInputStream
- baisEncrypt = new ByteArrayInputStream(orig.getBytes());
- cisEncrypt = aes.inputStream(baisEncrypt, true);
- baosEncrypt = new ByteArrayOutputStream();
- transferFromInputStreamToOutputStream(cisEncrypt, baosEncrypt);
- cisEncrypt.close();
-
- b64encrypted = baosEncrypt.toByteArray();
-
- baisDecrypt = new ByteArrayInputStream(b64encrypted);
- cisDecrypt = aes.inputStream(baisDecrypt, false);
- baosDecrypt = new ByteArrayOutputStream();
- transferFromInputStreamToOutputStream(cisDecrypt, baosDecrypt);
- cisDecrypt.close();
-
- output = new String(baosDecrypt.toByteArray());
- assertThat(output, is(orig));
-
- Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
- aeskeySpec_field.setAccessible(true);
- aeskeySpec_field.set(aes, null);
-
- assertNull(aes.inputStream(baisEncrypt, true));
- assertThat(errStream.toString(), is("Error creating Aes CipherInputStream\n"));
- }
-
- @Test
- public void encryptDecryptFromOutputStream() throws Exception {
- String orig = "I'm a password, really";
- byte[] b64encrypted;
- String output;
-
- CipherOutputStream cosEncrypt;
- CipherOutputStream cosDecrypt;
-
- // Test CipherOutputStream
- baisEncrypt = new ByteArrayInputStream(orig.getBytes());
- baosEncrypt = new ByteArrayOutputStream();
- cosEncrypt = aes.outputStream(baosEncrypt, true);
- transferFromInputStreamToOutputStream(baisEncrypt, cosEncrypt);
- cosEncrypt.close();
-
- b64encrypted = baosEncrypt.toByteArray();
-
- baosDecrypt = new ByteArrayOutputStream();
- cosDecrypt = aes.outputStream(baosDecrypt, false);
- baisDecrypt = new ByteArrayInputStream(b64encrypted);
- transferFromInputStreamToOutputStream(baisDecrypt, cosDecrypt);
- cosDecrypt.close();
-
- output = new String(baosDecrypt.toByteArray());
- assertThat(output, is(orig));
-
- Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
- aeskeySpec_field.setAccessible(true);
- aeskeySpec_field.set(aes, null);
-
- assertNull(aes.outputStream(baosEncrypt, true));
- assertThat(errStream.toString(), is("Error creating Aes CipherOutputStream\n"));
- }
-
- public void transferFromInputStreamToOutputStream(InputStream is, OutputStream os) throws IOException {
- byte[] buffer = new byte[200];
- int len;
- while ((len = is.read(buffer)) != -1) {
- os.write(buffer, 0, len);
- }
- }
-
+ Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
+ aeskeySpec_field.setAccessible(true);
+ aeskeySpec_field.set(aes, null);
+
+ try {
+ aes.encrypt(orig.getBytes());
+ fail("Should have thrown an exception");
+ } catch (CadiException e) {
+ }
+ try {
+ aes.decrypt(encrypted);
+ fail("Should have thrown an exception");
+ } catch (CadiException e) {
+ }
+ }
+
+ @Test
+ public void saveToFileTest() throws Exception {
+ String filePath = "src/test/resources/output_key";
+ File keyfile = new File(filePath);
+ aes.save(keyfile);
+ assertTrue(Files.isReadable(Paths.get(filePath)));
+ assertFalse(Files.isWritable(Paths.get(filePath)));
+ assertFalse(Files.isExecutable(Paths.get(filePath)));
+ keyfile.delete();
+ }
+
+ @Test
+ public void encryptDecryptFromInputStream() throws Exception {
+ String orig = "I'm a password, really";
+ byte[] b64encrypted;
+ String output;
+
+ CipherInputStream cisEncrypt;
+ CipherInputStream cisDecrypt;
+
+ // Test CipherInputStream
+ baisEncrypt = new ByteArrayInputStream(orig.getBytes());
+ cisEncrypt = aes.inputStream(baisEncrypt, true);
+ baosEncrypt = new ByteArrayOutputStream();
+ transferFromInputStreamToOutputStream(cisEncrypt, baosEncrypt);
+ cisEncrypt.close();
+
+ b64encrypted = baosEncrypt.toByteArray();
+
+ baisDecrypt = new ByteArrayInputStream(b64encrypted);
+ cisDecrypt = aes.inputStream(baisDecrypt, false);
+ baosDecrypt = new ByteArrayOutputStream();
+ transferFromInputStreamToOutputStream(cisDecrypt, baosDecrypt);
+ cisDecrypt.close();
+
+ output = new String(baosDecrypt.toByteArray());
+ assertThat(output, is(orig));
+
+ Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
+ aeskeySpec_field.setAccessible(true);
+ aeskeySpec_field.set(aes, null);
+
+ assertNull(aes.inputStream(baisEncrypt, true));
+ assertThat(errStream.toString(), is("Error creating Aes CipherInputStream\n"));
+ }
+
+ @Test
+ public void encryptDecryptFromOutputStream() throws Exception {
+ String orig = "I'm a password, really";
+ byte[] b64encrypted;
+ String output;
+
+ CipherOutputStream cosEncrypt;
+ CipherOutputStream cosDecrypt;
+
+ // Test CipherOutputStream
+ baisEncrypt = new ByteArrayInputStream(orig.getBytes());
+ baosEncrypt = new ByteArrayOutputStream();
+ cosEncrypt = aes.outputStream(baosEncrypt, true);
+ transferFromInputStreamToOutputStream(baisEncrypt, cosEncrypt);
+ cosEncrypt.close();
+
+ b64encrypted = baosEncrypt.toByteArray();
+
+ baosDecrypt = new ByteArrayOutputStream();
+ cosDecrypt = aes.outputStream(baosDecrypt, false);
+ baisDecrypt = new ByteArrayInputStream(b64encrypted);
+ transferFromInputStreamToOutputStream(baisDecrypt, cosDecrypt);
+ cosDecrypt.close();
+
+ output = new String(baosDecrypt.toByteArray());
+ assertThat(output, is(orig));
+
+ Field aeskeySpec_field = AES.class.getDeclaredField("aeskeySpec");
+ aeskeySpec_field.setAccessible(true);
+ aeskeySpec_field.set(aes, null);
+
+ assertNull(aes.outputStream(baosEncrypt, true));
+ assertThat(errStream.toString(), is("Error creating Aes CipherOutputStream\n"));
+ }
+
+ public void transferFromInputStreamToOutputStream(InputStream is, OutputStream os) throws IOException {
+ byte[] buffer = new byte[200];
+ int len;
+ while ((len = is.read(buffer)) != -1) {
+ os.write(buffer, 0, len);
+ }
+ }
+
}