Sunday, 22 October 2017

A Strong Random Password Generator Using C/C++

random password generator is software program or hardware device that takes input from a random or pseudo-random number generator and automatically generates a password. Random passwords can be generated manually, using simple sources of randomness such as dice or coins, or they can be generated using a computer software program.

Here we have a simple program by using it you can generate a strong password of any predefined length. As you increase the length of password, it becomes stronger.  

//Source Code:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
     int counter = 0;
     srand(time(NULL));  // seeding function for random()
     char randChar;

     int  passwordLength;
           
printf("****A Random Password Generator****\n\n");
     printf("Type password Length: ");
     scanf("%d", &passwordLength);

     printf("\n\n");
     while (counter < passwordLength)
     {
           //Get a random char among 70 characters
           randChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@#$%^&*!"[rand() % 70];
           printf("%c", randChar);
           counter++;
     }
    
     printf("\n");
     _getch();
     return 0;
}

Output:


Friday, 13 October 2017

Displaying star (*) for input password using c

Here we demonstrate how can we show the star (*) for an input password. It may be used when we are developing an application that has a login form in which we use a user name and password as logging information.

For security purpose it is needed to show (*) instead showing the plain text for the password. The source code is given below to show how we can do it using c/c++.

Source Code:
#include <stdio.h>
#include <conio.h>

int main()
{
     char password[20], ch = 0;
     int i = 0, j;

     printf("Enter the password <limit 20 characters>: ");

     while (i < 20)
     {
           ch = _getch();
           if (ch == `\r`)
                break;
           password[i] = ch;
           i++;
           ch = `*`;
           printf("%c", ch);
     }

     password[i] = `\0`;

     printf("\nThe password is :");

     for (j = 0; j < i; j++)
     {
           printf("%c", password[j]);
     }
     _getch();
     return 0;
}

Output:


Sunday, 8 October 2017

String Manipulation Using C

String manipulation is the action of the fundamental operations on strings, including their creation, concatenation, the extraction of string segments, string matching, their comparison, discovering their length, replacing sub-strings by other strings. Here we demonstrate some example of string manipulation.

Source code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

/* string length */
int StrLength(char *string)
{
       char *count = string;
       while (*count)
       {
              count++;
       }
       return count - string;
}

/* string append */
char* StrAppend(char* string, char* append)
{
       char* newstring = NULL;
       size_t needed = _snprintf(NULL, 0, "%s%s", string, append);
       newstring = (char*)(malloc(needed));
       sprintf(newstring, "%s%s", string, append);
       return newstring;
}

/* string append char */
char* StrAppendChar(char* string, char append)
{
       char* newstring = NULL;
       size_t needed = _snprintf(NULL, 0, "%s%c", string, append);
       newstring = (char*)(malloc(needed));
       sprintf(newstring, "%s%c", string, append);
       return newstring;
}

/* string equals */
int StrEquals(char *equal1, char *eqaul2)
{
       while (*equal1 == *eqaul2)
       {
              if (*equal1 == `\0` || *eqaul2 == `\0`)
              {
                     break;
              }
              equal1++;
              eqaul2++;
       }
       if (*equal1 == `\0` && *eqaul2 == `\0`)
       {
              return 0;
       }
       else
       {
              return -1;
       }
}

/* string replace */
char* StrReplace(char* search, char* replace, char* subject)
{
       char* newstring = "";
       int i = 0;
       for (i = 0; i < StrLength(subject); i++)
       {
              if (subject[i] == search[0])
              {
                     int e = 0;
                     char* calc = "";
                     for (e = 0; e < StrLength(search); e++)
                     {
                           if (subject[i + e] == search[e])
                           {
                                  calc = StrAppendChar(calc, search[e]);
                           }
                     }
                     if (StrEquals(search, calc) == 0)
                     {
                           newstring = StrAppend(newstring, replace);
                           i = i + StrLength(search) - 1;
                     }
                     else
                     {
                           newstring = StrAppendChar(newstring, subject[i]);
                     }
              }
              else
              {
                     newstring = StrAppendChar(newstring, subject[i]);
              }
       }
       return newstring;
}

/* string replace maximal */
char* StrReplaceMax(char* search, char* replace, char* subject, int count)
{
       char* newstring = "";
       int i = 0;
       for (i = 0; i < StrLength(subject); i++)
       {
              if (subject[i] == search[0])
              {
                     int e = 0;
                     char* calc = "";
                     for (e = 0; e < StrLength(search); e++)
                     {
                           if (subject[i + e] == search[e])
                           {
                                  calc = StrAppendChar(calc, search[e]);
                           }
                     }
                     if (StrEquals(search, calc) == 0)
                     {
                           if (count > 0)
                           {
                                  newstring = StrAppend(newstring, replace);
                                  i = i + StrLength(search) - 1;
                                  count = count - 1;
                           }
                           else
                           {
                                  newstring = StrAppendChar(newstring, subject[i]);
                           }

                     }
                     else
                     {
                           newstring = StrAppendChar(newstring, subject[i]);
                     }
              }
              else
              {
                     newstring = StrAppendChar(newstring, subject[i]);
              }
       }
       return newstring;
}


int main()
{
       char* str = "this is a string example";
       int len_str = StrLength(str);
       char* appned_str = StrAppend(str, "append this str");
       char* append_char_str = StrAppendChar(str, `#`);
       char* replace_str = StrReplace("is", "was", str);
       int equal_str = StrEquals(str, replace_str);

       printf("***  String Manipulation Using C  ***\n\n");
       printf("\nExample String                 : %s", str);
       printf("\nString Length                  : %d", len_str);
       printf("\nAppended String                : %s", appned_str);
       printf("\nAppended character             : %s", append_char_str);
       printf("\nReplaced String                : %s", replace_str);
       printf("\nis equal replaced string       : %d", equal_str);

       getchar();

       return 0;
}

Output: