18 package org.turro.elephant.impl.security;
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;
42 public static final String
44 PAIR_KEY_FILE =
"/WEB-INF/elephant/security/keypair.cipher";
46 private static void initKeys() {
49 SecretKey sk = SymetricCipher.getSecretKey();
55 KeyPair kp = AsymetricCipher.getKeyPair();
57 }
catch (Exception ex) {
58 Logger.getLogger(
Cipher.class.getName()).log(Level.SEVERE,
null, ex);
65 return SymetricCipher.encrypt(inpBytes, (SecretKey)
71 return SymetricCipher.decrypt(inpBytes, (SecretKey)
78 return AsymetricCipher.encrypt(inpBytes, kp.getPrivate());
84 return AsymetricCipher.decrypt(inpBytes, kp.getPublic());
87 private static Serializable getFromFile(File file) {
88 Serializable obj =
null;
90 FileInputStream fis =
null;
91 ObjectInputStream ois =
null;
94 fis =
new FileInputStream(file);
95 ois =
new ObjectInputStream(fis);
97 obj = (Serializable) ois.readObject();
102 }
catch (ClassNotFoundException | IOException e) {
109 private static void saveToFile(Serializable obj, File file) {
112 FileOutputStream fos =
null;
113 ObjectOutputStream oos =
null;
116 fos =
new FileOutputStream(file);
117 oos =
new ObjectOutputStream(fos);
119 oos.writeObject(obj);
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);
static String getRealPath(String path)
static String logMsg(String msg)
static byte[] asymetricEncrypt(byte[] inpBytes)
static byte[] symetricEncrypt(byte[] inpBytes)
static byte[] symetricDecrypt(byte[] inpBytes)
static byte[] asymetricDecrypt(byte[] inpBytes)
static final String SECRET_KEY_FILE