Virtual UART device in linux

I’m trying to implement a virtual UART using socat command for send and receive a string that is encrypted using XOR cipher on sending and decrpyted while receiving using c program.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

#define BUFFER_SIZE 256
#define UART_DEVICE_TX "./ttyV0"  // Transmit end
#define UART_DEVICE_RX "./ttyV1"  // Receive end

// XOR cipher for encryption and decryption
void xor_cipher(char *data, size_t length, char key) {
    for (size_t i = 0; i < length; ++i) {
        data[i] ^= key;
    }
}

// Configure the UART device
int configure_uart(const char *device) {
    int uart_fd = open(device, O_RDWR | O_NOCTTY);
    if (uart_fd < 0) {
        perror("Failed to open UART device");
        return -1;
    }

    struct termios options;
    tcgetattr(uart_fd, &options);

    // Set baud rate to 9600, 8 data bits, no parity, 1 stop bit
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    options.c_cflag = (options.c_cflag & ~CSIZE) | CS8;  // 8 data bits
    options.c_cflag |= (CLOCAL | CREAD);                 // Enable receiver
    options.c_cflag &= ~(PARENB | CSTOPB);               // No parity, 1 stop bit
    options.c_iflag &= ~(IXON | IXOFF | IXANY);          // Disable flow control
    options.c_lflag = 0;                                 // Raw input mode
    options.c_oflag = 0;                                 // Raw output mode

    tcsetattr(uart_fd, TCSANOW, &options);
    return uart_fd;
}

int main() {
    const char key = 0x5A;          // XOR encryption key
    char original_message[] = "Hello, UART!";  // Original message
    char encrypted_message[BUFFER_SIZE];
    char received_message[BUFFER_SIZE];

    printf("Original message: %sn", original_message);

    // Copy the original message and encrypt it
    strcpy(encrypted_message, original_message);
    xor_cipher(encrypted_message, strlen(encrypted_message), key);

    // Configure UART devices
    int uart_fd_tx = configure_uart(UART_DEVICE_TX);  // Transmit end
    int uart_fd_rx = configure_uart(UART_DEVICE_RX);  // Receive end
    if (uart_fd_tx < 0 || uart_fd_rx < 0) {
        return EXIT_FAILURE;
    }

    // Send the encrypted message to the TX port (master)
    write(uart_fd_tx, encrypted_message, strlen(encrypted_message));
    printf("Encrypted message sent: %sn", encrypted_message);

    // Receive the encrypted message from the RX port (slave)
    memset(received_message, 0, BUFFER_SIZE);
    int bytes_read = read(uart_fd_rx, received_message, BUFFER_SIZE - 1);
    if (bytes_read < 0) {
        perror("Failed to read from UART device");
        close(uart_fd_tx);
        close(uart_fd_rx);
        return EXIT_FAILURE;
    }

    // Decrypt the received message
    xor_cipher(received_message, bytes_read, key);
    printf("Decrypted message: %sn", received_message);

    // Check if the original and decrypted messages match
    if (strcmp(original_message, received_message) == 0) {
        printf("Decryption successful: Messages match.n");
    } else {
        printf("Decryption failed: Messages do not match.n");
    }

    // Clean up
    close(uart_fd_tx);
    close(uart_fd_rx);
    return EXIT_SUCCESS;
}

for creating virtual uart driver in the working directory i used this command:
socat -d -d PTY,link=./ttyV0,raw,echo=0 PTY,link=./ttyV1,raw,echo=0

after compilation and running the c file the encryption parting running and iam getting the encrypted data on tty1(cat ./ttyV1)

but the decryption part is not working.. i need help to figure this out.

when i tried without looking on the devices its working fine

Original message: Hello, UART!

Encrypted message sent: ?665vz

Encrypted message received: ?665vz

Decrypted message: Hello, UART!

Decryption successful: Messages match.

when i start cat ./ttyV1 in one terminal and run the program it will display like this

Original message: Hello, UART!

Encrypted message sent: ?665vz

not doing the decryption part

but the key can be seen in the other terminal that runs cat ./ttyV1

why this happening?

New contributor

cyberdude is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật