1+ package com .genexus .cryptography ;
2+
3+ import java .security .NoSuchAlgorithmException ;
4+
5+ import javax .crypto .NoSuchPaddingException ;
6+
7+ import com .genexus .cryptography .encryption .symmetric .CipherSymProvider ;
8+ import com .genexus .cryptography .encryption .symmetric .IGXSymEncryption ;
9+ import com .genexus .cryptography .exception .AlgorithmNotSupportedException ;
10+ import com .genexus .cryptography .exception .EncryptionException ;
11+ import com .genexus .cryptography .exception .InvalidKeyLengthException ;
12+
13+ public class GXSymEncryption {
14+
15+ private int _lastError ;
16+ private String _lastErrorDescription ;
17+ private IGXSymEncryption _symAlg ; // Algorithm instance
18+ private String _algorithm ;
19+ private String _key = "" ; // key
20+ private String _iv = "" ; // initialization vector
21+ private boolean isDirty ;
22+ private int _keySize ;
23+ private int _blockSize ;
24+
25+ public GXSymEncryption () {
26+ isDirty = true ;
27+ _algorithm = String .format ("%s/%s/%s" , Constants .DEFAULT_SYM_ALGORITHM , Constants .DEFAULT_SYM_MODE ,
28+ Constants .DEFAULT_SYM_PADDING );
29+ }
30+
31+ private void Initialize () {
32+ if (isDirty ) {
33+ // Supported algorithms = {Rijndael, DES, RC2, TripleDES}
34+ setError (0 );
35+
36+ try {
37+ _symAlg = new CipherSymProvider (_algorithm );
38+ if (validPropertyValue (_key )) {
39+ _symAlg .setKey (_key );
40+ }
41+ if (validPropertyValue (_iv )) {
42+ _symAlg .setIV (_iv );
43+ }
44+ if (_blockSize > 0 ) {
45+ _symAlg .setBlockSize (_blockSize );
46+ }
47+ if (_keySize > 0 ) {
48+ _symAlg .setKeySize (_keySize );
49+ }
50+ isDirty = false ;
51+ } catch (NoSuchAlgorithmException e ) {
52+ setError (2 );
53+ Utils .logError (e );
54+ } catch (NoSuchPaddingException e ) {
55+ setError (3 );
56+ Utils .logError (e );
57+ } catch (InvalidKeyLengthException e ) {
58+ setError (4 , e .getMessage ());
59+ Utils .logError (e );
60+ } catch (AlgorithmNotSupportedException e ) {
61+ setError (2 );
62+ Utils .logError (e );
63+ }
64+
65+ }
66+ }
67+
68+ private boolean validPropertyValue (String value ) {
69+ return value != null && !value .equals ("" );
70+ }
71+
72+ public String encrypt (String text ) {
73+ Initialize ();
74+ String encrypted = "" ;
75+ if (!anyError ()) {
76+ try {
77+ encrypted = _symAlg .encrypt (text );
78+ } catch (EncryptionException e ) {
79+ setError (1 );
80+ Utils .logError (e );
81+ }
82+ }
83+ return encrypted ;
84+ }
85+
86+ public String decrypt (String text ) {
87+ Initialize ();
88+ String decrypted = "" ;
89+ if (!anyError ()) {
90+ try {
91+ if (getIV ().equals ("" )){
92+ setError (5 );
93+ return "" ;
94+ }
95+ decrypted = _symAlg .decrypt (text );
96+ } catch (EncryptionException e ) {
97+ setError (1 );
98+ Utils .logError (e );
99+ }
100+ }
101+ return decrypted ;
102+ }
103+
104+ public String getAlgorithm () {
105+ return _algorithm ;
106+ }
107+
108+ public void setAlgorithm (String algorithm )
109+ {
110+ //Android , https://developer.android.com/reference/android/security/keystore/KeyProperties.html#KEY_ALGORITHM_AES
111+ // Rijndael == AES
112+ // TripleDES == DESede
113+ // SHA-256 == HmacSHA256
114+ // change to Android KeyProperties
115+ if (algorithm .equalsIgnoreCase ("Rijndael" )) { algorithm = "AES" ; }
116+ if (algorithm .equalsIgnoreCase ("TripleDES" )) { algorithm = "DESede" ; }
117+ if (algorithm .equalsIgnoreCase ("SHA-256" )) { algorithm = "HmacSHA256" ; }
118+
119+
120+ algorithm = String .format ("%s/%s/%s" , algorithm , Constants .DEFAULT_SYM_MODE , Constants .DEFAULT_SYM_PADDING );
121+ isDirty = isDirty || !this ._algorithm .equals (algorithm );
122+ this ._algorithm = algorithm ;
123+ }
124+
125+ public String getKey () {
126+ if (!anyError () && _symAlg != null )
127+ return _symAlg .getKey ();
128+ return _key ;
129+ }
130+
131+ public void setKey (String key ) {
132+ isDirty = isDirty || !this ._key .equals (key );
133+ this ._key = key ;
134+ }
135+
136+ public String getIV () {
137+ if (!anyError () && _symAlg != null )
138+ return _symAlg .getIV ();
139+ return _iv ;
140+ }
141+
142+ public void setIV (String iv ) {
143+ isDirty = isDirty || !this ._iv .equals (iv );
144+ this ._iv = iv ;
145+ }
146+
147+ public int getKeySize () {
148+ if (!anyError () && _symAlg != null )
149+ return _symAlg .getKeySize ();
150+ return _keySize ;
151+ }
152+
153+ public void setKeySize (int keySize ) {
154+ isDirty = isDirty || this ._keySize != keySize ;
155+ this ._keySize = keySize ;
156+
157+ }
158+
159+ public int getBlockSize () {
160+ if (!anyError () && _symAlg != null )
161+ return _symAlg .getBlockSize ();
162+ return _blockSize ;
163+ }
164+
165+ public void setBlockSize (int blockSize ) {
166+ isDirty = isDirty || this ._blockSize != blockSize ;
167+ this ._blockSize = blockSize ;
168+ }
169+
170+ private void setError (int errorCode ) {
171+ setError (errorCode , "" );
172+ }
173+
174+ private void setError (int errorCode , String errDsc ) {
175+ _lastError = errorCode ;
176+ switch (errorCode ) {
177+ case 0 :
178+ _lastErrorDescription = "" ;
179+ break ;
180+ case 1 :
181+ _lastErrorDescription = Constants .ENCRYPTION_ERROR ;
182+ break ;
183+ case 2 :
184+ _lastErrorDescription = Constants .ALGORITHM_NOT_SUPPORTED ;
185+ break ;
186+ case 3 :
187+ _lastErrorDescription = Constants .ENCRYPTION_ERROR ;
188+ break ;
189+ case 4 :
190+ _lastErrorDescription = Constants .KEY_NOT_VALID ;
191+ break ;
192+ case 5 :
193+ _lastErrorDescription = "IV must be set for Decryption" ;
194+ break ;
195+ default :
196+ break ;
197+ }
198+ if (!errDsc .equals ("" )) {
199+ _lastErrorDescription = errDsc ;
200+ }
201+ }
202+
203+ private boolean anyError () {
204+ return _lastError != 0 ;
205+ }
206+
207+ public int getErrCode () {
208+ return _lastError ;
209+ }
210+
211+ public String getErrDescription () {
212+ return _lastErrorDescription ;
213+ }
214+ }
0 commit comments