# Cybersecurity and Medical Imaging: A Simulation-Based Approach to DICOM Communication
## 4 Implementation
### Setting Up the Python Libraries  
Before implementing the DICOM simulator, it is essential to set up and install the required _Pydicom_ and _Pyntedicom_ libraries. The provided code is written in Python and demonstrates a function for sending DICOM files to a PACS server. Providing a pip requirements file or setting up an isolated environment can be streamlined using services such as Docker [47], which simplifies the environment setup process and ensures enhanced portability [27]. An extended explanation of the code follows in the code listing below. In Listing 1, the necessary modules and classes required for the code are imported. _os_ is imported to perform file operations, _dcmread_ is imported from the _Pydicom_ library for reading DICOM files, _JPEG Baseline_ is imported from _pydicom.uid_ for specifying the Transfer Syntax and AE, _StoragePresentationContexts_ is imported from _Pyntedicom_ for managing the application entity and storage presentation contexts, and _CTImageStorage_ is imported from _pynetdicom.so_class_ for specifying the DICOM storage context. ```
1 imports
2 frompydicomimportdcmread
3 frompydicom.uidimportJPEGBaseline
4 frompyntedicomimportAE,StoragePresentationContexts
5 frompyntedicom.so_classimportCTImageStorage
```  
Listing 1: Importing the libraries. The main function _send_dicom_to_pacs()_ (Listing 2) receives as input four arguments: _dicom_folder_ (the path to the folder containing DICOM files), _pacs_ip_ (the IP address of the PACS server), _pacs_port_ (the port number of the PACS server), and _pacs_ae_title_ (the AE title of the PACS server). Inside the function, an instance of AE is created, with the AE title set to _\"MODALITY\"_. The supported storage presentation contexts are set to _StoragePresentationContexts_, and a requested context for X-ray Angiographic Image Storage is added with the UID _\"1.2.840.10008.5.1.4.1.12.1\"_ and the transfer syntax _[JPEGBaseline]_. ```
1 defsend_dicom_to_pacs(dicom_folder,pacs_ip,pacs_port,pacs_ae_title)\(\leftarrow\) :
2#CreateanApplicationEntity
3 ae=AE(ae_title='MODALITY')
4 ae.supported_contexts=StoragePresentationContexts
5
6#AddtherequestedtransfersyntaxfortheX-rayAngiographic\(\leftrightarrow\)ImageStoragecontextae.add_requested_context\(\leftarrow\) ('1.2.840.10008.5.1.4.1.1.12.1', [JPEGBaseline])
```  
**Listing 2:** The main function. An association is established (Listing 3) with the PACS server using the associate method of the AE instance, binding the IP address of the PACS server, port number, and defined the AE title as arguments. Then, a message is printed indicating the successful connection. ```
1 assoc=ae.associate(pacs_ip,pacs_port,ae_title=pacs_ae_title)
2 ifassoc.is_established:
3 print('ConnectedtothePACServer.') ```  
Listing 3: Connecting to the PACS server. Inside the established association, the function, as presented in Listing 4, iterates over the files in the specified _dicom_folder_. If a file has the extension _.DCM_, then its full path is obtained using _os.path.join_. The DICOM file is read using _dcnread_ and stored in the dataset variable. The _send_c_store_ method of the association is called to send the DICOM dataset to the PACS server. The status of the storage request is checked, and appropriate messages are printed based on the success or failure of the operation. ```
1 forfilenameinos.listdir(dicom_folder):
2 iffilename.endswith('.DCM'):
3 dicom_file=os.path.join(dicom_folder,filename)
4#ReadtheDICOMfile
5 dataset=dcnread(dicom_file)
6#SendtheDICOMimagetothePACServer
7 status=assoc.send_c_store(dataset)
8#Checkthestatusofthestoragerequest
9 ifstatus:
10 print(f'Successfullysent{dicom_file}tothePACServer.') 11 else:
12 print(f'Failedtosend{dicom_file}tothePACServer.') ```  
Listing 4: Iterating over DICOM files in the folder. After processing all the DICOM files, the association is released (Listing 5) using the release method and a message is printed indicating the disconnection from the PACS server.