ope.c( dont share)

PHOTO EMBED

Fri Jun 16 2023 04:24:30 GMT+0000 (Coordinated Universal Time)

Saved by @codehub #c++

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int ARRAY_SIZE = 100;

int *R; 

void generateRandomNumbers() {
    int i;
    R = (int *)malloc(ARRAY_SIZE * sizeof(int));
    if (R == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }

    srand(time(NULL));
    for (i = 0; i < ARRAY_SIZE; i++) {
        R[i] = i + 1;
    }

    for (i = ARRAY_SIZE - 1; i > 0; i--) {
        int j = rand() % (i + 1);
        int temp = R[i];
        R[i] = R[j];
        R[j] = temp;
    }
}

int encrypt(int p) {
    int i, c = 0;
    for (i = 0; i < p; i++) {
        c += R[i];
    }
    return c;
}

int decrypt(int c) {
    int i, sum = 0;
    for (i = 0; i < ARRAY_SIZE; i++) {
        sum += R[i];
        if (sum >= c) {
            return i + 1;
        }
    }
    return -1;
}

int main() {
    int plainValues[] = {92, 95, 22};
    int cipher[] = {0,0,0};

    generateRandomNumbers();

    printf("Encryption:\n");
    for (int i = 0; i < 3; i++) {
        cipher[i] = encrypt(plainValues[i]);
        printf("Plain: %d\tCipher: %d\n", plainValues[i], cipher[i]);
    }

    printf("\nDecryption:\n");
    for (int i = 0; i < 3; i++) {
        int plain = decrypt(plainValues[i]);
        printf("Cipher: %d\tPlain: %d\n",cipher[i] , plainValues[i]);
    }

    free(R); // Free the allocated memory

    return 0;
}
content_copyCOPY