Skip to content

Commit 27874ab

Browse files
committed
Revert "Remove implementation of deprecated Cryptography data types"
This reverts commit 2f48e22.
1 parent 469a962 commit 27874ab

File tree

2 files changed

+387
-0
lines changed

2 files changed

+387
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package com.genexus.cryptography;
2+
3+
import com.genexus.cryptography.encryption.asymmetric.CipherAsymProvider;
4+
import com.genexus.cryptography.encryption.asymmetric.IGXAsymEncryption;
5+
import com.genexus.cryptography.exception.AlgorithmNotSupportedException;
6+
import com.genexus.cryptography.exception.EncryptionException;
7+
import com.genexus.cryptography.exception.PrivateKeyNotFoundException;
8+
import com.genexus.cryptography.exception.PublicKeyNotFoundException;
9+
10+
public class GXAsymEncryption {
11+
12+
private static final String DEFAULT_SYM_ALGORITHM = "RSA";
13+
private static final String DEFAULT_SYM_PADDING = "PKCS1Padding";
14+
private static final String DEFAULT_SYM_MODE = "ECB";
15+
16+
private static final String SHA256_SYM_PADDING = "OAEPWithSHA-256AndMGF1Padding";
17+
18+
private int _lastError;
19+
private String _lastErrorDescription;
20+
private String _algorithm;
21+
22+
private GXCertificate _cert;
23+
private IGXAsymEncryption _asymAlg;
24+
private boolean _isDirty;
25+
26+
public GXAsymEncryption() {
27+
_isDirty = true;
28+
_algorithm = String.format("%s/%s/%s", DEFAULT_SYM_ALGORITHM,
29+
DEFAULT_SYM_MODE, DEFAULT_SYM_PADDING);
30+
initialize();
31+
}
32+
33+
private void initialize() {
34+
if (_isDirty) {
35+
// Support algorithms = RSA only for now..
36+
// SHA256 ?
37+
setError(0);
38+
39+
if (_cert != null && _cert.certLoaded() == true) {
40+
try {
41+
_asymAlg = new CipherAsymProvider(_algorithm,
42+
_cert.getPublicKey(), _cert.getPrivateKey());
43+
_isDirty = false;
44+
} catch (AlgorithmNotSupportedException e) {
45+
setError(2);
46+
}
47+
} else {
48+
setError(4);
49+
}
50+
}
51+
}
52+
53+
public String encrypt(String text) {
54+
initialize();
55+
String encrypted = "";
56+
if (!anyError()) {
57+
try {
58+
encrypted = _asymAlg.encrypt(text);
59+
} catch (PublicKeyNotFoundException e) {
60+
setError(4);
61+
} catch (EncryptionException e) {
62+
setError(3);
63+
Utils.logError(e);
64+
}
65+
}
66+
return encrypted;
67+
}
68+
69+
public String decrypt(String text) {
70+
initialize();
71+
String decrypted = "";
72+
if (!anyError()) {
73+
74+
try {
75+
decrypted = _asymAlg.decrypt(text);
76+
} catch (PrivateKeyNotFoundException e) {
77+
setError(5);
78+
} catch (EncryptionException e) {
79+
setError(3);
80+
Utils.logError(e);
81+
}
82+
}
83+
return decrypted;
84+
}
85+
86+
private void setError(int errorCode) {
87+
setError(errorCode, "");
88+
}
89+
90+
private void setError(int errorCode, String errDsc) {
91+
_lastError = errorCode;
92+
switch (errorCode) {
93+
case 0:
94+
_lastErrorDescription = Constants.OK;
95+
break;
96+
case 1:
97+
break;
98+
case 2:
99+
_lastErrorDescription = Constants.ALGORITHM_NOT_SUPPORTED;
100+
break;
101+
case 3:
102+
_lastErrorDescription = Constants.ENCRYPTION_ERROR;
103+
break;
104+
case 4:
105+
_lastErrorDescription = "";
106+
break;
107+
case 5:
108+
_lastErrorDescription = Constants.PRIVATEKEY_NOT_PRESENT;
109+
break;
110+
default:
111+
break;
112+
}
113+
if (!errDsc.equals("")) {
114+
_lastErrorDescription = errDsc;
115+
}
116+
}
117+
118+
public String getAlgorithm() {
119+
return _algorithm;
120+
}
121+
122+
public void setAlgorithm(String value)
123+
{
124+
//Android , https://developer.android.com/reference/android/security/keystore/KeyProperties.html#KEY_ALGORITHM_AES
125+
// RSA == RSA
126+
// only support RSA https://developer.android.com/reference/javax/crypto/Cipher.html
127+
// change to Android KeyProperties
128+
//if (value.equalsIgnoreCase("SHA1")) { value = "HmacSHA1"; }
129+
//if (value.equalsIgnoreCase("SHA256")) { value = "HmacSHA256"; }
130+
//if (value.equalsIgnoreCase("SHA512")) { value = "HmacSHA512"; }
131+
132+
// convert sha256 to RSA with sha256
133+
if (value.equalsIgnoreCase("SHA256"))
134+
{
135+
value = String.format("%s/%s/%s", DEFAULT_SYM_ALGORITHM, DEFAULT_SYM_MODE,
136+
SHA256_SYM_PADDING);
137+
}
138+
else
139+
{
140+
value = String.format("%s/%s/%s", value, DEFAULT_SYM_MODE,
141+
DEFAULT_SYM_PADDING);
142+
}
143+
_isDirty = _isDirty || !value.equals(_algorithm);
144+
_algorithm = value;
145+
}
146+
147+
public GXCertificate getCertificate() {
148+
return _cert;
149+
150+
}
151+
152+
public void setCertificate(GXCertificate cert) {
153+
_isDirty = _isDirty || cert != _cert;
154+
_cert = cert;
155+
}
156+
157+
private boolean anyError() {
158+
159+
if (_cert == null || (!_cert.certLoaded() == true)) {
160+
setError(4); // Certificate not initialized
161+
}
162+
return _lastError != 0;
163+
164+
}
165+
166+
public int getErrCode() {
167+
return _lastError;
168+
}
169+
170+
public String getErrDescription() {
171+
return _lastErrorDescription;
172+
}
173+
}
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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

Comments
 (0)