Advertisement
EBobkunov

encryption

Mar 10th, 2023 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. union messageStorage {
  3.     unsigned char segments[sizeof(unsigned long long int)];
  4.     unsigned long long int number;
  5. };
  6. /*
  7.  * Encryption of an original message with replacing
  8.  * neighbour even and odd bytes and further decryption of it
  9.  */
  10. void encryption(union messageStorage ms) {
  11.     char temp;
  12.     // first time for encryption and the second time for decryption
  13.     for (int j = 0; j < 2; j++) {
  14.         for (int i = 0; i < sizeof(ms.number); i += 2) {
  15.             temp = ms.segments[i];
  16.             ms.segments[i] = ms.segments[i+1];
  17.             ms.segments[i+1] = temp;
  18.         }
  19.         if (j == 0) {
  20.             printf("Encrypted message: %lld \n", ms.number);
  21.         } else {
  22.             printf("Decrypted message: %lld \n", ms.number);
  23.         }
  24.     }
  25. }
  26. int main() {
  27.     char temp;
  28.     union messageStorage ms;
  29.     printf("size of t is %lu \n", sizeof(ms.number));
  30.     printf("size of p is %lu \n", sizeof(ms.segments));
  31.     printf("Enter the number: ");
  32.     scanf("%lld", &ms.number);
  33.     printf("Original message: %lld\n", ms.number);
  34.     encryption(ms);
  35.     return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement