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 package fr.paris.lutece.util.keydiversification;
35
36 import java.io.UnsupportedEncodingException;
37 import java.security.InvalidKeyException;
38 import java.security.NoSuchAlgorithmException;
39 import java.security.spec.InvalidKeySpecException;
40 import java.security.spec.KeySpec;
41 import java.util.Base64;
42 import javax.crypto.BadPaddingException;
43 import javax.crypto.Cipher;
44 import javax.crypto.IllegalBlockSizeException;
45 import javax.crypto.NoSuchPaddingException;
46 import javax.crypto.SecretKey;
47 import javax.crypto.SecretKeyFactory;
48 import javax.crypto.spec.DESedeKeySpec;
49
50
51
52
53 public class CryptoService
54 {
55
56 private static final String UNICODE_FORMAT = "UTF8";
57 private static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
58
59 private final Cipher _cipher;
60 private final SecretKey _key;
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public CryptoService( String strEncryptionKey ) throws KeyDiversificationException
75 {
76 try
77 {
78 byte [ ] arrayBytes = new byte [ DESedeKeySpec.DES_EDE_KEY_LEN];
79
80 for ( int i = 0; i < DESedeKeySpec.DES_EDE_KEY_LEN; i++ )
81 {
82 if ( i < strEncryptionKey.length( ) )
83 {
84 arrayBytes [i] = (byte) strEncryptionKey.charAt( i );
85 }
86 }
87 KeySpec keySpec = new DESedeKeySpec( arrayBytes );
88 SecretKeyFactory skf = SecretKeyFactory.getInstance( DESEDE_ENCRYPTION_SCHEME );
89 _cipher = Cipher.getInstance( DESEDE_ENCRYPTION_SCHEME );
90 _key = skf.generateSecret( keySpec );
91 }
92 catch( InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException e )
93 {
94 throw new KeyDiversificationException( "Error during the initialisation of encryption!", e );
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107 public String encrypt( String strSource ) throws KeyDiversificationException
108 {
109 try
110 {
111 String strEncrypted;
112 _cipher.init( Cipher.ENCRYPT_MODE, _key );
113 byte [ ] plainText = strSource.getBytes( UNICODE_FORMAT );
114 byte [ ] encryptedText = _cipher.doFinal( plainText );
115 strEncrypted = new String( Base64.getEncoder( ).encode( encryptedText ), UNICODE_FORMAT );
116 return strEncrypted;
117 }
118 catch( InvalidKeyException | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e )
119 {
120 throw new KeyDiversificationException( "Error during the encryption of '" + strSource + "'", e );
121 }
122 }
123
124
125
126
127
128
129
130
131
132
133 public String decrypt( String strEncrypted ) throws KeyDiversificationException
134 {
135 try
136 {
137 String strDecrypted;
138 _cipher.init( Cipher.DECRYPT_MODE, _key );
139 byte [ ] encryptedText = Base64.getDecoder( ).decode( strEncrypted );
140 byte [ ] plainText = _cipher.doFinal( encryptedText );
141 strDecrypted = new String( plainText, UNICODE_FORMAT );
142 return strDecrypted;
143 }
144 catch( IllegalBlockSizeException | BadPaddingException | InvalidKeyException | UnsupportedEncodingException e )
145 {
146 throw new KeyDiversificationException( "Error during the decryption of '" + strEncrypted + "'", e );
147 }
148 }
149
150 }