4779f09e21e45e8143039f7ad8c9cb9717f092ca
[aaf/cadi.git] / core / src / test / java / org / onap / aaf / cadi / JU_AES.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi;\r
24 \r
25 import java.io.ByteArrayInputStream;\r
26 import java.io.ByteArrayOutputStream;\r
27 \r
28 import javax.crypto.CipherInputStream;\r
29 import javax.crypto.CipherOutputStream;\r
30 \r
31 import org.junit.Test;\r
32 import org.onap.aaf.cadi.AES;\r
33 import org.onap.aaf.cadi.Symm;\r
34 \r
35 import junit.framework.Assert;\r
36 \r
37 public class JU_AES {\r
38 \r
39         @Test\r
40         public void test() throws Exception {\r
41                 AES aes = new AES();\r
42                 String orig = "I'm a password, really";\r
43                 byte[] passin = orig.getBytes();\r
44                 byte[] encrypted = aes.encrypt(passin);\r
45                 byte[] b64enc = Symm.base64.encode(encrypted);\r
46                 System.out.println(new String(b64enc));\r
47                 \r
48                 encrypted = Symm.base64.decode(b64enc);\r
49                 passin = aes.decrypt(encrypted);\r
50                 Assert.assertEquals(orig, new String(passin));\r
51         }\r
52 \r
53         @Test\r
54         public void testInputStream() throws Exception {\r
55                 AES aes = new AES();\r
56                 String orig = "I'm a password, really";\r
57                 ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
58                 CipherInputStream cis = aes.inputStream(bais, true);\r
59                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
60                 Symm.base64.encode(cis, baos);\r
61                 cis.close();\r
62 \r
63                 byte[] b64encrypted;\r
64                 System.out.println(new String(b64encrypted=baos.toByteArray()));\r
65                 \r
66                 \r
67                 baos.reset();\r
68                 CipherOutputStream cos = aes.outputStream(baos, false);\r
69                 Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
70                 cos.close();\r
71                 Assert.assertEquals(orig, new String(baos.toByteArray()));\r
72         }\r
73 \r
74         @Test\r
75         public void testObtain() throws Exception {\r
76                 byte[] keygen = Symm.baseCrypt().keygen();\r
77                 \r
78                 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
79                 \r
80                 String orig ="Another Password, please";\r
81                 String encrypted = symm.enpass(orig);\r
82                 System.out.println(encrypted);\r
83                 String decrypted = symm.depass(encrypted);\r
84                 System.out.println(decrypted);\r
85                 Assert.assertEquals(orig, decrypted);\r
86         }\r
87         \r
88         @Test\r
89         public void test1() throws Exception {\r
90                 AES aes = new AES();\r
91                 String orig = "I'm a password, really Cool";\r
92                 byte[] passin = orig.getBytes();\r
93                 byte[] encrypted = aes.encrypt(passin);\r
94                 byte[] b64enc = Symm.base64.encode(encrypted);\r
95                 System.out.println(new String(b64enc));\r
96                 \r
97                 encrypted = Symm.base64.decode(b64enc);\r
98                 passin = aes.decrypt(encrypted);\r
99                 Assert.assertEquals(orig, new String(passin));\r
100         }\r
101 \r
102         @Test\r
103         public void testInputStream1() throws Exception {\r
104                 AES aes = new AES();\r
105                 String orig = "I'm a password, really cool";\r
106                 ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
107                 CipherInputStream cis = aes.inputStream(bais, true);\r
108                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
109                 Symm.base64.encode(cis, baos);\r
110                 cis.close();\r
111 \r
112                 byte[] b64encrypted;\r
113                 System.out.println(new String(b64encrypted=baos.toByteArray()));\r
114                 \r
115                 \r
116                 baos.reset();\r
117                 CipherOutputStream cos = aes.outputStream(baos, false);\r
118                 Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
119                 cos.close();\r
120                 Assert.assertEquals(orig, new String(baos.toByteArray()));\r
121         }\r
122 \r
123         @Test\r
124         public void testObtain1() throws Exception {\r
125                 byte[] keygen = Symm.baseCrypt().keygen();\r
126                 \r
127                 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
128                 \r
129                 String orig ="Another Password, please cool";\r
130                 String encrypted = symm.enpass(orig);\r
131                 System.out.println(encrypted);\r
132                 String decrypted = symm.depass(encrypted);\r
133                 System.out.println(decrypted);\r
134                 Assert.assertEquals(orig, decrypted);\r
135         }\r
136 \r
137         \r
138         @Test\r
139         public void test2() throws Exception {\r
140                 AES aes = new AES();\r
141                 String orig = "I'm a password, really Nice";\r
142                 byte[] passin = orig.getBytes();\r
143                 byte[] encrypted = aes.encrypt(passin);\r
144                 byte[] b64enc = Symm.base64.encode(encrypted);\r
145                 System.out.println(new String(b64enc));\r
146                 \r
147                 encrypted = Symm.base64.decode(b64enc);\r
148                 passin = aes.decrypt(encrypted);\r
149                 Assert.assertEquals(orig, new String(passin));\r
150         }\r
151 \r
152         @Test\r
153         public void testInputStream2() throws Exception {\r
154                 AES aes = new AES();\r
155                 String orig = "I'm a password, really Nice";\r
156                 ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
157                 CipherInputStream cis = aes.inputStream(bais, true);\r
158                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
159                 Symm.base64.encode(cis, baos);\r
160                 cis.close();\r
161 \r
162                 byte[] b64encrypted;\r
163                 System.out.println(new String(b64encrypted=baos.toByteArray()));\r
164                 \r
165                 \r
166                 baos.reset();\r
167                 CipherOutputStream cos = aes.outputStream(baos, false);\r
168                 Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
169                 cos.close();\r
170                 Assert.assertEquals(orig, new String(baos.toByteArray()));\r
171         }\r
172 \r
173         @Test\r
174         public void testObtain2() throws Exception {\r
175                 byte[] keygen = Symm.baseCrypt().keygen();\r
176                 \r
177                 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
178                 \r
179                 String orig ="Another Password, please Nice";\r
180                 String encrypted = symm.enpass(orig);\r
181                 System.out.println(encrypted);\r
182                 String decrypted = symm.depass(encrypted);\r
183                 System.out.println(decrypted);\r
184                 Assert.assertEquals(orig, decrypted);\r
185         }\r
186 \r
187         \r
188         @Test\r
189         public void test3() throws Exception {\r
190                 AES aes = new AES();\r
191                 String orig = "I'm a password, magic";\r
192                 byte[] passin = orig.getBytes();\r
193                 byte[] encrypted = aes.encrypt(passin);\r
194                 byte[] b64enc = Symm.base64.encode(encrypted);\r
195                 System.out.println(new String(b64enc));\r
196                 \r
197                 encrypted = Symm.base64.decode(b64enc);\r
198                 passin = aes.decrypt(encrypted);\r
199                 Assert.assertEquals(orig, new String(passin));\r
200         }\r
201 \r
202         @Test\r
203         public void testInputStream3() throws Exception {\r
204                 AES aes = new AES();\r
205                 String orig = "I'm a password, magic";\r
206                 ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());\r
207                 CipherInputStream cis = aes.inputStream(bais, true);\r
208                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
209                 Symm.base64.encode(cis, baos);\r
210                 cis.close();\r
211 \r
212                 byte[] b64encrypted;\r
213                 System.out.println(new String(b64encrypted=baos.toByteArray()));\r
214                 \r
215                 \r
216                 baos.reset();\r
217                 CipherOutputStream cos = aes.outputStream(baos, false);\r
218                 Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);\r
219                 cos.close();\r
220                 Assert.assertEquals(orig, new String(baos.toByteArray()));\r
221         }\r
222 \r
223         @Test\r
224         public void testObtain3() throws Exception {\r
225                 byte[] keygen = Symm.baseCrypt().keygen();\r
226                 \r
227                 Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));\r
228                 \r
229                 String orig ="Another Password, magic";\r
230                 String encrypted = symm.enpass(orig);\r
231                 System.out.println(encrypted);\r
232                 String decrypted = symm.depass(encrypted);\r
233                 System.out.println(decrypted);\r
234                 Assert.assertEquals(orig, decrypted);\r
235         }\r
236 \r
237 }\r