Advertisement
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 = {"a","A","b","B","c","C","0","1","2","3","4","5","6","7","8","9","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","0","1","2","3","4","5","6","7","8","9","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"} , key;
- string Key =string.Empty;
- Random pos = new Random();
- key = new string[20];
- for(int i = 0 ; i < 20 ; i++)
- {
- Pos = pos.Next(1,100);
- key[i] = n[Pos % 52];
- }
- for(int i = 0 ; i < 20 ; i++)
- {
- Key += key[i];
- }
- // System.Console.WriteLine("Tamanho da chave (aleatorio): " + Len);
- // System.Console.WriteLine("Pos (num aleatorio): " + Pos);
- // System.Console.WriteLine("Tamanho do vetor n: " + n.Length);
- // for(int i = 0 ; i < Len ; i++)
- // {
- // System.Console.WriteLine(key[i]);
- // }
- return Key;
- }
- static string Criptografar()
- {
- string text = string.Empty, doc = string.Empty , chave = string.Empty;
- 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",",",".",";",":","/","|","!","@","#","$","%","&","*","-","+","="};
- string[] vet_text , newvet_text;
- chave = Chave();
- //cripto/doc.txt
- /* Gerar Hash */
- Random rdn = new Random();
- hash = (rdn.Next(100,999));
- /* Guardar posição */
- Random rdn2 = new Random();
- pos = (rdn2.Next(1,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();
- }
- 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];
- /*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) % 78;
- }
- /*---*/
- /* Gerando a letra criptografada */
- for (int j = 0; j < vet_text.Length; j++)
- {
- for(i = 0; i < vet_ascii.Length; i++)
- {
- newvet_text[j] = vet_ascii[cript_after[j]];
- }
- }
- /*---*/
- /*Escrever em um novo documento */
- try
- {
- using (StreamWriter writer = new StreamWriter("cript/newdoc.txt"))
- {
- writer.Write(pos.ToString()); //inserir posição onde estara a chave e o documento
- for (i = 0; i < pos; i++)
- {
- writer.Write(newvet_text[i]);
- }
- writer.Write(hash);
- writer.Write(chave);
- for (i = pos; i < newvet_text.Length - pos; i++)
- {
- writer.Write(newvet_text[i]);
- }
- System.Console.WriteLine("Texto criptografado passado para o documento com sucesso");
- /* 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);
- }
- System.Console.WriteLine("hash: " + hash);
- System.Console.WriteLine("hash 2 :" + pos );
- System.Console.WriteLine("Chave: " + chave);
- /*
- /*
- Controle de saida dos textos
- System.Console.WriteLine();
- System.Console.WriteLine();
- System.Console.WriteLine("Controle de saida");
- System.Console.WriteLine("hash: " + hash);
- System.Console.WriteLine("hash 2 :" + pos );
- System.Console.WriteLine("Chave: " + chave);
- System.Console.WriteLine();
- Console.WriteLine("Texto a ser Criptografado: " + text);
- System.Console.WriteLine();
- */
- Console.Write("cript: " );
- for(i = 0; i < text.Length; i++)
- {
- Console.Write(cript[i] + " - ");
- }
- Console.WriteLine();
- Console.Write("cript_after: " );
- for(i = 0; i < text.Length; i++)
- {
- Console.Write(cript_after[i] + " - ");
- }
- Console.WriteLine();
- return "\n-------------------------\nTexto Criptografado com sucesso!!!\n-------------------------";
- }
- static string Descriptografar()
- {
- /*
- cript_before[i] = ((cript[i] * hash) + hash) % 78;
- tenho que achar cript[i]
- tenho hash
- TO DO LIST:
- - achar posição da letra criptografada antes;
- - fazer operação inversa
- - achar posição da letra correta
- */
- string doc_d , text_d = string.Empty , k = String.Empty , key_usuario , aut , msg_d = string.Empty , Hash = string.Empty ;
- string[] vet_d , vet_hk , vet_k;
- int[] vet_h ;
- int i = 0 , pos;
- /* VARIAVEIS PARA DESCRIPTOGRAFAR */
- int[] pos_before, pos_after ;
- int aux ,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",",",".",";",":","/","|","!","@","#","$","%","&","*","-","+","="};
- /*cript_after[i] = ((cript[i] * hash)+hash) % 78; */
- /* 159 */
- /* C i e n c i a */
- /* 6 17 9 27 5 17 1 Original*/
- /* 21 54 30 6 18 54 6 Criptografado*/
- /*1113 */
- System.Console.WriteLine("Digite o caminho para o documento criptografado:");
- doc_d = Console.ReadLine();
- System.Console.WriteLine();
- /* Leitura do Documento D */
- 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);
- }
- System.Console.WriteLine();
- vet_d = new string[text_d.Length];
- vet_hk = new string[23];
- vet_h = new int[3];
- vet_k = new string[20];
- foreach(char d in text_d)
- {
- vet_d[i] = d.ToString();
- i++;
- }
- System.Console.WriteLine("vet- d");
- for(i = 0 ; i < text_d.Length; i++)
- {
- System.Console.Write(vet_d[i] + " - ");
- }System.Console.WriteLine();
- pos = int.Parse(vet_d[0]);
- System.Console.WriteLine("pos: " + pos);
- int j = 0;
- for(i = pos + 1 ; i < (pos + 1 + 23) ; i++)
- {
- vet_hk[j] = vet_d[i];
- j++;
- }
- System.Console.WriteLine("vet-hk");
- for(i = 0 ; i < vet_hk.Length; i++)
- {
- System.Console.Write(vet_hk[i] + " - ");
- }System.Console.WriteLine();
- for (j = 0; j < vet_h.Length; j++)
- {
- vet_h[j] = int.Parse(vet_hk[j]);
- }
- System.Console.WriteLine("vet-h");
- for(i = 0 ; i < vet_h.Length; i++)
- {
- System.Console.Write(vet_h[i] + " - ");
- }System.Console.WriteLine();
- for(i = 0 ; i < vet_h.GetLength(0); i++)
- {
- Hash += vet_h[i];
- }
- System.Console.WriteLine("hash: "+ Hash);
- hash = int.Parse(Hash);
- i = 0;
- for (j = 3; j < vet_k.Length + 3; j++)
- {
- vet_k[i] =vet_hk[j];
- i++;
- }
- System.Console.WriteLine("vet-k");
- for(i = 0 ; i < vet_k.Length; i++)
- {
- System.Console.Write(vet_k[i] + " - ");
- }System.Console.WriteLine();
- for (i = 0; i < vet_k.Length; i++)
- {
- k += vet_k[i];
- }
- System.Console.WriteLine("key - " + k);
- System.Console.WriteLine("Digite a Key por favor");
- key_usuario = Console.ReadLine();
- System.Console.WriteLine();
- if(key_usuario == k)
- {
- vet_d[0] = string.Empty;
- for(i = pos + 1; i < (23 + pos + 1) ; i++)
- {
- vet_d[i] = string.Empty;
- }
- System.Console.WriteLine("Vet_d: ");
- for(i = 0 ; i < vet_d.Length; i++)
- {
- System.Console.Write(vet_d[i] + " ");
- }
- for(i = 0 ; i < vet_d.Length; i++)
- {
- msg_d += vet_d[i];
- }
- System.Console.WriteLine("msg_d = " + msg_d);
- /* colocar msg_d em um vetor */
- vet_text_d = new string[msg_d.Length];
- pos_before = new int[msg_d.Length];
- pos_after = new int[msg_d.Length];
- descrip = new string[msg_d.Length];
- i = 0;
- foreach(char t in msg_d)
- {
- vet_text_d[i] = t.ToString();
- i++;
- }
- aux = 0;
- System.Console.WriteLine("vet_text_d: ");
- for(i = 0 ; i < vet_text_d.Length; i++)
- {
- System.Console.Write(vet_text_d[i] + " ");
- }
- System.Console.WriteLine();
- System.Console.WriteLine("Tamanhos");
- System.Console.WriteLine(vet_d.Length);
- System.Console.WriteLine(vet_text_d.Length);
- System.Console.WriteLine(msg_d.Length);
- System.Console.WriteLine(pos_after.Length);
- System.Console.WriteLine(pos_before.Length);
- System.Console.WriteLine();
- /* OK */
- for (j = 0; j < vet_text_d.Length; j++)
- {
- for(i = 0; i < vet_ascii.Length; i++)
- {
- if(vet_text_d[j] == vet_ascii[i])
- {
- pos_before[aux] = i;
- aux++;
- }
- }
- }
- /*
- 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++;
- }
- }
- }
- */
- System.Console.WriteLine("pos before");
- for(i = 0 ; i < vet_text_d.Length; i++)
- {
- System.Console.Write(pos_before[i] + " ");
- }
- System.Console.WriteLine();
- /* FAZER */
- /*
- hash 880
- 6 17 9
- 76 6 64
- 12 32 60
- 64
- */
- for ( i = 0 ; i < pos_before.Length ; i++)
- {
- pos_after[i] = (pos_before[i] - (hash % 78))%78;
- } //((cript[i] * hash)-hash) % 78;
- System.Console.WriteLine();
- System.Console.WriteLine(hash % 78);
- System.Console.WriteLine();
- System.Console.WriteLine("pos_after:");
- for(i = 0; i < pos_after.Length;i++)
- {
- System.Console.Write(pos_after[i] + " ");
- }
- System.Console.WriteLine();
- for (j = 0; j < pos_after.Length; j++)
- {
- for(i = 0; i < vet_ascii.Length; i++)
- {
- descrip[j] = vet_ascii[pos_after[j]];
- }
- }
- for(i = 0; i < msg_d.Length; i++)
- {
- System.Console.Write(descrip[i]);
- }
- System.Console.WriteLine();
- aut = "Texto Descriptografado com sucesso!!!!!!! ";
- }
- else
- {
- aut = "Chave de acesso incorreto!!\nVocê não esta autorizado a acessar esse documento!";
- }
- // for(i = 0; i < vet_h.Length; i++ )
- // {
- // System.Console.Write(vet_h[i]);
- // }
- // System.Console.WriteLine();
- // for(i = 0; i < vet_k.Length; i++ )
- // {
- // System.Console.Write(vet_k[i]);
- // }
- // 6ooooooxxxxxxxxxxxxxxxxxxxxxxxooooooooooooo
- // 0123456789012345678901234567890123456789012
- // 111111111122222222223333333333444
- 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;
- }
- /* hash: 295
- hash 2 :6
- Chave: N96cDMdcghek99g4a4ge */
- /* DESCRIPTOGRAFAR */
- // bool continuar = false;
- // System.Console.WriteLine("Deseja Descriptografar?");
- // if (continuar)
- // {
- // }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement