Roberto Sánchez
2014-02-24 ef282bd185482f2cab9648a8293aa74db85d787f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package net.curisit.securis.utils;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import net.curisit.securis.SeCurisException;
import org.apache.commons.net.util.Base64;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
//import net.curisit.common.ui.Dialogs;
public class CryptoHelper {
   private static final Logger log = LogManager.getLogger(SignatureHelper.class);
   private static final String KEY_FACTORY = "PBEWITHHMACSHA1";
   private static final String PPROVIDER = "BC";
   private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
   private static CryptoHelper singleton = new CryptoHelper();
   private String passPhrase = null;
   private CryptoHelper() {
   }
   public static CryptoHelper getInstance() {
       return singleton;
   }
   /**
    * Encrypts a given text with AES algorithm
    * 
    * @param plainText
    * @return The encrypted text in Base64
    */
   public String encrypt(String plainText) throws SeCurisException {
       return encrypt(plainText, this.passPhrase);
   }
   public String encrypt(String plainText, String pass) throws SeCurisException {
       Cipher aes;
       try {
           aes = Cipher.getInstance(CIPHER_ALGORITHM);
           byte[] salt = getSalt();
           aes.init(Cipher.ENCRYPT_MODE, getSecretKey(salt, pass));
           byte[] ciphertext = aes.doFinal(plainText.getBytes("utf-8"));
           return Base64.encodeBase64String(salt) + "\n" + Base64.encodeBase64String(ciphertext);
       } catch (NoSuchAlgorithmException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       } catch (NoSuchPaddingException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       } catch (InvalidKeyException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       } catch (IllegalBlockSizeException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       } catch (BadPaddingException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       } catch (UnsupportedEncodingException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       }
   }
   /**
    * Encrypts a given text with AES algorithm
    * 
    * @param plainText
    *            in Base64
    * @return
    */
   public String decript(String ciphertext) throws SeCurisException {
       return decript(ciphertext, this.passPhrase);
   }
   public String decript(String ciphertext, String pass) throws SeCurisException {
       Cipher aes;
       try {
           aes = Cipher.getInstance(CIPHER_ALGORITHM);
           int sep = ciphertext.indexOf('\n');
           if (sep == -1)
               throw new SeCurisException("Unknown format ciphered text");
           byte[] salt = Base64.decodeBase64(ciphertext.substring(0, sep));
           aes.init(Cipher.DECRYPT_MODE, getSecretKey(salt, pass));
           byte[] encryptedBytes = Base64.decodeBase64(ciphertext.substring(sep + 1));
           String cleartext = new String(aes.doFinal(encryptedBytes));
           return cleartext;
       } catch (NoSuchAlgorithmException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       } catch (NoSuchPaddingException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       } catch (InvalidKeyException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       } catch (IllegalBlockSizeException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       } catch (BadPaddingException e) {
           log.error("Error decrypting text", e);
           throw new SeCurisException("Error decrypting text", e);
       }
   }
   private byte[] getSalt() throws SeCurisException {
       byte[] salt = new byte[20];
       new SecureRandom().nextBytes(salt);
       return salt;
   }
   private SecretKeySpec getSecretKey(byte[] salt, String pass) throws SeCurisException {
       String passPhrase = pass.replace('a', 'ä');
       try {
           int iterations = 10000;
           SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_FACTORY, PPROVIDER);
           SecretKey tmp = factory.generateSecret(new PBEKeySpec(passPhrase.toCharArray(), salt, iterations, 128));
           byte[] key = Arrays.copyOf(tmp.getEncoded(), 16);
           SecretKeySpec spec = new SecretKeySpec(key, "AES");
           return spec;
       } catch (NoSuchAlgorithmException e) {
           log.error("Error generation secret key", e);
           throw new SeCurisException("Error generation secret key", e);
       } catch (InvalidKeySpecException e) {
           log.error("Error generation secret key", e);
           throw new SeCurisException("Error generation secret key", e);
       } catch (NoSuchProviderException e) {
           log.error("Error generation secret key", e);
           throw new SeCurisException("Error generation secret key", e);
       }
   }
}