730493291abb40f220f27793e4d9d7b63e313363
[aaf/cadi.git] / core / src / main / java / com / att / cadi / 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 com.att.cadi;\r
24 \r
25 import java.io.File;\r
26 import java.io.FileInputStream;\r
27 import java.io.FileOutputStream;\r
28 import java.io.IOException;\r
29 import java.io.InputStream;\r
30 import java.io.OutputStream;\r
31 import java.security.InvalidKeyException;\r
32 import java.security.NoSuchAlgorithmException;\r
33 \r
34 import javax.crypto.BadPaddingException;\r
35 import javax.crypto.Cipher;\r
36 import javax.crypto.CipherInputStream;\r
37 import javax.crypto.CipherOutputStream;\r
38 import javax.crypto.IllegalBlockSizeException;\r
39 import javax.crypto.KeyGenerator;\r
40 import javax.crypto.NoSuchPaddingException;\r
41 import javax.crypto.SecretKey;\r
42 import javax.crypto.spec.SecretKeySpec;\r
43 \r
44 import com.att.cadi.util.Chmod;\r
45 \r
46 public class AES {\r
47         public static final String AES = AES.class.getSimpleName();\r
48         public static final int AES_KEY_SIZE = 128; // 256 isn't supported on all JDKs.\r
49         \r
50         private Cipher aesCipher;\r
51         private SecretKeySpec aeskeySpec;\r
52 \r
53         public AES() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {\r
54                 aesCipher = Cipher.getInstance(AES);\r
55             aeskeySpec = new SecretKeySpec(newKey().getEncoded(), AES);\r
56         }\r
57         \r
58         public static SecretKey newKey() throws NoSuchAlgorithmException {\r
59                 KeyGenerator kgen = KeyGenerator.getInstance(AES);\r
60             kgen.init(AES_KEY_SIZE);\r
61             return kgen.generateKey();\r
62         }\r
63 \r
64         public AES(File keyfile) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {\r
65                 aesCipher = Cipher.getInstance(AES);\r
66                 byte[] aesKey = new byte[AES_KEY_SIZE/8];\r
67                 FileInputStream fis = new FileInputStream(keyfile);\r
68                 try {\r
69                         fis.read(aesKey);\r
70                 } finally {\r
71                         fis.close();\r
72                 }\r
73                 aeskeySpec = new SecretKeySpec(aesKey,AES);\r
74         }\r
75 \r
76         public AES(byte[] aeskey, int offset, int len) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {\r
77                 aesCipher = Cipher.getInstance(AES);\r
78                 aeskeySpec = new SecretKeySpec(aeskey,offset,len,AES);\r
79         }\r
80         \r
81         public byte[] encrypt(byte[] in) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {\r
82                 aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);\r
83                 return aesCipher.doFinal(in);\r
84         }\r
85         \r
86         public byte[] decrypt(byte[] in) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {\r
87                 aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec); \r
88                 return aesCipher.doFinal(in);\r
89         }\r
90         \r
91         public void save(File keyfile) throws IOException {\r
92                 FileOutputStream fis = new FileOutputStream(keyfile);\r
93                 try {\r
94                         fis.write(aeskeySpec.getEncoded());\r
95                 } finally {\r
96                         fis.close();\r
97                 }\r
98                 Chmod.to400.chmod(keyfile);\r
99         }\r
100 \r
101         public CipherOutputStream outputStream(OutputStream os, boolean encrypt) {\r
102                 try {\r
103                         if(encrypt) {\r
104                                 aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);\r
105                         } else {\r
106                                 aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec);\r
107                         }\r
108                 } catch (InvalidKeyException e) {\r
109                         // KeySpec created earlier... no chance being wrong.\r
110                 } \r
111                 return new CipherOutputStream(os,aesCipher);\r
112         }\r
113         \r
114         public CipherInputStream inputStream(InputStream is, boolean encrypt) {\r
115                 try {\r
116                         if(encrypt) {\r
117                                 aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);\r
118                         } else {\r
119                                 aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec);\r
120                         }\r
121                 } catch (InvalidKeyException e) {\r
122                         // KeySpec created earlier... no chance being wrong.\r
123                 } \r
124                 \r
125                 return new CipherInputStream(is,aesCipher);\r
126         }\r
127 }\r