This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-kamelets.git


The following commit(s) were added to refs/heads/main by this push:
     new 18d1e063e Fix #3246: crypto-encrypt-action and crypto-decrypt-action 
allow the CBC mode with a random initialization vector (#3247)
18d1e063e is described below

commit 18d1e063e363627719bc5b1c5a1434405726e82b
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 19 07:35:21 2026 +0100

    Fix #3246: crypto-encrypt-action and crypto-decrypt-action allow the CBC 
mode with a random initialization vector (#3247)
    
    With the algorithm name alone, such as AES, Java encrypts in ECB mode,
    and a full transformation fails because the same value names the key.
    A new optional mode option uses <algorithm>/<mode>/PKCS5Padding with a
    new random initialization vector put in front of each message. Without
    mode nothing changes, so earlier data still decrypts.
    
    Signed-off-by: Claus Ibsen <[email protected]>
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 kamelets/crypto-decrypt-action.kamelet.yaml        | 31 +++++--
 kamelets/crypto-encrypt-action.kamelet.yaml        | 31 +++++--
 library/camel-kamelets-catalog/pom.xml             |  7 ++
 .../camel/kamelets/catalog/CryptoKameletsTest.java | 96 ++++++++++++++++++++++
 .../main/resources/META-INF/kamelets-index.json    |  4 +-
 .../kamelets/crypto-decrypt-action.kamelet.yaml    | 31 +++++--
 .../kamelets/crypto-encrypt-action.kamelet.yaml    | 31 +++++--
 7 files changed, 205 insertions(+), 26 deletions(-)

diff --git a/kamelets/crypto-decrypt-action.kamelet.yaml 
b/kamelets/crypto-decrypt-action.kamelet.yaml
index d3174a96a..f1d2b5686 100644
--- a/kamelets/crypto-decrypt-action.kamelet.yaml
+++ b/kamelets/crypto-decrypt-action.kamelet.yaml
@@ -39,7 +39,7 @@ spec:
 
       Output: the body is the original data, as bytes (a stream). The headers 
are kept.
 
-      Good to know: data that was changed, or a different `algorithm` or `key` 
than when encrypting, fails the message with "Message authentication failed". 
The key is a secret; keep it in a vault or a secret, not in plain configuration.
+      Good to know: data that was changed, or a different `algorithm`, `mode` 
or `key` than when encrypting, fails the message with "Message authentication 
failed". The key is a secret; keep it in a vault or a secret, not in plain 
configuration.
     required:
       - algorithm
       - key
@@ -47,9 +47,14 @@ spec:
     properties:
       algorithm:
         title: Algorithm
-        description: "The name of the encryption algorithm the data was 
encrypted with, such as `AES`, or `DESede` for Triple DES. Give the name alone: 
a full form such as `AES/CBC/PKCS5Padding` fails each message."
+        description: "The name of the encryption algorithm the data was 
encrypted with, such as `AES`, or `DESede` for Triple DES. Give the name alone: 
a full form such as `AES/CBC/PKCS5Padding` fails each message; the mode is set 
with `mode`."
         type: string
         example: "AES"
+      mode:
+        title: Mode
+        description: "The block cipher mode, `CBC`: each message is then 
encrypted with a new random initialization vector, which is put in front of the 
encrypted data, so the same text gives different bytes each time. When it is 
not set, `AES` and `DESede` use ECB mode, in which the same text with the same 
key always gives the same bytes, so repeats in the data can be seen. Use the 
same value in `crypto-encrypt-action`."
+        type: string
+        example: "CBC"
       key:
         title: Secret Key
         description: "The secret key the data was encrypted with, as text. Its 
length must fit the algorithm: for `AES`, 16, 24 or 32 characters (plain ASCII 
characters count as one byte each), and for `DESede`, 24."
@@ -70,7 +75,21 @@ spec:
     from:
       uri: kamelet:source
       steps:
-        - unmarshal:
-            crypto:
-              algorithm: "{{algorithm}}"
-              key: "{{cryptoKey}}"
+        - choice:
+            precondition: true
+            when:
+              - expression:
+                  simple:
+                    expression: "${propertiesExist:mode}"
+                steps:
+                  - unmarshal:
+                      crypto:
+                        algorithm: "{{algorithm}}/{{mode}}/PKCS5Padding"
+                        key: "{{cryptoKey}}"
+                        inline: true
+            otherwise:
+              steps:
+                - unmarshal:
+                    crypto:
+                      algorithm: "{{algorithm}}"
+                      key: "{{cryptoKey}}"
diff --git a/kamelets/crypto-encrypt-action.kamelet.yaml 
b/kamelets/crypto-encrypt-action.kamelet.yaml
index 756129ff3..b7543f502 100644
--- a/kamelets/crypto-encrypt-action.kamelet.yaml
+++ b/kamelets/crypto-encrypt-action.kamelet.yaml
@@ -39,7 +39,7 @@ spec:
 
       Output: the body is the encrypted data, as bytes (a stream), a little 
longer than the input. It is not text: to send it where only text fits, encode 
it, for example in Base64. The headers are kept.
 
-      Good to know: the key is a secret; anyone who has it can decrypt the 
data, so keep it in a vault or a secret, not in plain configuration. The 
encrypted data carries a check value, so `crypto-decrypt-action` fails on data 
that was changed or decrypted with another key. A key of the wrong length fails 
each message, not the start.
+      Good to know: the key is a secret; anyone who has it can decrypt the 
data, so keep it in a vault or a secret, not in plain configuration. The 
encrypted data carries a check value, so `crypto-decrypt-action` fails on data 
that was changed or decrypted with another key. A key of the wrong length fails 
each message, not the start. Set `mode` to `CBC`: without it, AES encrypts in 
ECB mode, where equal parts of the data give equal encrypted bytes. Data 
encrypted without `mode` is decryp [...]
     required:
       - algorithm
       - key
@@ -47,9 +47,14 @@ spec:
     properties:
       algorithm:
         title: Algorithm
-        description: "The name of the encryption algorithm, such as `AES`, or 
`DESede` for Triple DES. Give the name alone: a full form such as 
`AES/CBC/PKCS5Padding` fails each message. Use the same value in 
`crypto-decrypt-action`."
+        description: "The name of the encryption algorithm, such as `AES`, or 
`DESede` for Triple DES. Give the name alone: a full form such as 
`AES/CBC/PKCS5Padding` fails each message; choose the mode with `mode`. Use the 
same value in `crypto-decrypt-action`."
         type: string
         example: "AES"
+      mode:
+        title: Mode
+        description: "The block cipher mode, `CBC`: each message is then 
encrypted with a new random initialization vector, which is put in front of the 
encrypted data, so the same text gives different bytes each time. When it is 
not set, `AES` and `DESede` use ECB mode, in which the same text with the same 
key always gives the same bytes, so repeats in the data can be seen. Use the 
same value in `crypto-decrypt-action`."
+        type: string
+        example: "CBC"
       key:
         title: Secret Key
         description: "The secret key, as text. Its length must fit the 
algorithm: for `AES`, 16, 24 or 32 characters (plain ASCII characters count as 
one byte each), and for `DESede`, 24. Use the same key in 
`crypto-decrypt-action`."
@@ -70,7 +75,21 @@ spec:
     from:
       uri: kamelet:source
       steps:
-        - marshal:
-            crypto:
-              algorithm: "{{algorithm}}"
-              key: "{{cryptoKey}}"
+        - choice:
+            precondition: true
+            when:
+              - expression:
+                  simple:
+                    expression: "${propertiesExist:mode}"
+                steps:
+                  - marshal:
+                      crypto:
+                        algorithm: "{{algorithm}}/{{mode}}/PKCS5Padding"
+                        key: "{{cryptoKey}}"
+                        inline: true
+            otherwise:
+              steps:
+                - marshal:
+                    crypto:
+                      algorithm: "{{algorithm}}"
+                      key: "{{cryptoKey}}"
diff --git a/library/camel-kamelets-catalog/pom.xml 
b/library/camel-kamelets-catalog/pom.xml
index f2d8ca5ae..c763fa7ac 100644
--- a/library/camel-kamelets-catalog/pom.xml
+++ b/library/camel-kamelets-catalog/pom.xml
@@ -149,6 +149,13 @@
             <version>${camel.version}</version>
             <scope>test</scope>
         </dependency>
+        <!-- CryptoKameletsTest: encrypts and decrypts with the crypto actions 
-->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-crypto</artifactId>
+            <version>${camel.version}</version>
+            <scope>test</scope>
+        </dependency>
         <!-- HttpSinkSecretTest: runs http-sink against a local server -->
         <dependency>
             <groupId>org.apache.camel</groupId>
diff --git 
a/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/CryptoKameletsTest.java
 
b/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/CryptoKameletsTest.java
new file mode 100644
index 000000000..74ff48384
--- /dev/null
+++ 
b/library/camel-kamelets-catalog/src/test/java/org/apache/camel/kamelets/catalog/CryptoKameletsTest.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.kamelets.catalog;
+
+import java.nio.file.Path;
+import java.util.Arrays;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.kamelet.KameletComponent;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+/**
+ * Encrypts with crypto-encrypt-action and decrypts with 
crypto-decrypt-action, with and without a mode.
+ */
+public class CryptoKameletsTest {
+
+    // the source files, not the catalog jar
+    private static final Path KAMELETS = Path.of("../../kamelets");
+    // two equal blocks of 16 bytes, so ECB shows the repeat
+    private static final String TEXT = "0123456789abcdef0123456789abcdef";
+
+    private CamelContext context;
+
+    @BeforeEach
+    void start() throws Exception {
+        context = new DefaultCamelContext();
+        context.getComponent("kamelet", KameletComponent.class)
+                .setLocation("file:" + KAMELETS.toAbsolutePath().normalize());
+        context.start();
+    }
+
+    @AfterEach
+    void stop() {
+        if (context != null) {
+            context.stop();
+        }
+    }
+
+    @ParameterizedTest
+    @CsvSource({ "AES,0123456789abcdef", "DESede,0123456789abcdef01234567" })
+    void cbcGivesDifferentBytesForTheSameText(String algorithm, String key) {
+        String options = "?algorithm=" + algorithm + "&key=RAW(" + key + 
")&mode=CBC";
+
+        byte[] first = encrypt(options);
+        byte[] second = encrypt(options);
+
+        assertFalse(Arrays.equals(first, second), "a new initialization vector 
for each message");
+        assertEquals(TEXT, decrypt(options, first));
+        assertEquals(TEXT, decrypt(options, second));
+    }
+
+    @ParameterizedTest
+    @CsvSource({ "AES,0123456789abcdef" })
+    void withoutModeTheDataOfEarlierVersionsIsStillDecrypted(String algorithm, 
String key) {
+        String options = "?algorithm=" + algorithm + "&key=RAW(" + key + ")";
+
+        byte[] first = encrypt(options);
+
+        // ECB, as before: the same text gives the same bytes
+        assertArrayEquals(first, encrypt(options));
+        assertEquals(TEXT, decrypt(options, first));
+    }
+
+    private byte[] encrypt(String options) {
+        ProducerTemplate template = context.createProducerTemplate();
+        return template.requestBody("kamelet:crypto-encrypt-action" + options, 
TEXT, byte[].class);
+    }
+
+    private String decrypt(String options, byte[] data) {
+        ProducerTemplate template = context.createProducerTemplate();
+        return template.requestBody("kamelet:crypto-decrypt-action" + options, 
data, String.class);
+    }
+}
diff --git 
a/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json 
b/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
index cba69f028..eeecf0961 100644
--- a/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
+++ b/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
@@ -585,7 +585,7 @@
     "supportLevel" : "Stable",
     "title" : "Crypto Decrypt Action",
     "summary" : "Decrypts the body of each message that 
`crypto-encrypt-action` encrypted, with the same algorithm and secret key.",
-    "description" : "Decrypts the body of each message that 
`crypto-encrypt-action` encrypted, with the same algorithm and secret 
key.\n\nUse it to get back the original data that was encrypted with 
`crypto-encrypt-action`, for example after reading it from storage or receiving 
it from another system.\n\nInput: the encrypted data exactly as 
`crypto-encrypt-action` produced it, as bytes or a stream. If it was encoded on 
the way, for example in Base64, decode it first.\n\nOutput: the body  [...]
+    "description" : "Decrypts the body of each message that 
`crypto-encrypt-action` encrypted, with the same algorithm and secret 
key.\n\nUse it to get back the original data that was encrypted with 
`crypto-encrypt-action`, for example after reading it from storage or receiving 
it from another system.\n\nInput: the encrypted data exactly as 
`crypto-encrypt-action` produced it, as bytes or a stream. If it was encoded on 
the way, for example in Base64, decode it first.\n\nOutput: the body  [...]
     "group" : "Actions",
     "namespace" : "Transformation",
     "provider" : "Apache Software Foundation"
@@ -595,7 +595,7 @@
     "supportLevel" : "Stable",
     "title" : "Crypto Encrypt Action",
     "summary" : "Encrypts the body of each message with a secret key, such as 
an AES key, for `crypto-decrypt-action` to decrypt later with the same key.",
-    "description" : "Encrypts the body of each message with a secret key, such 
as an AES key, for `crypto-decrypt-action` to decrypt later with the same 
key.\n\nUse it to protect sensitive data, such as personal or payment details, 
before it is stored or sent through a system you do not fully trust, and 
decrypt it later with `crypto-decrypt-action` and the same `algorithm` and 
`key`.\n\nInput: text or bytes.\n\nOutput: the body is the encrypted data, as 
bytes (a stream), a little longer  [...]
+    "description" : "Encrypts the body of each message with a secret key, such 
as an AES key, for `crypto-decrypt-action` to decrypt later with the same 
key.\n\nUse it to protect sensitive data, such as personal or payment details, 
before it is stored or sent through a system you do not fully trust, and 
decrypt it later with `crypto-decrypt-action` and the same `algorithm` and 
`key`.\n\nInput: text or bytes.\n\nOutput: the body is the encrypted data, as 
bytes (a stream), a little longer  [...]
     "group" : "Actions",
     "namespace" : "Transformation",
     "provider" : "Apache Software Foundation"
diff --git 
a/library/camel-kamelets/src/main/resources/kamelets/crypto-decrypt-action.kamelet.yaml
 
b/library/camel-kamelets/src/main/resources/kamelets/crypto-decrypt-action.kamelet.yaml
index d3174a96a..f1d2b5686 100644
--- 
a/library/camel-kamelets/src/main/resources/kamelets/crypto-decrypt-action.kamelet.yaml
+++ 
b/library/camel-kamelets/src/main/resources/kamelets/crypto-decrypt-action.kamelet.yaml
@@ -39,7 +39,7 @@ spec:
 
       Output: the body is the original data, as bytes (a stream). The headers 
are kept.
 
-      Good to know: data that was changed, or a different `algorithm` or `key` 
than when encrypting, fails the message with "Message authentication failed". 
The key is a secret; keep it in a vault or a secret, not in plain configuration.
+      Good to know: data that was changed, or a different `algorithm`, `mode` 
or `key` than when encrypting, fails the message with "Message authentication 
failed". The key is a secret; keep it in a vault or a secret, not in plain 
configuration.
     required:
       - algorithm
       - key
@@ -47,9 +47,14 @@ spec:
     properties:
       algorithm:
         title: Algorithm
-        description: "The name of the encryption algorithm the data was 
encrypted with, such as `AES`, or `DESede` for Triple DES. Give the name alone: 
a full form such as `AES/CBC/PKCS5Padding` fails each message."
+        description: "The name of the encryption algorithm the data was 
encrypted with, such as `AES`, or `DESede` for Triple DES. Give the name alone: 
a full form such as `AES/CBC/PKCS5Padding` fails each message; the mode is set 
with `mode`."
         type: string
         example: "AES"
+      mode:
+        title: Mode
+        description: "The block cipher mode, `CBC`: each message is then 
encrypted with a new random initialization vector, which is put in front of the 
encrypted data, so the same text gives different bytes each time. When it is 
not set, `AES` and `DESede` use ECB mode, in which the same text with the same 
key always gives the same bytes, so repeats in the data can be seen. Use the 
same value in `crypto-encrypt-action`."
+        type: string
+        example: "CBC"
       key:
         title: Secret Key
         description: "The secret key the data was encrypted with, as text. Its 
length must fit the algorithm: for `AES`, 16, 24 or 32 characters (plain ASCII 
characters count as one byte each), and for `DESede`, 24."
@@ -70,7 +75,21 @@ spec:
     from:
       uri: kamelet:source
       steps:
-        - unmarshal:
-            crypto:
-              algorithm: "{{algorithm}}"
-              key: "{{cryptoKey}}"
+        - choice:
+            precondition: true
+            when:
+              - expression:
+                  simple:
+                    expression: "${propertiesExist:mode}"
+                steps:
+                  - unmarshal:
+                      crypto:
+                        algorithm: "{{algorithm}}/{{mode}}/PKCS5Padding"
+                        key: "{{cryptoKey}}"
+                        inline: true
+            otherwise:
+              steps:
+                - unmarshal:
+                    crypto:
+                      algorithm: "{{algorithm}}"
+                      key: "{{cryptoKey}}"
diff --git 
a/library/camel-kamelets/src/main/resources/kamelets/crypto-encrypt-action.kamelet.yaml
 
b/library/camel-kamelets/src/main/resources/kamelets/crypto-encrypt-action.kamelet.yaml
index 756129ff3..b7543f502 100644
--- 
a/library/camel-kamelets/src/main/resources/kamelets/crypto-encrypt-action.kamelet.yaml
+++ 
b/library/camel-kamelets/src/main/resources/kamelets/crypto-encrypt-action.kamelet.yaml
@@ -39,7 +39,7 @@ spec:
 
       Output: the body is the encrypted data, as bytes (a stream), a little 
longer than the input. It is not text: to send it where only text fits, encode 
it, for example in Base64. The headers are kept.
 
-      Good to know: the key is a secret; anyone who has it can decrypt the 
data, so keep it in a vault or a secret, not in plain configuration. The 
encrypted data carries a check value, so `crypto-decrypt-action` fails on data 
that was changed or decrypted with another key. A key of the wrong length fails 
each message, not the start.
+      Good to know: the key is a secret; anyone who has it can decrypt the 
data, so keep it in a vault or a secret, not in plain configuration. The 
encrypted data carries a check value, so `crypto-decrypt-action` fails on data 
that was changed or decrypted with another key. A key of the wrong length fails 
each message, not the start. Set `mode` to `CBC`: without it, AES encrypts in 
ECB mode, where equal parts of the data give equal encrypted bytes. Data 
encrypted without `mode` is decryp [...]
     required:
       - algorithm
       - key
@@ -47,9 +47,14 @@ spec:
     properties:
       algorithm:
         title: Algorithm
-        description: "The name of the encryption algorithm, such as `AES`, or 
`DESede` for Triple DES. Give the name alone: a full form such as 
`AES/CBC/PKCS5Padding` fails each message. Use the same value in 
`crypto-decrypt-action`."
+        description: "The name of the encryption algorithm, such as `AES`, or 
`DESede` for Triple DES. Give the name alone: a full form such as 
`AES/CBC/PKCS5Padding` fails each message; choose the mode with `mode`. Use the 
same value in `crypto-decrypt-action`."
         type: string
         example: "AES"
+      mode:
+        title: Mode
+        description: "The block cipher mode, `CBC`: each message is then 
encrypted with a new random initialization vector, which is put in front of the 
encrypted data, so the same text gives different bytes each time. When it is 
not set, `AES` and `DESede` use ECB mode, in which the same text with the same 
key always gives the same bytes, so repeats in the data can be seen. Use the 
same value in `crypto-decrypt-action`."
+        type: string
+        example: "CBC"
       key:
         title: Secret Key
         description: "The secret key, as text. Its length must fit the 
algorithm: for `AES`, 16, 24 or 32 characters (plain ASCII characters count as 
one byte each), and for `DESede`, 24. Use the same key in 
`crypto-decrypt-action`."
@@ -70,7 +75,21 @@ spec:
     from:
       uri: kamelet:source
       steps:
-        - marshal:
-            crypto:
-              algorithm: "{{algorithm}}"
-              key: "{{cryptoKey}}"
+        - choice:
+            precondition: true
+            when:
+              - expression:
+                  simple:
+                    expression: "${propertiesExist:mode}"
+                steps:
+                  - marshal:
+                      crypto:
+                        algorithm: "{{algorithm}}/{{mode}}/PKCS5Padding"
+                        key: "{{cryptoKey}}"
+                        inline: true
+            otherwise:
+              steps:
+                - marshal:
+                    crypto:
+                      algorithm: "{{algorithm}}"
+                      key: "{{cryptoKey}}"

Reply via email to