encryption - Substitution cipher giving me the same text in C program -


over last few weeks i've been meddling encryption in c. i've been using simple substitution cipher i've encountered problems following code. though program runs smoothly, contents of text file "message" change same piece of text : c=Øžû†. hoping change every character of string in file random letter.

#define _crt_secure_no_warnings #include  <stdio.h> #include  <stdlib.h> #include  <windows.h> #include  <type.h> #include <string.h>  const int maxsize = 50;  void encrypt(file *file, char file[maxsize], int i, int j) {     file = fopen("message.txt", "r+");     (i = 0; < 6; i++)     {         file[i] = rand() + 26;         fputc(file[i], file);     }      printf("%s", file);      fclose(file);     return; }  int main() {     int = 0;     int j = 0;     char file[maxsize];     file *file = 0;      encrypt(file, file, i, j);      system("pause");     return 0; } 

there quite few problem code shared, are:

  1. how decrypt encrypted text if using rand()/srand() functions.
  2. file[i] = rand() + 26; here dumping character value , dumping file, doesn't make sense, need encrypt text.

i attaching simple sample code ceaser cipher[type of substitution ciper] reference. output follow:

testtest 123123 testtest
tbwxatzd 164904 aowptbwx
testtest 123123 testtest

#include <stdio.h> #include <string.h>  static char key[] = "helloworld";  int encrypt(char *atext,char *akey) {     int ltextlen    = strlen(atext);     int lkeylen     = strlen(akey);      int lcount,lshift;     int lcount1 = 0;      for(lcount = 0; lcount < ltextlen;lcount++)     {         if(atext[lcount] >= 'a' && atext[lcount] <= 'z')         {             lshift = akey[lcount1] % 26;             atext[lcount] = ((atext[lcount] + lshift - 97) % 26) + 97;         }         else if(atext[lcount] >= 'a' && atext[lcount] <= 'z')         {             lshift = akey[lcount1] % 26;             atext[lcount] = ((atext[lcount] + lshift - 65) % 26) + 65;         }         else if(atext[lcount] >= '0' && atext[lcount] <= '9')         {             lshift = akey[lcount1] % 10;             atext[lcount] = ((atext[lcount] + lshift - 48) % 10) + 48;         }         else         {         }         lcount1 = (lcount1 + 1) % lkeylen;     } }  int decrypt(char *atext,char *akey) {     int ltextlen    = strlen(atext);     int lkeylen     = strlen(akey);      int lcount,lshift;     int lcount1 = 0;      for(lcount = 0; lcount < ltextlen;lcount++)     {         if(atext[lcount] >= 'a' && atext[lcount] <= 'z')         {             lshift = 26 - (akey[lcount1] % 26);             atext[lcount] = ((atext[lcount] + lshift - 97) % 26) + 97;         }         else if(atext[lcount] >= 'a' && atext[lcount] <= 'z')         {             lshift = 26 - (akey[lcount1] % 26);             atext[lcount] = ((atext[lcount] + lshift - 65) % 26) + 65;         }         else if(atext[lcount] >= '0' && atext[lcount] <= '9')         {             lshift = 10 - (akey[lcount1] % 10);             atext[lcount] = ((atext[lcount] + lshift - 48) % 10) + 48;         }         else         {         }         lcount1 = (lcount1 + 1) % lkeylen;     } } int main() {     char plaintext[] = "testtest 123123 testtest";     printf("%s\n",plaintext);     encrypt(plaintext,key);     printf("%s\n",plaintext);     decrypt(plaintext,key);     printf("%s\n",plaintext);     return 0; } 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -