|
1 | | -// This file was generated by Mendix Studio Pro. |
2 | | -// |
3 | | -// WARNING: Only the following code will be retained when actions are regenerated: |
4 | | -// - the import list |
5 | | -// - the code between BEGIN USER CODE and END USER CODE |
6 | | -// - the code between BEGIN EXTRA CODE and END EXTRA CODE |
7 | | -// Other code you write will be lost the next time you deploy the project. |
8 | | -// Special characters, e.g., é, ö, à, etc. are supported in comments. |
9 | | - |
10 | | -package mendixsso.actions; |
11 | | - |
12 | | -import com.mendix.systemwideinterfaces.MendixRuntimeException; |
13 | | -import com.mendix.systemwideinterfaces.core.IContext; |
14 | | -import com.mendix.webui.CustomJavaAction; |
15 | | -import javax.crypto.BadPaddingException; |
16 | | -import javax.crypto.Cipher; |
17 | | -import javax.crypto.spec.GCMParameterSpec; |
18 | | -import javax.crypto.spec.IvParameterSpec; |
19 | | -import javax.crypto.spec.SecretKeySpec; |
20 | | -import java.security.InvalidAlgorithmParameterException; |
21 | | -import java.util.Base64; |
22 | | - |
23 | | -public class DecryptString extends CustomJavaAction<java.lang.String> |
24 | | -{ |
25 | | - private java.lang.String value; |
26 | | - private java.lang.String key; |
27 | | - private java.lang.String prefix; |
28 | | - |
29 | | - public DecryptString(IContext context, java.lang.String value, java.lang.String key, java.lang.String prefix) |
30 | | - { |
31 | | - super(context); |
32 | | - this.value = value; |
33 | | - this.key = key; |
34 | | - this.prefix = prefix; |
35 | | - } |
36 | | - |
37 | | - @java.lang.Override |
38 | | - public java.lang.String executeAction() throws Exception |
39 | | - { |
40 | | - // BEGIN USER CODE |
41 | | - if (this.value == null || !isStartsWithRightPrefix()) |
42 | | - return null; |
43 | | - if (this.prefix == null || this.prefix.isEmpty()) |
44 | | - throw new MendixRuntimeException("Prefix should not be empty"); |
45 | | - if (this.key == null || this.key.isEmpty()) |
46 | | - throw new MendixRuntimeException("Key should not be empty"); |
47 | | - if (this.key.length() != 16) |
48 | | - throw new MendixRuntimeException("Key length should be 16"); |
49 | | - |
50 | | - String decryptedText = null; |
51 | | - |
52 | | - if (isEncryptedWithLegacyAlgorithm(this.value)) { |
53 | | - decryptedText = decryptUsingLegacyAlgorithm(); |
54 | | - } else { |
55 | | - decryptedText = decryptUsingGcm(); |
56 | | - } |
57 | | - |
58 | | - return decryptedText; |
59 | | - // END USER CODE |
60 | | - } |
61 | | - |
62 | | - /** |
63 | | - * Returns a string representation of this action |
64 | | - */ |
65 | | - @java.lang.Override |
66 | | - public java.lang.String toString() |
67 | | - { |
68 | | - return "DecryptString"; |
69 | | - } |
70 | | - |
71 | | - // BEGIN EXTRA CODE |
72 | | - private final int GCM_TAG_LENGTH = 16; // in bytes |
73 | | - private final String LEGACY_PREFIX = "{AES}"; |
74 | | - private final String WRONG_KEY_ERROR_MESSAGE = "Cannot decrypt the text because it was either NOT encrypted with a key of length 16 or they key is different"; |
75 | | - |
76 | | - private String decryptUsingGcm() throws Exception { |
77 | | - String[] s = this.value.substring(this.prefix.length()).split(";"); |
78 | | - |
79 | | - if (s.length < 2) //Not an encrypted string, just return the original value. |
80 | | - return this.value; |
81 | | - |
82 | | - Cipher c = Cipher.getInstance("AES/GCM/PKCS5PADDING"); |
83 | | - SecretKeySpec k = new SecretKeySpec(this.key.getBytes(), "AES"); |
84 | | - |
85 | | - try { |
86 | | - GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, Base64.getDecoder().decode(s[0])); |
87 | | - c.init(Cipher.DECRYPT_MODE, k, spec); |
88 | | - byte[] encryptedData = Base64.getDecoder().decode(s[1]); |
89 | | - return new String(c.doFinal(encryptedData)); |
90 | | - } catch (InvalidAlgorithmParameterException | BadPaddingException ex) { |
91 | | - if (isEncryptedWithWrongKey(ex.getMessage())) |
92 | | - throw new MendixRuntimeException(WRONG_KEY_ERROR_MESSAGE); |
93 | | - else throw ex; |
94 | | - } |
95 | | - } |
96 | | - |
97 | | - private boolean isEncryptedWithWrongKey(String message) { |
98 | | - return |
99 | | - message.equals("Wrong IV length: must be 16 bytes long") || |
100 | | - message.equals("Given final block not properly padded"); |
101 | | - } |
102 | | - |
103 | | - private String decryptUsingLegacyAlgorithm() throws Exception { |
104 | | - String[] s = this.value.substring(LEGACY_PREFIX.length()).split(";"); |
105 | | - |
106 | | - if (s.length < 2) //Not an encrypted string, just return the original value. |
107 | | - return this.value; |
108 | | - |
109 | | - Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING"); |
110 | | - SecretKeySpec k = new SecretKeySpec(this.key.getBytes(), "AES"); |
111 | | - |
112 | | - try { |
113 | | - c.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(Base64.getDecoder().decode(s[0]))); |
114 | | - byte[] encryptedData = Base64.getDecoder().decode(s[1]); |
115 | | - return new String(c.doFinal(encryptedData)); |
116 | | - } catch (InvalidAlgorithmParameterException | BadPaddingException ex) { |
117 | | - if (isEncryptedWithWrongKey(ex.getMessage())) |
118 | | - throw new MendixRuntimeException(WRONG_KEY_ERROR_MESSAGE); |
119 | | - else throw ex; |
120 | | - } |
121 | | - } |
122 | | - |
123 | | - private boolean isEncryptedWithLegacyAlgorithm(String text) { |
124 | | - return text.startsWith(LEGACY_PREFIX); |
125 | | - } |
126 | | - |
127 | | - private boolean isStartsWithRightPrefix() { |
128 | | - return this.value.startsWith(this.value) || isEncryptedWithLegacyAlgorithm(this.value); |
129 | | - } |
130 | | - // END EXTRA CODE |
131 | | -} |
| 1 | +// This file was generated by Mendix Studio Pro. |
| 2 | +// |
| 3 | +// WARNING: Only the following code will be retained when actions are regenerated: |
| 4 | +// - the import list |
| 5 | +// - the code between BEGIN USER CODE and END USER CODE |
| 6 | +// - the code between BEGIN EXTRA CODE and END EXTRA CODE |
| 7 | +// Other code you write will be lost the next time you deploy the project. |
| 8 | +// Special characters, e.g., é, ö, à, etc. are supported in comments. |
| 9 | + |
| 10 | +package mendixsso.actions; |
| 11 | + |
| 12 | +import com.mendix.systemwideinterfaces.MendixRuntimeException; |
| 13 | +import com.mendix.systemwideinterfaces.core.IContext; |
| 14 | +import com.mendix.webui.CustomJavaAction; |
| 15 | +import javax.crypto.BadPaddingException; |
| 16 | +import javax.crypto.Cipher; |
| 17 | +import javax.crypto.spec.GCMParameterSpec; |
| 18 | +import javax.crypto.spec.IvParameterSpec; |
| 19 | +import javax.crypto.spec.SecretKeySpec; |
| 20 | +import java.security.InvalidAlgorithmParameterException; |
| 21 | +import java.util.Base64; |
| 22 | + |
| 23 | +public class DecryptString extends CustomJavaAction<java.lang.String> |
| 24 | +{ |
| 25 | + private java.lang.String value; |
| 26 | + private java.lang.String key; |
| 27 | + private java.lang.String prefix; |
| 28 | + |
| 29 | + public DecryptString(IContext context, java.lang.String value, java.lang.String key, java.lang.String prefix) |
| 30 | + { |
| 31 | + super(context); |
| 32 | + this.value = value; |
| 33 | + this.key = key; |
| 34 | + this.prefix = prefix; |
| 35 | + } |
| 36 | + |
| 37 | + @java.lang.Override |
| 38 | + public java.lang.String executeAction() throws Exception |
| 39 | + { |
| 40 | + // BEGIN USER CODE |
| 41 | + if (this.value == null || !isStartsWithRightPrefix()) |
| 42 | + return null; |
| 43 | + if (this.prefix == null || this.prefix.isEmpty()) |
| 44 | + throw new MendixRuntimeException("Prefix should not be empty"); |
| 45 | + if (this.key == null || this.key.isEmpty()) |
| 46 | + throw new MendixRuntimeException("Key should not be empty"); |
| 47 | + if (this.key.length() != 16) |
| 48 | + throw new MendixRuntimeException("Key length should be 16"); |
| 49 | + |
| 50 | + String decryptedText = null; |
| 51 | + |
| 52 | + if (isEncryptedWithLegacyAlgorithm(this.value)) { |
| 53 | + decryptedText = decryptUsingLegacyAlgorithm(); |
| 54 | + } else { |
| 55 | + decryptedText = decryptUsingGcm(); |
| 56 | + } |
| 57 | + |
| 58 | + return decryptedText; |
| 59 | + // END USER CODE |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns a string representation of this action |
| 64 | + */ |
| 65 | + @java.lang.Override |
| 66 | + public java.lang.String toString() |
| 67 | + { |
| 68 | + return "DecryptString"; |
| 69 | + } |
| 70 | + |
| 71 | + // BEGIN EXTRA CODE |
| 72 | + private final int GCM_TAG_LENGTH = 16; // in bytes |
| 73 | + private final String LEGACY_PREFIX = "{AES}"; |
| 74 | + private final String WRONG_KEY_ERROR_MESSAGE = "Cannot decrypt the text because it was either NOT encrypted with a key of length 16 or they key is different"; |
| 75 | + |
| 76 | + private String decryptUsingGcm() throws Exception { |
| 77 | + String[] s = this.value.substring(this.prefix.length()).split(";"); |
| 78 | + |
| 79 | + if (s.length < 2) //Not an encrypted string, just return the original value. |
| 80 | + return this.value; |
| 81 | + |
| 82 | + Cipher c = Cipher.getInstance("AES/GCM/PKCS5PADDING"); |
| 83 | + SecretKeySpec k = new SecretKeySpec(this.key.getBytes(), "AES"); |
| 84 | + |
| 85 | + try { |
| 86 | + GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, Base64.getDecoder().decode(s[0])); |
| 87 | + c.init(Cipher.DECRYPT_MODE, k, spec); |
| 88 | + byte[] encryptedData = Base64.getDecoder().decode(s[1]); |
| 89 | + return new String(c.doFinal(encryptedData)); |
| 90 | + } catch (InvalidAlgorithmParameterException | BadPaddingException ex) { |
| 91 | + if (isEncryptedWithWrongKey(ex.getMessage())) |
| 92 | + throw new MendixRuntimeException(WRONG_KEY_ERROR_MESSAGE); |
| 93 | + else throw ex; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private boolean isEncryptedWithWrongKey(String message) { |
| 98 | + return |
| 99 | + message.equals("Wrong IV length: must be 16 bytes long") || |
| 100 | + message.equals("Given final block not properly padded"); |
| 101 | + } |
| 102 | + |
| 103 | + private String decryptUsingLegacyAlgorithm() throws Exception { |
| 104 | + String[] s = this.value.substring(LEGACY_PREFIX.length()).split(";"); |
| 105 | + |
| 106 | + if (s.length < 2) //Not an encrypted string, just return the original value. |
| 107 | + return this.value; |
| 108 | + |
| 109 | + Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING"); |
| 110 | + SecretKeySpec k = new SecretKeySpec(this.key.getBytes(), "AES"); |
| 111 | + |
| 112 | + try { |
| 113 | + c.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(Base64.getDecoder().decode(s[0]))); |
| 114 | + byte[] encryptedData = Base64.getDecoder().decode(s[1]); |
| 115 | + return new String(c.doFinal(encryptedData)); |
| 116 | + } catch (InvalidAlgorithmParameterException | BadPaddingException ex) { |
| 117 | + if (isEncryptedWithWrongKey(ex.getMessage())) |
| 118 | + throw new MendixRuntimeException(WRONG_KEY_ERROR_MESSAGE); |
| 119 | + else throw ex; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private boolean isEncryptedWithLegacyAlgorithm(String text) { |
| 124 | + return text.startsWith(LEGACY_PREFIX); |
| 125 | + } |
| 126 | + |
| 127 | + private boolean isStartsWithRightPrefix() { |
| 128 | + return this.value.startsWith(this.value) || isEncryptedWithLegacyAlgorithm(this.value); |
| 129 | + } |
| 130 | + // END EXTRA CODE |
| 131 | +} |
0 commit comments