sábado, 14 de dezembro de 2013

Gerador de seriais - linguagem c

Quem nunca esqueceu uma senha, ou serial, que foi usado para criar um e-mail
login em sites, arquivos criptografados, senhas do windows, wifi, atire a primeira pedra.
Pensando nisto, resolvi criar este programa, que gera sempre os mesmos seriais, sem usar a famosa função rand();causando um grande conforto para o utilizador.
Tudo o que o usuário tem que fazer é digitar números muito simples, como por exemplo, número da casa, os quatro primeiros dígitos da data de nascimento, idade, etc, respeitando os limites do programa que ficou estabelecido em 10 000.
O programa rejeita números negativos, e qualquer caractere indesejado, além de bloquear números acima de 9999.

Breve resumo do funcionamento:

A função strcpy (); faz a cópia de cada parte do serial solicitado que está presente nas Matrizes de char para cada Vetor de char correspondente a solicitação, e a função strcat(); faz a junções destes seriais que já estão nos vetores de char, repassando para o vetor de ponteiro *ais, que foi obrigatoriamente passado como referência nas funções Gera_Seriais(); e Gera_Arquivos();, depois que me deu um bom trabalho, entendi que não funcionaria corretamente se não fosse assim.
Depois do Serial gerado, o usuário tem a opção através de um switch();case,
de imprimi-lo na tela e num arquivo .txt, más se não gostou, tem a opção de gerar outro serial, ou de até mesmo finalizar o programa.

Veja o vídeo de funcionamento do programa:


Veja o código abaixo:



#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>
void Janela5 ( ) {
     int lin, col;
     for ( lin = 0; lin <= 25 ; lin++ ) {
         for ( col = 0; col <= 80 ; col++ ) {
              gotoxy ( col , lin );
              if ( lin == 2 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( col == 1 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( lin == 25 ) {
                   textbackground ( LIGHTBLUE );
              }
              if ( col == 80 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
         }
     }
     textbackground ( BLACK );
}
int n = 0, un, de, ce, mi;
int main ( );
char num [ 3 ];
int Gerar_Seriais ( char num [ 3 ] ) {
     unsigned int i;
     for ( i = 0; i < strlen ( num ) ; i++ ) {
         if ( num [ i ] < '0' || num [ i ] > '9' )
              return 0;
     }
     return 1;
}
void Gera_Arquivos ( char *ais );
void Gera_Seriais ( char *ais ) {
     system ( "cls" );
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 29 , 3 );
     printf ( "PROGRAMA GERADOR DE SERIAIS" );
     textcolor ( YELLOW );
     gotoxy ( 24 , 5 );
     printf ( "Programa desenvolvido por:" );
     textcolor ( LIGHTCYAN );
     gotoxy ( 51 , 5 );
     printf ( "Samuel Lima" );
     textcolor ( LIGHTGREEN );
     gotoxy ( 34 , 7 );
     printf ( "sa_sp10@hotmail.com" );
     textcolor ( WHITE );
     gotoxy ( 25 , 9 );
     printf ( "Digite um Numero ou [ -1 ] Para sair: " );
     textcolor ( YELLOW );
     gets ( num );
     fflush ( stdin );
     n = atoi ( num );
     if ( n == 0 ) {
         textcolor ( LIGHTGREEN );
         gotoxy ( 25 , 11 );
         printf ( "\aCaracteres e zero nao sao aceitos !" );
         Sleep ( 1800 );
         textcolor ( LIGHTGREEN );
         gotoxy ( 25 , 13 );
         printf ( "\aVoce digitou: %s" , num );
         Sleep ( 1800 );
         textcolor ( LIGHTGREEN );
         gotoxy ( 25 , 15 );
         printf ( "\a%s Nao e Valido !" , num );
         Sleep ( 1800 );
         Gera_Seriais ( ais );
         system ( "cls" );
     }
     if ( n == -1 ) {
         textcolor ( LIGHTRED );
         gotoxy ( 33 , 20 );
         printf ( "O ROCCO AGRADECE" );
         Sleep ( 1800 );
         exit ( 0 );
     }
     if ( n < 0 ) {
         textcolor ( LIGHTGREEN );
         gotoxy ( 25 , 11 );
         printf ( "\aO Unico Negativo Valido e o -1" );
         Sleep ( 1800 );
     }
     if ( n > 9999 ) {
         textcolor ( LIGHTGREEN );
         gotoxy ( 22 , 11 );
         printf ( "\aSo sao aceitos Numeros menores que 10 000 " );
         getche ( );
         Gera_Seriais ( ais );
         system ( "cls" );
     }
     if ( Gerar_Seriais ( num ) == 0 ) {
         textcolor ( LIGHTGREEN );
         gotoxy ( 25 , 13 );
         printf ( "\aVoce digitou: %s" , num );
         Sleep ( 1800 );
         textcolor ( LIGHTGREEN );
         gotoxy ( 25 , 15 );
         printf ( "\a%s Nao e Valido !" , num );
         Sleep ( 1800 );
         Gera_Seriais ( ais );
         system ( "cls" );
     } else {
         mi = ( n / 1000 ); //separa milhares
         ce = ( ( n - ( mi * 1000 ) ) / 100 ); //separa centenas
         de = ( ( n - ( ( mi * 1000 ) + ( ce * 100 ) ) ) / 10 ); //separa dezenas
         un = ( n - ( ( mi * 1000 ) + ( ce * 100 ) + ( de * 10 ) ) ); //separa unidades
         textcolor ( YELLOW );
         gotoxy ( 35 , 11 );
         printf ( "Voce digitou: " );
         textcolor ( LIGHTRED );
         gotoxy ( 50 , 11 );
         printf ( "%d %d %d %d" , mi , ce , de , un );
         getche ( );
         int x = n;
         system ( "cls" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 30 , 3 );
         printf ( "PROGRAMA GERADOR DE SERIAIS" );
         int i = 1, j;
         char u_ge [ 5 ], d_ra [ 5 ], c_se [ 5 ], m_ri [ 5 ];
         char ri [ 9 ] [ 9 ] =
               { " NATXBG ", " TRABZS ", " RKPIVX ", " XURKXT ",
                   " NBMIKT ", " NEGVWC ", " XYVNAB ", " BFYEKJ ", " HFNWTQ " };
         char se [ 9 ] [ 9 ] =
              { " XCVBWE ", " DUZKVF ", " XCVRGV ", " EYARIM ",
                 " QUINRI ", " VFCEKT ", " QETSVP ", " TMCZPK ", " KCRLTP " };
         char ra [ 9 ] [ 9 ] =
              { " DZEBTG ", " QIGXRN ", " BNTFCL ", " QUARHN ",
                 " NCULZJ ", " SEYSQU ", " STETWK ", " WITQSL ", " GPNOKQ " };
         char ge [ 10 ] [ 9 ] =
              { " WQXYAH ", " IXJHPZ ", " OFGHOS ", " WKLFXJ ",
                 " YTRDIZ ", " QAZXKL ", " VBNHOV ", " SKYINM ", " PKFQUT ", " ZYTBFC " };

         textcolor ( LIGHTBLUE );
         gotoxy ( 30 , 5 );
         printf ( "Veja abaixo o Serial Gerado " );
         textcolor ( LIGHTRED );
         gotoxy ( 23 , 7 );
         strcpy ( ais , "" );
         strcpy ( u_ge , "" );
         strcpy ( d_ra , "" );
         strcpy ( c_se , "" );
         strcpy ( m_ri , "" );
         while ( x > 0 && x < 10000 ) {
              j = x % 10;
              x = x / 10;
              switch ( i ) {
                   case 1: {
                        if ( j - 1 >= 0 ) {
                            strcat ( u_ge , ge [ j - 1 ] );
                            i++;
                            break;
                        } else {
                            i++;
                            break;
                        }
                   }
                   case 2: {
                        if ( j - 1 == 0 ) {
                            strcat ( d_ra , ra [ 0 ] );
                            i++;
                            break;
                        } else {
                            if ( j - 1 > 0 ) {
                                 strcat ( d_ra , ra [ j - 1 ] );
                                 i++;
                                 break;
                            } else {
                                 i++;
                                 break;
                            }
                        }
                   }
                   case 3: {
                        if ( j - 1 == 0 ) {
                            strcat ( c_se , se [ 0 ] );
                            i++;
                            break;
                        } else {
                            if ( j - 1 > 0 ) {
                                 strcat ( c_se , se [ j - 1 ] );
                                 i++;
                                 break;
                            } else {
                                 i++;
                                 break;
                            }
                        }
                   }
                   case 4: {
                        if ( j - 1 == 0 ) {
                            strcat ( m_ri , ri [ 0 ] );
                            i++;
                            break;
                        } else {
                            strcat ( m_ri , ri [ j - 1 ] );
                            i++;
                            break;
                        }
                   }
              }
         }
         textcolor ( LIGHTRED );
         strcat ( ais , m_ri );
         strcat ( ais , c_se );
         strcat ( ais , d_ra );
         strcat ( ais , u_ge );
         puts ( ais );
         getche ( );
         textcolor ( LIGHTBLUE );
         gotoxy ( 10 , 9 );
         printf ( "Abaixo o Serial Gerado e Invertido " );
         textcolor ( LIGHTRED );
         gotoxy ( 23 , 11 );
         printf ( "%s" , strrev ( ais ) );
         getche ( );
         textcolor ( LIGHTBLUE );
         gotoxy ( 10 , 13 );
         printf ( "Veja abaixo o Serial Gerado em Minusculo " );
         textcolor ( LIGHTRED );
         gotoxy ( 10 , 15 );
         printf ( " %s" , strlwr ( ais ) );
         getche ( );
         textcolor ( LIGHTBLUE );
         gotoxy ( 10 , 17 );
         printf ( "Abaixo o Serial em Minusculo e Invertido " );
         textcolor ( LIGHTRED );
         gotoxy ( 10 , 19 );
         printf ( "%s" , strrev ( ais ) );
         getche ( );
         textcolor ( YELLOW );
         gotoxy ( 29 , 21 );
         printf ( "PRESSIONE QUALQUER TECLA" );
         getche ( );
         strupr ( ais );
         do {
              system ( "cls" );
              Janela5 ( );
              textcolor ( LIGHTRED );
              gotoxy ( 29 , 3 );
              printf ( "PROGRAMA GERADOR DE SERIAIS" );
              textcolor ( LIGHTBLUE );
              gotoxy ( 13 , 5 );
              printf (
                        "Deseja Imprimir o Serial em um Arquivo, ou quer gerar outro ?" );
              textcolor ( LIGHTGREEN );
              gotoxy ( 8 , 7 );
              printf (
                        "[ I ] Para Imprimir,[ G ] Para Gerar outro Serial,[ S ] Para sair    " );
              char q;
              textcolor ( LIGHTRED );
              q = ( toupper ( getche ( ) ) );
              fflush ( stdin );
              gotoxy ( 75 , 7 );
              switch ( q ) {
                   case 'I':
                        Gera_Arquivos ( ais );
                        break;
                   case 'G':
                        Gera_Seriais ( ais );
                        break;
                   case 'S':
                        textcolor ( LIGHTRED );
                        gotoxy ( 33 , 20 );
                        printf ( "O ROCCO AGRADECE" );
                        Sleep ( 1800 );
                        exit ( 0 );
                        break;
                   default:
                        textcolor ( YELLOW );
                        gotoxy ( 33 , 10 );
                        printf ( "\a Opcao invalida" );
                        getche ( );
                        gotoxy ( 33 , 10 );
                        printf ( "                  " );
                        break;
              }
         } while ( 1 );
     }
}
void Gera_Arquivos ( char *ais ) {
     FILE *arq;
     char str [ 2 ] [ 24 ] = { " Pre - Senha: ", "\n Serial Em Maiusculo: " };
     char str1 [ 1 ] [ 24 ] = { "\n Serial Em Minusculo: " };
     char str2 [ 1 ] [ 37 ] = { "\n Serial Em Maiusculo e Invertido: " };
     char str3 [ 1 ] [ 37 ] = { "\n Serial Em Minusculo e Invertido:" };
     char Linha [ 100 ];
     int result;
     arq = fopen ( "Serial.txt" , "wt" );
     if ( arq == NULL ) {
         printf ( "Problemas na criacao do arquivo\n" );
         return;
     }
     result = fprintf ( arq , str [ 0 ] );
     fprintf ( arq , num );
     fprintf ( arq , str [ 1 ] );
     fprintf ( arq , ais );
     fprintf ( arq , str1 [ 0 ] );
     fprintf ( arq , strlwr ( ais ) );
     fprintf ( arq , str2 [ 0 ] , strrev ( ais ) );
     fprintf ( arq , strupr ( ais ) );
     fprintf ( arq , str3 [ 0 ] , ais );
     fprintf ( arq , strlwr ( ais ) );
     if ( result == EOF )
         printf ( "Erro na Gravacao\n" );
     fclose ( arq );
     textcolor ( LIGHTBLUE );
     gotoxy ( 23 , 9 );
     printf ( "Abrindo o arquivo .txt contendo o Serial" );
     getche ( );
     system ( "start Serial.txt" );
     getche ( );
     textcolor ( LIGHTBLUE );
     gotoxy ( 19 , 11 );
     printf ( "Fechando agora o arquivo .txt contendo o Serial" );
     getche ( );
     system ( "taskkill.exe /IM notepad.exe" );
     gotoxy ( 1 , 11 );
     printf ( "%c" , 219 );
     clreol ( ); // Usando a funcao clreol (); que junto com gotoxy, apaga uma linha
     textcolor ( LIGHTBLUE );
     gotoxy ( 23 , 9 );
     printf ( "Abaixo, a leitura do Serial num arquivo .txt" );
     textcolor ( LIGHTRED );
     gotoxy ( 10 , 13 );
     arq = fopen ( "Serial.txt" , "rb" );
     while ( !feof ( arq ) ) {
         fgets ( Linha , 100 , arq );
         if ( result ) {
              printf ( "%s" , Linha );
         }
         gotoxy ( 10 , 14 );
         fgets ( Linha , 100 , arq );
         if ( result ) {
              printf ( "%s" , Linha );
         }
         gotoxy ( 10 , 15 );
         fgets ( Linha , 100 , arq );
         if ( result ) {
              printf ( "%s" , Linha );
         }
         gotoxy ( 10 , 16 );
         fgets ( Linha , 100 , arq );
         if ( result ) {
              printf ( "%s" , Linha );
         }
         gotoxy ( 10 , 17 );
         fgets ( Linha , 100 , arq );
         if ( result ) {
              printf ( "%s" , Linha );
         }
     }
     fclose ( arq );
     getche ( );
     Gera_Seriais ( ais );
}
int main ( ) {
     system ( "title PROGRAMA GERADOR DE SERIAIS" );
     char *ais [ 5 ];
     Gera_Seriais ( ais [ 5 ] );
     Gera_Arquivos ( ais [ 5 ] );
     getche ( );
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.