Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  onePASSWORD
Language: C/C++
Code:
#include <stdio.h>
#include <strings.h>
char inbuf[300];
char list[30][2][20];               // Global variables
char buffer[20];
char shortbuf[3];
char key[21] = "JZLw8c5g;GQ,Ya8+FUf";
int choice, col, line, cha, bli, bch, keycount, bufclean, incount;
// 
void fileread()                    // Read and decrypt file
{
     FILE *f;
     f = fopen("password.txt", "r+");
     fgets(inbuf, sizeof(inbuf), f);
incount = 0;
     keycount = 0;
     for (line = 0; line < 30; line++)
          for (col = 0; col < 2; col++)
               for (cha = 0; cha < 20; cha++)
               {
list[line][col][cha] = inbuf[incount];
               incount++;                    
if (list[line][col][cha] == '0')
                         list[line][col][cha] = 0;
                    list[line][col][cha] =
                         (list[line][col][cha] ^ key[keycount]);
                    keycount++;
                    if (keycount > 19)
                         keycount = 0;
               }
     fclose(f);
}

void filewrite()                    // Encrypt and write file
{
     FILE *f;
     f = fopen("password.txt", "r+");
incount = 0;
     keycount = 0;
     for (line = 0; line < 30; line++)
          for (col = 0; col < 2; col++)
               for (cha = 0; cha < 20; cha++)
               {
                    list[line][col][cha] =
                         (list[line][col][cha] ^ key[keycount]);
                    keycount++;
                    if (keycount > 19)
                         keycount = 0;
                    if (list[line][col][cha] == 0)
                         list[line][col][cha] = '0';
inbuf[incount] = list[line][col][cha];
incount++;
               }
     fprintf(f, inbuf);
     fclose(f);
}

void shortclean()                    // Clean shortbuf
{
     for (bufclean = 0; bufclean < 3; bufclean++)
          shortbuf[bufclean] = 0;
}

void clean()                         // Clean buffer
{
     for (bch = 0; bch < 20; bch++)
          buffer[bch] = 0;
}

void write()                         // Write from buffer
{
     for (bch = 0; bch < 20; bch++)
          list[line][col][bch] = buffer[bch];
}

void read()                              // Read to buffer
{
     for (bch = 0; bch < 20; bch++)
          buffer[bch] = list[line][col][bch];
}

void password()                         // Function start
{
     if (choice == 1)               // Print password list
     {
          printf("\\n[1] Password list:\\n");
          line = 0;
          do
          {
               if (list[line][0][0] == 0)     // Empty list
               {
                    if (line == 0)
                         printf("\\nYour password list is empty");
                    break;
               }
               printf("\\n%d\\t", line + 1);
               for (col = 0; col < 2; col++)
               {
                    for (cha = 0; cha < 20; cha++)
                         printf("%c", list[line][col][cha]);     // Print char
                    printf("\\t");
                    if (strlen(list[line][col]) < 8)
                         printf("\\t");          // Adjust table width
                    if (strlen(list[line][col]) < 16)
                         printf("\\t");
               }
               line++;
          }
          while (line < 30);
          printf("\\n");
     }
     if (choice == 2)               // Scan new Password
     {
          printf("\\n[2] Add Password\\n");
          for (line = 0; line < 30; line++)     // Find line
               if (list[line][0][0] == 0)
                    break;
          if (line < 29)
          {
               printf("\\nChoose the account:\\t");
               clean();               // Clean buffer
               scanf("%s", &buffer);     // Input to buffer
               col = 0;
               write();               // Write from buffer
               printf("Choose the password:\\t");
               clean();               // Clean buffer
               scanf("%s", &buffer);     // Input to buffer
               col++;
               write();               // Write from buffer
               printf("Your Password has been saved.\\n");
          }
          else
               printf
                    ("\\nYour password list is full.\\nPlease delete a password first.\\n");
     }
     if (choice == 3)               // Delete Password
     {
          printf("\\n[3] Delete Passwordn");
          printf("\\nChoose the line you want to delete: ");
          scanf("%s", &shortbuf);     // Input
          line = (shortbuf[0] - 48);     // Char to integer
          shortclean();               // Clean shortbuf
          if (line > 0 && line < 30)
          {
               for (col = 0; col < 2; col++)
                    for (cha = 0; cha < 20; cha++)
                         list[line - 1][col][cha] = 0;
               printf("The chosen line has been deleted.\\n");     // Clean line
               for (; line < 29; line++)
                    for (col = 0; col < 2; col++)
                    {
                         clean();     // Clean buffer
                         read();          // Read to buffer
                         line--;
                         write();     // Write from buffer
                         line++;
                    }
          }
          else
               printf("\\n!!  Your choice is invalid.\\n!!  Please try again.\\n");
     }
     if (choice == 4)
     {
          int request = 0;
          printf
               ("\\n[4] Clean list.\\n\\nAre you sure you want to delete all passwords? (y/n)\\n\\n--> Your Choice: ");
          scanf("%s", &shortbuf);     // Input
          request = shortbuf[0];     // Char to integer
          shortclean();               // Clean shortbuf
          if (request == 121)          // ASCII 121 = 'y'
          {
               for (line = 0; line < 30; line++)
                    for (col = 0; col < 2; col++)
                    {
                         clean();     // Clean buffer
                         write();     // Write from buffer
                    }
               printf("\\nAll passwords deleted.\\n");
          }
          else
               printf("\\nAction cancelled.\\n");
     }
     if (choice == 5)
     {
          printf("\\n[5] Sort the list\\n");
          int len, fir, sec, o, newline;
          for (len = 0; len < 30; len++)     // List length
          {
               if (list[len][0][0] == 0)
                    break;
          }
          for (fir = 0; fir < len; fir++)
               for (sec = fir + 1; sec < len; sec++)
               {                    // Compare case insensitiv
                    if (strcasecmp(list[fir][0], list[sec][0]) > 0)
                    {
                         printf(".");     // Show processing
                         newline++;
                         if (newline == 16)
                              printf("\\n");
                         for (col = 0; col < 2; col++)     // Exchange
                         {
                              clean();     // Clean
                              line = fir;
                              read();     // Read
                              for (bch = 0; bch < 20; bch++)
                                   list[fir][col][bch] = list[sec][col][bch];     // Copy
                              line = sec;
                              write();     // Write
                         }
                    }
               }
          printf("done\\n");
     }
}

void menu()                              // Function start
{
     int exit = 0;
     shortclean();     // Clean shortbuf
     do
     {
          printf
               ("\\v[1] Password list\\n[2] Add passwordn[3] Delete Password\\n[4] Clean the list\\n[5] Sort the listn[6] Exit & Save\\n\\n--> Your choice: ");
          scanf("%s", &shortbuf);     // Input
          choice = (shortbuf[0] - 48);     // Char to integer
          shortclean();     // Clean shortbuf
          if (choice > 0 && choice < 6)
               password();               // Password function
          if (choice == 6)          // Exit
               exit = 1;
          if (choice < 1 || choice > 6)
               printf("\\n!!  Your choice is invalid.\\n!!  Please try again.\\n");
     }
     while (exit != 1);
     printf("\\nThank you for using onePASSWORD.\\nSee you next time!");
}

int main()
{
     char input[16];
     char password[16] = "leet1337";
     int pwcount;
     printf("Welcome to onePASSWORD.\\nPlease enter your Password.\\n ");
     for (pwcount = 3; pwcount > 0; pwcount--)
     {
          printf("\\nPassword: ");
          scanf("%s", &input);     // Password input
          if (strcmp(input, password) == 0)
          {
               fileread();               // Decrypt and read File
               menu();                    // Menu function
               filewrite();          // Encrypt and write File
               pwcount = 0;
          }
          else
          {
               if (pwcount == 1)
                    printf("\\nYou are stupid. Please leave this program.");
               else
                    printf
                         ("\\nYou 've entered an invalid password.\\nPlease try again (%d)\\n",
                          pwcount - 1);
          }
     }
}
          
          
          
          
          
          
          
          
          
          
Comments: