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 com.att.cadi;
\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
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
44 import com.att.cadi.util.Chmod;
\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
50 private Cipher aesCipher;
\r
51 private SecretKeySpec aeskeySpec;
\r
53 public AES() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {
\r
54 aesCipher = Cipher.getInstance(AES);
\r
55 aeskeySpec = new SecretKeySpec(newKey().getEncoded(), AES);
\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
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
73 aeskeySpec = new SecretKeySpec(aesKey,AES);
\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
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
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
91 public void save(File keyfile) throws IOException {
\r
92 FileOutputStream fis = new FileOutputStream(keyfile);
\r
94 fis.write(aeskeySpec.getEncoded());
\r
98 Chmod.to400.chmod(keyfile);
\r
101 public CipherOutputStream outputStream(OutputStream os, boolean encrypt) {
\r
104 aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);
\r
106 aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec);
\r
108 } catch (InvalidKeyException e) {
\r
109 // KeySpec created earlier... no chance being wrong.
\r
111 return new CipherOutputStream(os,aesCipher);
\r
114 public CipherInputStream inputStream(InputStream is, boolean encrypt) {
\r
117 aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);
\r
119 aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec);
\r
121 } catch (InvalidKeyException e) {
\r
122 // KeySpec created earlier... no chance being wrong.
\r
125 return new CipherInputStream(is,aesCipher);
\r