-3
\$\begingroup\$

I wanted to improve a crypter from internet. I found it poor and modified it.

Do you like it?

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

//Simple C program to encrypt and decrypt a string

#include <stdio.h>

int main()
{
  int scnt;
  int scnt2;
   int i, x;
   char str[512];
   printf("\nCrypter ver0.1\n");
   printf("\nYou need enter or create two keys for security\nWrite they in safe place\nTHIS PROGRAM CONTAINS BUGS");
   printf("\nPlease enter a string:\t");
   gets(str);

   printf("\nPlease choose following options:\n");
   printf("1 = Encrypt the string.\n");
   printf("2 = Decrypt the string.\n");
   scanf("%d", &x);
   printf("Create or Provide Exist Key:\n");
   scanf("%d", &scnt);
   printf("Create or Provide Exist Second Key:\n");
   scanf("%d", &scnt2);



   //using switch case statements
   switch(x)
   {
   case 1:
      for(i = 0; (i < 512 && str[i] != '\0'); i++)
        str[i] = str[i] + scnt - scnt2; //the key for encryption is 3 that is added to ASCII value
        printf("\nYou Encrypted String! You now in safe");
      printf("\nEncrypted string: %s\n", str);
      break;

   case 2:
      for(i = 0; (i < 512 && str[i] != '\0'); i++)
        str[i] = str[i] - scnt + scnt2; //the key for encryption is 3 that is subtracted to ASCII value
        printf("\nYou Decrypted String!\n");
      printf("\nDecrypted string: %s\n", str);
      break;

   default:
      printf("\nYou entered wrong number Select from 1 to 2\n");
   }
   return 0;
}

yes I know it contains so many bugs but I tested it on a long string and it worked (bugs are: 1. spaces don't work 2. in some keys it halts 3. unstable) but it decrypts fully without mistakes (tested around 50+ keys).
I made it so hard to hack even if you have source of program because my crypter uses 2 keys what makes it more hard to hack!

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Welcome to the Code Review Community. We only review code that is working as expected, there are other sites that will help you debug your code. Please read Where can I get help? and How do I ask a good question?. \$\endgroup\$
    – pacmaninbw
    Commented Jun 25, 2023 at 13:29
  • \$\begingroup\$ First rule about software security mechanisms: Don't invent your own while you haven't invested thousands of hours in the field. \$\endgroup\$
    – greybeard
    Commented Jun 26, 2023 at 12:19
  • \$\begingroup\$ Increasing the first key by one and decreasing the second key by one yields the same encryption. Your actual key is scnt - scnt2, a single number. You have the user remember two numbers, but provide no better encryption than with a single number. This is typically called the "Cesar cipher", the simplest and oldest cipher in the books. Decoding the text with all possible keys and seeing which of the results makes sense is trivial. Do not rely on this cipher for anything. \$\endgroup\$ Commented Jun 26, 2023 at 15:14

1 Answer 1

3
\$\begingroup\$

Use modern C

gets() has been dropped from the C standard library since C11 (12 years) and was even then considered poor practice.

Consider alternatives like using fgets() for all user input.

Test not needed

i < 512 is useless. For that to be false, prior gets() would have overrun str[] and that is undefined behavior (UB).

// for(i = 0; (i < 512 && str[i] != '\0'); i++)
for(i = 0; str[i]; i++)

Simplify

Code does either + scnt - scnt2 or - scnt + scnt2. Consider forming the difference once.

int diff = scnt - scnt2;
...
str[i] += diff;
...
str[i] -= diff;

Use auto-formatting

Early null character

str[i] + scnt - scnt2 may result in 0 and then the later printf("\nEncrypted string: %s\n", str); will not print the entire string.

Overflow

As str[i] + scnt - scnt2 may overflow or easily result in a value outside the char range. Re-code to prevent overflow and nicely handle out-of-range results. Perhaps a limit on scnt, scnt2 like code did with x?

\$\endgroup\$
1
  • \$\begingroup\$ thank you for your answer i will use your tips to improve my crypter! \$\endgroup\$ Commented Jun 25, 2023 at 12:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.