1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi;
\r
25 import java.io.ByteArrayInputStream;
\r
26 import java.io.ByteArrayOutputStream;
\r
28 import javax.crypto.CipherInputStream;
\r
29 import javax.crypto.CipherOutputStream;
\r
31 import org.junit.Test;
\r
32 import org.onap.aaf.cadi.AES;
\r
33 import org.onap.aaf.cadi.Symm;
\r
35 import junit.framework.Assert;
\r
37 import static org.junit.Assert.assertFalse;
\r
38 import static org.junit.Assert.assertTrue;
\r
40 import java.io.ByteArrayInputStream;
\r
41 import java.io.ByteArrayOutputStream;
\r
42 import java.io.File;
\r
43 import java.io.OutputStream;
\r
45 import javax.crypto.CipherInputStream;
\r
46 import javax.crypto.CipherOutputStream;
\r
48 import org.junit.Test;
\r
49 import org.onap.aaf.cadi.AES;
\r
50 import org.onap.aaf.cadi.Symm;
\r
52 import junit.framework.Assert;
\r
54 public class JU_AES {
\r
57 public void test() throws Exception {
\r
58 AES aes = new AES();
\r
59 String orig = "I'm a password, really";
\r
60 byte[] passin = orig.getBytes();
\r
61 byte[] encrypted = aes.encrypt(passin);
\r
62 byte[] b64enc = Symm.base64.encode(encrypted);
\r
63 System.out.println(new String(b64enc));
\r
65 encrypted = Symm.base64.decode(b64enc);
\r
66 passin = aes.decrypt(encrypted);
\r
67 Assert.assertEquals(orig, new String(passin));
\r
71 public void testAESFileStream() throws Exception {
\r
72 File fi = new File("test/AESKeyFile");
\r
73 AES aes = new AES(fi);
\r
75 String orig = "I'm a password, really";
\r
76 byte[] passin = orig.getBytes();
\r
77 byte[] encrypted = aes.encrypt(passin);
\r
78 byte[] b64enc = Symm.base64.encode(encrypted);
\r
79 System.out.println(new String(b64enc));
\r
81 encrypted = Symm.base64.decode(b64enc);
\r
82 passin = aes.decrypt(encrypted);
\r
83 Assert.assertEquals(orig, new String(passin));
\r
85 File temp = new File("tempFile");
\r
87 assertFalse(temp.exists());
\r
89 assertTrue(temp.exists());
\r
90 } catch (Exception e) {
\r
98 public void testInputStream() throws Exception {
\r
99 AES aes = new AES();
\r
100 String orig = "I'm a password, really";
\r
101 ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());
\r
102 CipherInputStream cis = aes.inputStream(bais, true);
\r
103 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
104 Symm.base64.encode(cis, baos);
\r
107 byte[] b64encrypted;
\r
108 System.out.println(new String(b64encrypted=baos.toByteArray()));
\r
110 CipherInputStream cis1 = aes.inputStream(bais, false);
\r
111 ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
\r
112 Symm.base64.encode(cis1, baos1);
\r
115 byte[] b64encrypted1;
\r
116 System.out.println(new String(b64encrypted1=baos1.toByteArray()));
\r
119 CipherOutputStream cos = aes.outputStream(baos, false);
\r
120 Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);
\r
122 Assert.assertEquals(orig, new String(baos.toByteArray()));
\r
124 OutputStream stream = aes.outputStream(System.out, true);
\r
125 assertTrue(stream instanceof CipherOutputStream);
\r
130 public void testObtain() throws Exception {
\r
131 byte[] keygen = Symm.baseCrypt().keygen();
\r
133 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));
\r
135 String orig ="Another Password, please";
\r
136 String encrypted = symm.enpass(orig);
\r
137 System.out.println(encrypted);
\r
138 String decrypted = symm.depass(encrypted);
\r
139 System.out.println(decrypted);
\r
140 Assert.assertEquals(orig, decrypted);
\r
144 public void test1() throws Exception {
\r
145 AES aes = new AES();
\r
146 String orig = "I'm a password, really";
\r
147 byte[] passin = orig.getBytes();
\r
148 byte[] encrypted = aes.encrypt(passin);
\r
149 byte[] b64enc = Symm.base64.encode(encrypted);
\r
150 System.out.println(new String(b64enc));
\r
152 encrypted = Symm.base64.decode(b64enc);
\r
153 passin = aes.decrypt(encrypted);
\r
154 Assert.assertEquals(orig, new String(passin));
\r
158 public void testAESFileStream1() throws Exception {
\r
159 File fi = new File("test/AESKeyFile");
\r
160 AES aes = new AES(fi);
\r
162 String orig = "I'm a password, really";
\r
163 byte[] passin = orig.getBytes();
\r
164 byte[] encrypted = aes.encrypt(passin);
\r
165 byte[] b64enc = Symm.base64.encode(encrypted);
\r
166 System.out.println(new String(b64enc));
\r
168 encrypted = Symm.base64.decode(b64enc);
\r
169 passin = aes.decrypt(encrypted);
\r
170 Assert.assertEquals(orig, new String(passin));
\r
172 File temp = new File("tempFile");
\r
174 assertFalse(temp.exists());
\r
176 assertTrue(temp.exists());
\r
177 } catch (Exception e) {
\r
185 public void testInputStream1() throws Exception {
\r
186 AES aes = new AES();
\r
187 String orig = "I'm a password, really";
\r
188 ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());
\r
189 CipherInputStream cis = aes.inputStream(bais, true);
\r
190 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
191 Symm.base64.encode(cis, baos);
\r
194 byte[] b64encrypted;
\r
195 System.out.println(new String(b64encrypted=baos.toByteArray()));
\r
197 CipherInputStream cis1 = aes.inputStream(bais, false);
\r
198 ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
\r
199 Symm.base64.encode(cis1, baos1);
\r
202 byte[] b64encrypted1;
\r
203 System.out.println(new String(b64encrypted1=baos1.toByteArray()));
\r
206 CipherOutputStream cos = aes.outputStream(baos, false);
\r
207 Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);
\r
209 Assert.assertEquals(orig, new String(baos.toByteArray()));
\r
211 OutputStream stream = aes.outputStream(System.out, true);
\r
212 assertTrue(stream instanceof CipherOutputStream);
\r
217 public void testObtain1() throws Exception {
\r
218 byte[] keygen = Symm.baseCrypt().keygen();
\r
220 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));
\r
222 String orig ="Another Password, please";
\r
223 String encrypted = symm.enpass(orig);
\r
224 System.out.println(encrypted);
\r
225 String decrypted = symm.depass(encrypted);
\r
226 System.out.println(decrypted);
\r
227 Assert.assertEquals(orig, decrypted);
\r