+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileOutputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.OutputStream;\r
-import java.security.InvalidKeyException;\r
-import java.security.NoSuchAlgorithmException;\r
-\r
-import javax.crypto.BadPaddingException;\r
-import javax.crypto.Cipher;\r
-import javax.crypto.CipherInputStream;\r
-import javax.crypto.CipherOutputStream;\r
-import javax.crypto.IllegalBlockSizeException;\r
-import javax.crypto.KeyGenerator;\r
-import javax.crypto.NoSuchPaddingException;\r
-import javax.crypto.SecretKey;\r
-import javax.crypto.spec.SecretKeySpec;\r
-\r
-import org.onap.aaf.cadi.util.Chmod;\r
-\r
-public class AES {\r
- public static final String AES = AES.class.getSimpleName();\r
- public static final int AES_KEY_SIZE = 128; // 256 isn't supported on all JDKs.\r
- \r
- private Cipher aesCipher;\r
- private SecretKeySpec aeskeySpec;\r
-\r
- public AES() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {\r
- aesCipher = Cipher.getInstance(AES);\r
- aeskeySpec = new SecretKeySpec(newKey().getEncoded(), AES);\r
- }\r
- \r
- public static SecretKey newKey() throws NoSuchAlgorithmException {\r
- KeyGenerator kgen = KeyGenerator.getInstance(AES);\r
- kgen.init(AES_KEY_SIZE);\r
- return kgen.generateKey();\r
- }\r
-\r
- public AES(File keyfile) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {\r
- aesCipher = Cipher.getInstance(AES);\r
- byte[] aesKey = new byte[AES_KEY_SIZE/8];\r
- FileInputStream fis = new FileInputStream(keyfile);\r
- try {\r
- fis.read(aesKey);\r
- } finally {\r
- fis.close();\r
- }\r
- aeskeySpec = new SecretKeySpec(aesKey,AES);\r
- }\r
-\r
- public AES(byte[] aeskey, int offset, int len) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException {\r
- aesCipher = Cipher.getInstance(AES);\r
- aeskeySpec = new SecretKeySpec(aeskey,offset,len,AES);\r
- }\r
- \r
- public byte[] encrypt(byte[] in) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {\r
- aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);\r
- return aesCipher.doFinal(in);\r
- }\r
- \r
- public byte[] decrypt(byte[] in) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {\r
- aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec); \r
- return aesCipher.doFinal(in);\r
- }\r
- \r
- public void save(File keyfile) throws IOException {\r
- FileOutputStream fis = new FileOutputStream(keyfile);\r
- try {\r
- fis.write(aeskeySpec.getEncoded());\r
- } finally {\r
- fis.close();\r
- }\r
- Chmod.to400.chmod(keyfile);\r
- }\r
-\r
- public CipherOutputStream outputStream(OutputStream os, boolean encrypt) {\r
- try {\r
- if(encrypt) {\r
- aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);\r
- } else {\r
- aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec);\r
- }\r
- } catch (InvalidKeyException e) {\r
- // KeySpec created earlier... no chance being wrong.\r
- } \r
- return new CipherOutputStream(os,aesCipher);\r
- }\r
- \r
- public CipherInputStream inputStream(InputStream is, boolean encrypt) {\r
- try {\r
- if(encrypt) {\r
- aesCipher.init(Cipher.ENCRYPT_MODE,aeskeySpec);\r
- } else {\r
- aesCipher.init(Cipher.DECRYPT_MODE,aeskeySpec);\r
- }\r
- } catch (InvalidKeyException e) {\r
- // KeySpec created earlier... no chance being wrong.\r
- } \r
- \r
- return new CipherInputStream(is,aesCipher);\r
- }\r
-}\r