# Enhancing Resilience in Digital Twins: ASCON-Based Security Solutions for Industry 4.0
## 3 Proposed Solution
### Ditto Java Base Payload Mapping  
In the context of Eclipse Ditto [20], data storage and transfer are facilitated through a format known as the Ditto protocol. This protocol utilizes a JSON structure, employing key-value pairs to represent and transmit information. To seamlessly integrate with Ditto's capabilities, the connectivity microservice bundled with Ditto offers an extension specifically designed for intercepting incoming data. This extension allows for the mapping of data from its original form to a format that Ditto can understand and store in its underlying MongoDB database. Using the Ditto payload mapping feature, we can decrypt incoming encrypted payload messages and convert them into a format that Ditto can process and store. With the payload mapping feature in Ditto's connectivity microservice, we can achieve the following: receive encrypted data from the IoT device, decrypt and authenticate it, and convert it into Ditto protocol messages. This helps ensure that the data sent between the IoT device and Ditto is secure and authentic. To implement our custom mapping functionality to encrypt and decrypt, we performed the following steps and illustrated in Figure 3:  
* **Build Encryption Algorithms in Java:** We created a JAR package from the encryption algorithms implemented in Java. This class provides the ASCON or AES-GCM encryption and decryption operations needed for secure communication and data handling. * **Develop Custom Message Mapper Class:** A custom message mapper class was developed to handle the conversion of incoming device messages to the appropriate Ditto protocol format. This class integrates with the encryption and decryption functionality to ensure data integrity and security during the mapping process. * **Configure Ditto Connectivity Microservice:** The Ditto connectivity microservice was configured to recognize and load our custom message mapper. This configuration step ensures that incoming messages are routed to our custom mapper for processing, enabling seamless integration of our specific data transformation requirements within the Ditto framework. * **Load Custom Message Mapper:** The custom message mapper is loaded by the Ditto connectivity microservice, ready to process incoming messages. * **Process Incoming Messages:** Incoming messages are processed by the custom message mapper. * **Encrypt/Decrypt Data:** Data within the incoming messages are encrypted or decrypted using the specified encryption algorithms (ASCON or AES-GCM). * **Convert to Ditto Protocol Format:** The processed and secured data are converted to the Ditto protocol format. To integrate our encryption jar packages with Ditto, we added the following lines to connection configurations \"source\": caption=Addition to \"sources\" in the connection configuration file as illustrated in Listing 1:  
```
\"payloadMapping\":[\"AsconPayload\"], \"replyTarget\":[ \"headerMapping\":[], \"expectedResponseTypes\":[ \"response\", \"error\" ], \"enabled\":false }
```  
And the following lines to the \"targets\" of the same file as illustrated in Listing 2:```
headerMapping^:{};{}; payloadMapping^:[\"AsconPayload\"]
```

In these lines, the \"AsconPayload\" was a reference to our encryption/decryption jar package. Within the Eclipse Ditto framework, data storage and transfer occur using the Ditto protocol, employing JSON format and key-value pairs for information representation. To integrate seamlessly with Ditto, its connectivity microservice offers an extension designed to intercept incoming data. This extension maps data to a format Ditto comprehends, storing it in MongoDB. Utilizing Ditto's payload mapping, encrypted payloads are decrypted and converted, ensuring secure and authentic communication between IoT devices and Ditto. To implement custom mapping for encryption and decryption, specific steps are followed. Firstly, a Java class providing ASCON or AES-GCM encryption/decryption operations is built. Then, a custom message mapper class is developed to convert device messages to the Ditto protocol format.