Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace cripto_final
- {
- class Program
- {
- static string Chave()
- {
- int Pos;
- string[] n = {"0","1","2","3","4","5","6","7","8","9"} , key;
- string Key =string.Empty;
- Random pos = new Random();
- //criar chave
- key = new string[20];
- for(int i = 0 ; i < 20 ; i++)
- {
- Pos = pos.Next(1,100);
- key[i] = n[Pos % n.Length];
- }
- for(int i = 0 ; i < 20 ; i++)
- {
- Key += key[i];
- }
- return Key;
- }
- static string Criptografar()
- { /*
- text - Recebe o texto
- doc - caminho onde vai ser procurado o documento
- chave - Key
- ndoc - caminho onde vai ser salvo o doc criptografado
- */
- string text = string.Empty, doc = string.Empty , chave = string.Empty , ndoc;
- int i = 0 , k = 0 , hash = 0 , pos = 0;
- int[] cript , cript_after;
- string[] vet_ascii = {" ","a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z","0","1","2","3","4","5","6","7","8","9",",",".",";",":","/","|","!","@","#","$","%","&","*","-","+","=","à","á","é","í","ó","ú","ã","ç","â","ê","ô"} , vet_text , newvet_text , cript_afters;
- // variavel recebendo a Key
- chave = Chave();
- /* Gerar Hash */
- Random rdn = new Random();
- hash = (rdn.Next(100,999));
- /*--*/
- /* Guardar posição */
- Random rdn2 = new Random();
- pos = (rdn2.Next(2,9));
- /*--*/
- /*Ler caminho do documento e atribuir a variavel text*/
- Console.WriteLine("Digite o caminho do documento a ser criptografado");
- doc = Console.ReadLine();
- try
- {
- using (StreamReader sr = new StreamReader(doc))
- {
- text = sr.ReadToEnd();
- }
- if(text.Length < ((pos*5) + k + 3))
- {
- System.Console.WriteLine("Não é possivel Ler o documento\nTexto muito curto");
- Environment.Exit(0);
- }
- else
- {
- System.Console.WriteLine("Leitura do documento feita com sucesso");
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("Documento não encontrado");
- Console.WriteLine(e);
- }
- /*---*/
- vet_text = new string[text.Length];
- newvet_text = new string[text.Length];
- cript = new int[text.Length];
- cript_after = new int[text.Length];
- cript_afters = new string[text.Length];
- /*Passar texto para dentro do vetor*/
- foreach(char c in text)
- {
- vet_text[i] = c.ToString();
- i++;
- }
- /*---*/
- /* Identificar cada posição da letra no texto dentro do vetor base */
- for (int j = 0; j < vet_text.Length; j++)
- {
- for(i = 0; i < vet_ascii.Length; i++)
- {
- if(vet_text[j] == vet_ascii[i])
- {
- cript[k] = i;
- k++;
- }
- }
- }
- /*---*/
- /* Criando um vetor com os valores dos numeros do texto criptografado */
- for ( i = 0 ; i < text.Length ; i++)
- {
- cript_after[i] = ((cript[i] * hash)+hash) ;
- }
- /*---*/
- for ( i = 0 ; i < cript_after.Length ; i++)
- {
- cript_afters[i] = cript_after[i].ToString("00000");
- }
- /* Gerando a letra criptografada */
- /*Escrever em um novo documento */
- System.Console.WriteLine("Escreva o caminho pra onde o novo documento sera criado: ");
- ndoc = Console.ReadLine();
- try
- {
- using (StreamWriter writer = new StreamWriter(ndoc))
- {
- writer.Write((pos*5).ToString()); //inserir posição onde estara a chave e o documento
- for (i = 0; i < pos; i++)
- {
- writer.Write(cript_afters[i]);
- }
- writer.Write(hash);
- writer.Write(chave);
- for (i = pos; i < cript_afters.Length; i++)
- {
- writer.Write(cript_afters[i]);
- }
- System.Console.WriteLine("Texto criptografado passado para o documento com sucesso");
- /* OBS - SAIDA NO DOCUMENTO
- "[pos]xxxxxxxx[hash][chave]xxxxxxxxxxxxxxxxxx"
- a posição de [hash][chave] sera definida por [pos]
- */
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("erro!!");
- Console.WriteLine(e);
- }
- return "\n-------------------------\nTexto Criptografado com sucesso!!!\n-------------------------";
- }
- static string Descriptografar()
- {
- string doc_d , text_d = string.Empty , k = String.Empty , key_usuario , aut , msg_d = string.Empty , Hash = string.Empty , Pos;
- string[] vet_d , vet_hk , vet_k;
- int[] vet_h ;
- int i = 0 , pos , j = 0;
- /* VARIAVEIS PARA DESCRIPTOGRAFAR */
- int[] pos_after ;
- int hash = 0;
- string[] vet_text_d , descrip;
- string[] vet_ascii = {" ","a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z","0","1","2","3","4","5","6","7","8","9",",",".",";",":","/","|","!","@","#","$","%","&","*","-","+","=","à","á","é","í","ó","ú","ã","ç","â","ê","ô"};
- /*---*/
- /* Leitura do Documento Criptografado */
- System.Console.WriteLine("Digite o caminho para o documento criptografado:");
- doc_d = Console.ReadLine();
- try
- {
- using (StreamReader sr = new StreamReader(doc_d))
- {
- text_d = sr.ReadToEnd();
- }
- System.Console.WriteLine("Leitura do documento feita com sucesso");
- }
- catch (Exception e)
- {
- Console.WriteLine("Documento não encontrado");
- Console.WriteLine(e);
- }
- /*---*/
- /* Tamanho dos vetores , d - texto total , hk - hash/key , h - hash , k - key*/
- vet_d = new string[text_d.Length];
- vet_hk = new string[23];
- vet_h = new int[3];
- vet_k = new string[20];
- /*---*/
- /*Passar cada caractere para dentro de um vetor*/
- foreach(char d in text_d)
- {
- vet_d[i] = d.ToString();
- i++;
- }
- /*---*/
- /* Pegar as 2 primeiras posições para definir qual é a posição dos elementos hk */
- Pos = vet_d[0]+vet_d[1];
- pos = int.Parse(Pos);
- /*---*/
- /*Colocar no vetor HK , a hash e a key */
- for(i = pos + 2 ; i < (pos + 2 + 23) ; i++)
- {
- vet_hk[j] = vet_d[i];
- j++;
- }
- /*---*/
- /* Pegar a hash(3 primeiros elementos de hk) e passar pra um vetor int e depois pra uma variavel*/
- for (j = 0; j < vet_h.Length; j++)
- {
- vet_h[j] = int.Parse(vet_hk[j]);
- }
- for(i = 0 ; i < vet_h.GetLength(0); i++)
- {
- Hash += vet_h[i]; /* Hash String */
- }
- hash = int.Parse(Hash); /* hash Int */
- /*---*/
- /* Fazer vetor com a Key e adicionar a uma variavel */
- i = 0;
- for (j = 3; j < vet_k.Length + 3; j++)
- {
- vet_k[i] =vet_hk[j];
- i++;
- }
- for (i = 0; i < vet_k.Length; i++)
- {
- k += vet_k[i];
- }
- /*---*/
- System.Console.WriteLine(k);
- /* AUTENTICAÇÃO DA KEY PELO USUARIO */
- System.Console.WriteLine("Digite a Key por favor");
- key_usuario = Console.ReadLine();
- if(key_usuario == k) /* TRUE */
- {
- /* Eliminar 2 primeiras posições */
- vet_d[0] = string.Empty;
- vet_d[1] = string.Empty;
- /*---*/
- /*Eliminar posições Hash/Key */
- for(i = pos + 2; i < (23 + pos + 2) ; i++)
- {
- vet_d[i] = string.Empty;
- }
- /*---*/
- /*Adicionar vetor somente com o texto criptografado a uma variavel string */
- for(i = 0 ; i < vet_d.Length; i++)
- {
- msg_d += vet_d[i];
- }
- /*---*/
- /* Vetores finais */
- vet_text_d = new string[msg_d.Length];/*Vetor com o texto criptografado para cada indice */
- pos_after = new int[msg_d.Length / 5];/*Vetor pra achar posição*/
- descrip = new string[msg_d.Length];/*Vetor com o texto descriptografado */
- /*---*/
- /*Vetor com o texto criptografado para cada indice*/
- i = 0;
- foreach(char t in msg_d)
- {
- vet_text_d[i] = t.ToString();
- i++;
- }
- /*---*/
- /*Vetor string Y pegando de 5 em 5 indices do vetor (vet_text_d) */
- /*Cada letra é formada por 5 numeros*/
- string[] y = new string[vet_text_d.Length / 5];
- int POS = 0 ;
- for(i = 0 ; i < vet_text_d.Length / 5 ; i++)
- {
- for(j = POS ; j < POS + 5 ; j++)
- {
- y[i] += vet_text_d[j];
- }
- POS = POS + 5;
- }
- /*---*/
- /* Vetor z recebe o vetor y e transforma pra int */
- int[] z = new int[y.Length];
- for(i = 0; i < z.Length ; i++)
- {
- z[i] = int.Parse(y[i]);
- }
- /*---*/
- /*Operação para achar a posição da letra original*/
- for ( i = 0 ; i < pos_after.Length ; i++)
- {
- pos_after[i] = (z[i] / hash) - 1;
- }
- /*---*/
- /*Adicionando cada letra original ao vetor */
- for (j = 0; j < pos_after.Length; j++)
- {
- for(i = 0; i < vet_ascii.Length; i++)
- {
- descrip[j] = vet_ascii[pos_after[j]];
- }
- }
- /*---*/
- /*Mostrando mensagem original*/
- for(i = 0; i < msg_d.Length; i++)
- {
- System.Console.Write(descrip[i]);
- }
- aut = "\nTexto Descriptografado com sucesso!!!!!!! ";
- }
- else /*Chave Errada*/
- {
- aut = "Chave de acesso incorreto!!\nVocê não esta autorizado a acessar esse documento!";
- }
- return aut;
- }
- static void Main(string[] args)
- {
- int cript;
- System.Console.WriteLine("(1) Criptografar\n(2) Descriptografar");
- cript = int.Parse(Console.ReadLine());
- switch(cript)
- {
- case 1:
- System.Console.WriteLine(Criptografar());
- break;
- case 2:
- System.Console.WriteLine(Descriptografar());
- break;
- default:
- System.Console.WriteLine("Opção invalida");
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment