BrightSide Workbench Full Report + Source Code
Cipher.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.security;
19 
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.io.Serializable;
28 import java.security.KeyPair;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import javax.crypto.SecretKey;
32 import org.turro.crypto.AsymetricCipher;
33 import org.turro.crypto.SymetricCipher;
34 import org.turro.elephant.context.ElephantContext;
35 
40 public class Cipher {
41 
42  public static final String
43  SECRET_KEY_FILE = "/WEB-INF/elephant/security/secret.cipher",
44  PAIR_KEY_FILE = "/WEB-INF/elephant/security/keypair.cipher";
45 
46  private static void initKeys() {
47  File key = new File(ElephantContext.getRealPath(SECRET_KEY_FILE));
48  if(!key.exists()) {
49  SecretKey sk = SymetricCipher.getSecretKey();
50  saveToFile(sk, key);
51  }
52  key = new File(ElephantContext.getRealPath(PAIR_KEY_FILE));
53  if(!key.exists()) {
54  try {
55  KeyPair kp = AsymetricCipher.getKeyPair();
56  saveToFile(kp, key);
57  } catch (Exception ex) {
58  Logger.getLogger(Cipher.class.getName()).log(Level.SEVERE, null, ex);
59  }
60  }
61  }
62 
63  public static byte[] symetricEncrypt(byte[] inpBytes) {
64  initKeys();
65  return SymetricCipher.encrypt(inpBytes, (SecretKey)
66  getFromFile(new File(ElephantContext.getRealPath(SECRET_KEY_FILE))));
67  }
68 
69  public static byte[] symetricDecrypt(byte[] inpBytes) {
70  initKeys();
71  return SymetricCipher.decrypt(inpBytes, (SecretKey)
72  getFromFile(new File(ElephantContext.getRealPath(SECRET_KEY_FILE))));
73  }
74 
75  public static byte[] asymetricEncrypt(byte[] inpBytes) {
76  initKeys();
77  KeyPair kp = (KeyPair) getFromFile(new File(ElephantContext.getRealPath(PAIR_KEY_FILE)));
78  return AsymetricCipher.encrypt(inpBytes, kp.getPrivate());
79  }
80 
81  public static byte[] asymetricDecrypt(byte[] inpBytes) throws Exception {
82  initKeys();
83  KeyPair kp = (KeyPair) getFromFile(new File(ElephantContext.getRealPath(PAIR_KEY_FILE)));
84  return AsymetricCipher.decrypt(inpBytes, kp.getPublic());
85  }
86 
87  private static Serializable getFromFile(File file) {
88  Serializable obj = null;
89 
90  FileInputStream fis = null;
91  ObjectInputStream ois = null;
92 
93  try {
94  fis = new FileInputStream(file);
95  ois = new ObjectInputStream(fis);
96 
97  obj = (Serializable) ois.readObject();
98 
99  fis.close();
100  ois.close();
101 
102  } catch (ClassNotFoundException | IOException e) {
103  Logger.getLogger(Cipher.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), e);
104  }
105 
106  return obj;
107  }
108 
109  private static void saveToFile(Serializable obj, File file) {
110  file.delete();
111 
112  FileOutputStream fos = null;
113  ObjectOutputStream oos = null;
114 
115  try {
116  fos = new FileOutputStream(file);
117  oos = new ObjectOutputStream(fos);
118 
119  oos.writeObject(obj);
120 
121  fos.close();
122  oos.close();
123 
124  } catch (FileNotFoundException e) {
125  Logger.getLogger(Cipher.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), e);
126  } catch (IOException e) {
127  Logger.getLogger(Cipher.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), e);
128  }
129  }
130 
131  private Cipher() {
132  }
133 
134 }
static byte[] asymetricEncrypt(byte[] inpBytes)
Definition: Cipher.java:75
static byte[] symetricEncrypt(byte[] inpBytes)
Definition: Cipher.java:63
static byte[] symetricDecrypt(byte[] inpBytes)
Definition: Cipher.java:69
static byte[] asymetricDecrypt(byte[] inpBytes)
Definition: Cipher.java:81
static final String SECRET_KEY_FILE
Definition: Cipher.java:43