[AAF-21] Initial code import
[aaf/cadi.git] / core / src / main / java / com / att / cadi / AES.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi;\r
25 \r
26 import java.io.File;\r
27 import java.io.FileInputStream;\r
28 import java.io.FileOutputStream;\r
29 import java.io.IOException;\r
30 import java.io.InputStream;\r
31 import java.io.OutputStream;\r
32 import java.security.InvalidKeyException;\r
33 import java.security.NoSuchAlgorithmException;\r
34 \r
35 import javax.crypto.BadPaddingException;\r
36 import javax.crypto.Cipher;\r
37 import javax.crypto.CipherInputStream;\r
38 import javax.crypto.CipherOutputStream;\r
39 import javax.crypto.IllegalBlockSizeException;\r
40 import javax.crypto.KeyGenerator;\r
41 import javax.crypto.NoSuchPaddingException;\r
42 import javax.crypto.SecretKey;\r
43 import javax.crypto.spec.SecretKeySpec;\r
44 \r
45 import com.att.cadi.util.Chmod;\r
46 \r
47 public class AES {\r
48         public static final String AES = AES.class.getSimpleName();\r
49         public static final int AES_KEY_SIZE = 128; // 256 isn't supported on all JDKs.\r
50         \r
51         private Cipher aesCipher;\r
52         private SecretKeySpec aeskeySpec;\r
53 \r
54         public AES() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {\r
55                 aesCipher = Cipher.getInstance(AES);\r
56             aeskeySpec = new SecretKeySpec(newKey().getEncoded(), AES);\r
57         }\r
58         \r
59         public static SecretKey newKey() throws NoSuchAlgorithmException {\r
60                 KeyGenerator kgen = KeyGenerator.getInstance(AES);\r
61             kgen.init(AES_KEY_SIZE);\r
62             return kgen.generateKey();\r
63         }\r
64 \r
65         public AES(File keyfile) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {\r
66                 aesCipher = Cipher.getInstance(AES);\r
67                 byte[] aesKey = new byte[AES_KEY_SIZE/8];\r
68                 FileInputStream fis = new FileInputStream(keyfile);\r
69                 try {\r
70                         fis.read(aesKey);\r
71                 } finally {\r
72                         fis.close();\r
73                 }\r
74                 aeskeySpec = new SecretKeySpec(aesKey,AES);\r
75         }\r
76 \r
77         public AES(byte[] aeskey, int offset, int len) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {\r
78                 aesCipher = Cipher.getInstance(AES);\r
79                 aeskeySpec = new SecretKeySpec(aeskey,offset,len,AES);\r
80         }\r
81         \r
82         public byte[] encrypt(byte[] in) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {\r
83                 aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);\r
84                 return aesCipher.doFinal(in);\r
85         }\r
86         \r
87         public byte[] decrypt(byte[] in) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {\r
88                 aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec); \r
89                 return aesCipher.doFinal(in);\r
90         }\r
91         \r
92         public void save(File keyfile) throws IOException {\r
93                 FileOutputStream fis = new FileOutputStream(keyfile);\r
94                 try {\r
95                         fis.write(aeskeySpec.getEncoded());\r
96                 } finally {\r
97                         fis.close();\r
98                 }\r
99                 Chmod.to400.chmod(keyfile);\r
100         }\r
101 \r
102         public CipherOutputStream outputStream(OutputStream os, boolean encrypt) {\r
103                 try {\r
104                         if(encrypt) {\r
105                                 aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);\r
106                         } else {\r
107                                 aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec);\r
108                         }\r
109                 } catch (InvalidKeyException e) {\r
110                         // KeySpec created earlier... no chance being wrong.\r
111                 } \r
112                 return new CipherOutputStream(os,aesCipher);\r
113         }\r
114         \r
115         public CipherInputStream inputStream(InputStream is, boolean encrypt) {\r
116                 try {\r
117                         if(encrypt) {\r
118                                 aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);\r
119                         } else {\r
120                                 aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec);\r
121                         }\r
122                 } catch (InvalidKeyException e) {\r
123                         // KeySpec created earlier... no chance being wrong.\r
124                 } \r
125                 \r
126                 return new CipherInputStream(is,aesCipher);\r
127         }\r
128 }\r