ThiagoFialho

CriptOk

Nov 13th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.63 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace cripto_final
  5. {    
  6.     class Program
  7.     {          
  8.         static string Chave()
  9.         {
  10.             int Pos;
  11.             string[] n = {"0","1","2","3","4","5","6","7","8","9"} , key;
  12.             string Key =string.Empty;
  13.            
  14.             Random pos = new Random();          
  15.            
  16.             //criar chave
  17.             key = new string[20];
  18.             for(int i = 0 ; i < 20 ; i++)
  19.             {
  20.                 Pos = pos.Next(1,100);                    
  21.                 key[i] = n[Pos % n.Length];                
  22.             }
  23.  
  24.             for(int i = 0 ; i < 20 ; i++)
  25.             {
  26.                 Key += key[i];
  27.             }    
  28.             return Key;          
  29.         }
  30.  
  31.         static string Criptografar()
  32.         {   /*
  33.             text - Recebe o texto
  34.             doc - caminho onde vai ser procurado o documento
  35.             chave - Key
  36.             ndoc - caminho onde vai ser salvo o doc criptografado
  37.             */
  38.             string text = string.Empty, doc = string.Empty , chave = string.Empty , ndoc;
  39.             int i = 0 , k = 0 , hash = 0 , pos = 0;
  40.             int[] cript , cript_after;
  41.             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;            
  42.            
  43.             // variavel recebendo a Key
  44.             chave = Chave();                          
  45.  
  46.             /* Gerar Hash */
  47.             Random rdn = new Random();
  48.             hash = (rdn.Next(100,999));
  49.             /*--*/
  50.  
  51.             /* Guardar posição */
  52.             Random rdn2 = new Random();
  53.             pos = (rdn2.Next(2,9));
  54.             /*--*/                          
  55.  
  56.             /*Ler caminho do documento e atribuir a variavel text*/                    
  57.             Console.WriteLine("Digite o caminho do documento a ser criptografado");
  58.             doc = Console.ReadLine();
  59.  
  60.             try
  61.             {
  62.                 using (StreamReader sr = new StreamReader(doc))
  63.                 {
  64.                    text = sr.ReadToEnd();
  65.                 }
  66.                 if(text.Length < ((pos*5) + k + 3))
  67.                 {
  68.                     System.Console.WriteLine("Não é possivel Ler o documento\nTexto muito curto");    
  69.                     Environment.Exit(0);
  70.                 }
  71.                 else
  72.                 {
  73.                     System.Console.WriteLine("Leitura do documento feita com sucesso");
  74.                 }
  75.                
  76.             }
  77.             catch (Exception e)
  78.             {
  79.                 Console.WriteLine("Documento não encontrado");
  80.                 Console.WriteLine(e);
  81.             }
  82.             /*---*/
  83.  
  84.             vet_text = new string[text.Length];
  85.             newvet_text = new string[text.Length];
  86.             cript = new int[text.Length];
  87.             cript_after = new int[text.Length];
  88.             cript_afters = new string[text.Length];
  89.            
  90.             /*Passar texto para dentro do vetor*/
  91.             foreach(char c in text)
  92.             {
  93.                 vet_text[i] = c.ToString();                                              
  94.                 i++;
  95.             }
  96.             /*---*/
  97.  
  98.             /* Identificar cada posição da letra no texto dentro do vetor base */
  99.             for (int j = 0; j < vet_text.Length; j++)
  100.             {            
  101.                 for(i = 0; i < vet_ascii.Length; i++)
  102.                 {
  103.                     if(vet_text[j] == vet_ascii[i])
  104.                     {                    
  105.                         cript[k] = i;
  106.                         k++;
  107.                     }    
  108.                 }
  109.             }
  110.             /*---*/
  111.            
  112.             /* Criando um vetor com os valores dos numeros do texto criptografado */            
  113.             for ( i = 0 ;  i < text.Length ; i++)
  114.             {                
  115.                 cript_after[i] = ((cript[i] * hash)+hash) ;                
  116.             }
  117.             /*---*/
  118.  
  119.             for ( i = 0 ;  i < cript_after.Length ; i++)
  120.             {
  121.                 cript_afters[i] = cript_after[i].ToString("00000");
  122.             }
  123.             /* Gerando a letra criptografada */      
  124.  
  125.  
  126.             /*Escrever em um novo documento */            
  127.             System.Console.WriteLine("Escreva o caminho pra onde o novo documento sera criado: ");
  128.             ndoc = Console.ReadLine();
  129.             try
  130.             {
  131.                 using (StreamWriter writer = new StreamWriter(ndoc))
  132.                 {
  133.                     writer.Write((pos*5).ToString()); //inserir posição onde estara a chave e o documento
  134.                     for (i = 0; i < pos; i++)
  135.                     {                        
  136.                         writer.Write(cript_afters[i]);
  137.                     }
  138.                     writer.Write(hash);
  139.                     writer.Write(chave);
  140.                     for (i = pos; i < cript_afters.Length; i++)
  141.                     {
  142.                         writer.Write(cript_afters[i]);
  143.                     }                    
  144.                     System.Console.WriteLine("Texto criptografado passado para o documento com sucesso");
  145.                     /* OBS - SAIDA NO DOCUMENTO
  146.                     "[pos]xxxxxxxx[hash][chave]xxxxxxxxxxxxxxxxxx"
  147.                     a posição de [hash][chave] sera definida por [pos]
  148.                      */
  149.                 }
  150.             }
  151.             catch (Exception e)
  152.             {
  153.                 Console.WriteLine("erro!!");
  154.                 Console.WriteLine(e);
  155.             }  
  156.            
  157.             return "\n-------------------------\nTexto Criptografado com sucesso!!!\n-------------------------";
  158.         }
  159.         static string Descriptografar()
  160.         {          
  161.            
  162.             string doc_d , text_d = string.Empty , k = String.Empty , key_usuario , aut , msg_d = string.Empty , Hash = string.Empty , Pos;
  163.             string[] vet_d , vet_hk , vet_k;
  164.             int[] vet_h ;
  165.             int i = 0 , pos , j = 0;
  166.  
  167.             /* VARIAVEIS PARA DESCRIPTOGRAFAR */
  168.             int[] pos_after ;
  169.             int hash = 0;
  170.             string[] vet_text_d , descrip;
  171.             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",",",".",";",":","/","|","!","@","#","$","%","&","*","-","+","=","à","á","é","í","ó","ú","ã","ç","â","ê","ô"};
  172.             /*---*/
  173.  
  174.             /* Leitura do Documento Criptografado */
  175.             System.Console.WriteLine("Digite o caminho para o documento criptografado:");
  176.             doc_d = Console.ReadLine();                      
  177.            
  178.             try
  179.             {                
  180.                 using (StreamReader sr = new StreamReader(doc_d))
  181.                 {
  182.                    text_d = sr.ReadToEnd();
  183.                 }
  184.                 System.Console.WriteLine("Leitura do documento feita com sucesso");
  185.             }
  186.             catch (Exception e)
  187.             {
  188.                 Console.WriteLine("Documento não encontrado");
  189.                 Console.WriteLine(e);
  190.             }
  191.             /*---*/
  192.  
  193.             /* Tamanho dos vetores , d - texto total , hk - hash/key , h - hash , k - key*/
  194.             vet_d = new string[text_d.Length];
  195.             vet_hk = new string[23];
  196.             vet_h = new int[3];
  197.             vet_k = new string[20];
  198.             /*---*/
  199.  
  200.            
  201.             /*Passar cada caractere para dentro de um vetor*/
  202.             foreach(char d in text_d)
  203.             {
  204.                 vet_d[i] = d.ToString();
  205.                 i++;
  206.             }
  207.             /*---*/
  208.  
  209.             /* Pegar as 2 primeiras posições para definir qual é a posição dos elementos hk */
  210.             Pos = vet_d[0]+vet_d[1];
  211.             pos = int.Parse(Pos);
  212.             /*---*/            
  213.            
  214.             /*Colocar no vetor HK , a hash e a key */
  215.             for(i = pos + 2 ; i < (pos + 2 + 23) ; i++)
  216.             {
  217.                 vet_hk[j] = vet_d[i];
  218.                 j++;
  219.             }
  220.             /*---*/
  221.            
  222.             /* Pegar a hash(3 primeiros elementos de hk) e passar pra um vetor int e depois pra uma variavel*/
  223.             for (j = 0; j < vet_h.Length; j++)
  224.             {
  225.                 vet_h[j] = int.Parse(vet_hk[j]);
  226.             }          
  227.                        
  228.             for(i = 0 ; i < vet_h.GetLength(0); i++)
  229.             {
  230.                 Hash += vet_h[i]; /* Hash String */
  231.             }
  232.             hash = int.Parse(Hash); /* hash Int */
  233.             /*---*/
  234.  
  235.             /* Fazer vetor com a Key e adicionar a uma variavel */            
  236.             i = 0;
  237.             for (j = 3; j < vet_k.Length + 3; j++)
  238.             {
  239.                 vet_k[i] =vet_hk[j];
  240.                 i++;
  241.             }          
  242.  
  243.             for (i = 0; i < vet_k.Length; i++)
  244.             {
  245.                 k += vet_k[i];
  246.             }
  247.             /*---*/
  248.  
  249.             System.Console.WriteLine(k);    
  250.             /* AUTENTICAÇÃO DA KEY PELO USUARIO */
  251.             System.Console.WriteLine("Digite a Key por favor");
  252.             key_usuario = Console.ReadLine();            
  253.  
  254.  
  255.             if(key_usuario == k) /* TRUE */
  256.             {
  257.                 /* Eliminar 2 primeiras posições */    
  258.                 vet_d[0] = string.Empty;
  259.                 vet_d[1] = string.Empty;
  260.                 /*---*/
  261.  
  262.                 /*Eliminar posições Hash/Key */
  263.                 for(i = pos + 2; i < (23 + pos + 2) ; i++)
  264.                 {
  265.                     vet_d[i] = string.Empty;
  266.                 }
  267.                 /*---*/
  268.  
  269.                 /*Adicionar vetor somente com o texto criptografado a uma variavel string */
  270.                 for(i = 0 ; i < vet_d.Length; i++)
  271.                 {
  272.                     msg_d += vet_d[i];
  273.                 }
  274.                 /*---*/
  275.                
  276.                 /* Vetores finais */
  277.                 vet_text_d = new string[msg_d.Length];/*Vetor com o texto criptografado para cada indice */            
  278.                 pos_after = new int[msg_d.Length / 5];/*Vetor pra achar posição*/
  279.                 descrip = new string[msg_d.Length];/*Vetor com o texto descriptografado */
  280.                 /*---*/
  281.  
  282.                 /*Vetor com o texto criptografado para cada indice*/
  283.                 i = 0;
  284.                 foreach(char t in msg_d)
  285.                 {
  286.                     vet_text_d[i] = t.ToString();
  287.                     i++;
  288.                 }
  289.                 /*---*/                
  290.  
  291.                 /*Vetor string Y pegando de 5 em 5 indices do vetor (vet_text_d) */
  292.                 /*Cada letra é formada por 5 numeros*/
  293.                 string[] y = new string[vet_text_d.Length / 5];
  294.                 int POS = 0 ;
  295.                
  296.                 for(i = 0 ; i < vet_text_d.Length / 5 ; i++)
  297.                 {                    
  298.                     for(j = POS ; j < POS + 5 ; j++)
  299.                     {
  300.                         y[i] += vet_text_d[j];
  301.                        
  302.                     }
  303.                     POS = POS + 5;
  304.                 }
  305.                 /*---*/              
  306.  
  307.                 /* Vetor z recebe o vetor y e transforma pra int */
  308.                 int[] z = new int[y.Length];
  309.                 for(i = 0; i < z.Length ; i++)
  310.                 {
  311.                     z[i] = int.Parse(y[i]);
  312.                 }
  313.                 /*---*/
  314.  
  315.                 /*Operação para achar a posição da letra original*/
  316.                 for ( i = 0 ;  i < pos_after.Length ; i++)
  317.                 {
  318.                     pos_after[i] = (z[i] / hash) - 1;
  319.                 }
  320.                 /*---*/                                  
  321.                
  322.                 /*Adicionando cada letra original ao vetor */
  323.                 for (j = 0; j < pos_after.Length; j++)
  324.                 {            
  325.                     for(i = 0; i < vet_ascii.Length; i++)
  326.                     {
  327.                         descrip[j] = vet_ascii[pos_after[j]];
  328.                     }
  329.                 }
  330.                 /*---*/
  331.  
  332.                 /*Mostrando mensagem original*/
  333.                 for(i = 0; i < msg_d.Length; i++)
  334.                 {
  335.                     System.Console.Write(descrip[i]);
  336.                 }            
  337.                 aut = "\nTexto Descriptografado com sucesso!!!!!!! ";
  338.             }
  339.             else /*Chave Errada*/
  340.             {
  341.                 aut = "Chave de acesso incorreto!!\nVocê não esta autorizado a acessar esse documento!";
  342.             }
  343.             return aut;
  344.         }
  345.  
  346.         static void Main(string[] args)
  347.         {                    
  348.             int cript;        
  349.             System.Console.WriteLine("(1) Criptografar\n(2) Descriptografar");
  350.             cript = int.Parse(Console.ReadLine());
  351.            
  352.             switch(cript)
  353.             {
  354.                 case 1:
  355.                     System.Console.WriteLine(Criptografar());
  356.                 break;
  357.                 case 2:
  358.                     System.Console.WriteLine(Descriptografar());
  359.                 break;
  360.                 default:
  361.                     System.Console.WriteLine("Opção invalida");
  362.                 break;
  363.             }
  364.         }
  365.     }
  366. }
Add Comment
Please, Sign In to add comment