Advertisement
ThiagoFialho

Cy

Nov 12th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.21 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 = {"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;
  12.             string Key =string.Empty;
  13.            
  14.             Random pos = new Random();          
  15.            
  16.             key = new string[20];
  17.             for(int i = 0 ; i < 20 ; i++)
  18.             {
  19.                 Pos = pos.Next(1,100);
  20.                    
  21.                 key[i] = n[Pos % 52];                
  22.             }
  23.             for(int i = 0 ; i < 20 ; i++)
  24.             {
  25.                 Key += key[i];
  26.             }            
  27.  
  28.             // System.Console.WriteLine("Tamanho da chave (aleatorio): " + Len);
  29.             // System.Console.WriteLine("Pos (num aleatorio): " + Pos);
  30.             // System.Console.WriteLine("Tamanho do vetor n: " + n.Length);
  31.             // for(int i = 0 ; i < Len ; i++)
  32.             // {
  33.             //     System.Console.WriteLine(key[i]);
  34.             // }
  35.                        
  36.             return Key;          
  37.         }
  38.  
  39.         static string Criptografar()
  40.         {
  41.             string text = string.Empty, doc = string.Empty , chave = string.Empty;
  42.             int i = 0 , k = 0 , hash = 0 , pos = 0;
  43.             int[] cript , cript_after;
  44.             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",",",".",";",":","/","|","!","@","#","$","%","&","*","-","+","="};
  45.             string[] vet_text , newvet_text;
  46.            
  47.             chave = Chave();                      
  48.        
  49.             //cripto/doc.txt
  50.  
  51.             /* Gerar Hash */
  52.             Random rdn = new Random();
  53.             hash = (rdn.Next(100,999));
  54.  
  55.             /* Guardar posição */
  56.             Random rdn2 = new Random();
  57.             pos = (rdn2.Next(1,9));
  58.             /*---*/                            
  59.  
  60.             /*Ler caminho do documento e atribuir a variavel text*/                    
  61.             Console.WriteLine("Digite o caminho do documento a ser criptografado");
  62.             doc = Console.ReadLine();
  63.  
  64.             try
  65.             {
  66.                 using (StreamReader sr = new StreamReader(doc))
  67.                 {
  68.                    text = sr.ReadToEnd();
  69.                 }
  70.                 System.Console.WriteLine("Leitura do documento feita com sucesso");
  71.             }
  72.             catch (Exception e)
  73.             {
  74.                 Console.WriteLine("Documento não encontrado");
  75.                 Console.WriteLine(e);
  76.             }
  77.             /*---*/
  78.  
  79.             vet_text = new string[text.Length];
  80.             newvet_text = new string[text.Length];
  81.             cript = new int[text.Length];
  82.             cript_after = new int[text.Length];
  83.            
  84.            
  85.             /*Passar texto para dentro do vetor*/
  86.             foreach(char c in text)
  87.             {
  88.                 vet_text[i] = c.ToString();                                              
  89.                 i++;
  90.             }
  91.             /*---*/
  92.  
  93.             /* Identificar cada posição da letra no texto dentro do vetor base */
  94.             for (int j = 0; j < vet_text.Length; j++)
  95.             {            
  96.                 for(i = 0; i < vet_ascii.Length; i++)
  97.                 {
  98.                     if(vet_text[j] == vet_ascii[i])
  99.                     {                    
  100.                         cript[k] = i;
  101.                         k++;
  102.                     }    
  103.                 }
  104.             }
  105.             /*---*/
  106.            
  107.             /* Criando um vetor com os valores dos numeros do texto criptografado */
  108.            
  109.             for ( i = 0 ;  i < text.Length ; i++)
  110.             {                
  111.                 cript_after[i] = ((cript[i] * hash)+hash) % 78;                
  112.             }
  113.             /*---*/
  114.  
  115.             /* Gerando a letra criptografada */            
  116.             for (int j = 0; j < vet_text.Length; j++)
  117.             {            
  118.                 for(i = 0; i < vet_ascii.Length; i++)
  119.                 {
  120.                    newvet_text[j] = vet_ascii[cript_after[j]];
  121.                 }
  122.             }
  123.             /*---*/
  124.            
  125.             /*Escrever em um novo documento */
  126.             try
  127.             {
  128.                 using (StreamWriter writer = new StreamWriter("cript/newdoc.txt"))
  129.                 {
  130.                     writer.Write(pos.ToString()); //inserir posição onde estara a chave e o documento
  131.                     for (i = 0; i < pos; i++)
  132.                     {                        
  133.                         writer.Write(newvet_text[i]);
  134.                     }
  135.                     writer.Write(hash);
  136.                     writer.Write(chave);
  137.                     for (i = pos; i < newvet_text.Length - pos; i++)
  138.                     {
  139.                         writer.Write(newvet_text[i]);
  140.                     }                    
  141.                     System.Console.WriteLine("Texto criptografado passado para o documento com sucesso");
  142.  
  143.                     /* SAIDA NO DOCUMENTO
  144.                     "[pos]xxxxxxxx[hash][chave]xxxxxxxxxxxxxxxxxx"
  145.                     a posição de [hash][chave] sera definida por [pos]
  146.                      */
  147.                 }
  148.             }
  149.             catch (Exception e)
  150.             {
  151.                 Console.WriteLine("erro!!");
  152.                 Console.WriteLine(e);
  153.             }  
  154.  
  155. System.Console.WriteLine("hash: " + hash);
  156. System.Console.WriteLine("hash 2 :" + pos );
  157. System.Console.WriteLine("Chave: " + chave);
  158.             /*
  159.             /*  
  160.            
  161.             Controle de saida dos textos
  162.                      
  163.             System.Console.WriteLine();
  164.             System.Console.WriteLine();
  165.             System.Console.WriteLine("Controle de saida");
  166.             System.Console.WriteLine("hash: " + hash);
  167.             System.Console.WriteLine("hash 2 :" + pos );
  168.             System.Console.WriteLine("Chave: " + chave);
  169.             System.Console.WriteLine();          
  170.             Console.WriteLine("Texto a ser Criptografado: " + text);
  171.             System.Console.WriteLine();  
  172.  
  173.            
  174.             */    
  175.             Console.Write("cript: " );
  176.                 for(i = 0; i < text.Length; i++)
  177.                 {
  178.                      Console.Write(cript[i] + " - ");
  179.                 }
  180.                 Console.WriteLine();
  181.             Console.Write("cript_after: " );
  182.                 for(i = 0; i < text.Length; i++)
  183.                 {
  184.                      Console.Write(cript_after[i] + " - ");
  185.                 }
  186.                 Console.WriteLine();
  187.             return "\n-------------------------\nTexto Criptografado com sucesso!!!\n-------------------------";
  188.         }
  189.         static string Descriptografar()
  190.         {
  191.             /*            
  192.                 cript_before[i] = ((cript[i] * hash) + hash) % 78;
  193.  
  194.                 tenho que achar cript[i]
  195.  
  196.                 tenho hash
  197.  
  198.                 TO DO LIST:
  199.  
  200.                 - achar posição da letra criptografada antes;
  201.                 - fazer operação inversa
  202.                 - achar posição da letra correta
  203.  
  204.              */
  205.  
  206.             string doc_d , text_d = string.Empty , k = String.Empty , key_usuario , aut , msg_d = string.Empty , Hash = string.Empty ;
  207.             string[] vet_d , vet_hk , vet_k;
  208.             int[] vet_h ;
  209.             int i = 0 , pos;
  210.  
  211.  
  212.             /* VARIAVEIS PARA DESCRIPTOGRAFAR */
  213.             int[] pos_before, pos_after ;
  214.             int aux ,hash = 0;
  215.             string[] vet_text_d , descrip;
  216.             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",",",".",";",":","/","|","!","@","#","$","%","&","*","-","+","="};  
  217.             /*cript_after[i] = ((cript[i] * hash)+hash) % 78; */
  218.             /* 159 */
  219.             /* C  i  e  n  c  i  a */
  220.             /* 6  17 9  27 5  17 1 Original*/
  221.             /* 21 54 30 6  18 54 6 Criptografado*/
  222.             /*1113  */      
  223.             System.Console.WriteLine("Digite o caminho para o documento criptografado:");
  224.             doc_d = Console.ReadLine();
  225.             System.Console.WriteLine();
  226.            
  227.             /* Leitura do Documento D */
  228.             try
  229.             {                
  230.                 using (StreamReader sr = new StreamReader(doc_d))
  231.                 {
  232.                    text_d = sr.ReadToEnd();
  233.                 }
  234.                 System.Console.WriteLine("Leitura do documento feita com sucesso");
  235.             }
  236.             catch (Exception e)
  237.             {
  238.                 Console.WriteLine("Documento não encontrado");
  239.                 Console.WriteLine(e);
  240.             }
  241.             System.Console.WriteLine();
  242.  
  243.             vet_d = new string[text_d.Length];
  244.             vet_hk = new string[23];
  245.             vet_h = new int[3];
  246.             vet_k = new string[20];
  247.  
  248.            
  249.  
  250.             foreach(char d in text_d)
  251.             {
  252.                 vet_d[i] = d.ToString();
  253.                 i++;
  254.             }
  255.  
  256. System.Console.WriteLine("vet- d");
  257. for(i = 0 ; i < text_d.Length; i++)
  258.     {
  259.         System.Console.Write(vet_d[i] + " - ");
  260.     }System.Console.WriteLine();
  261.  
  262.             pos = int.Parse(vet_d[0]);
  263. System.Console.WriteLine("pos: " + pos);
  264.  
  265.             int j = 0;
  266.            
  267.             for(i = pos + 1 ; i < (pos + 1 + 23) ; i++)
  268.             {
  269.                 vet_hk[j] = vet_d[i];
  270.                 j++;
  271.             }
  272.  
  273. System.Console.WriteLine("vet-hk");
  274. for(i = 0 ; i < vet_hk.Length; i++)
  275.     {
  276.         System.Console.Write(vet_hk[i] + " - ");
  277.     }System.Console.WriteLine();  
  278.  
  279.            
  280.             for (j = 0; j < vet_h.Length; j++)
  281.             {
  282.                 vet_h[j] = int.Parse(vet_hk[j]);
  283.             }
  284.  
  285. System.Console.WriteLine("vet-h");
  286. for(i = 0 ; i < vet_h.Length; i++)
  287.     {
  288.         System.Console.Write(vet_h[i] + " - ");
  289.     }System.Console.WriteLine();  
  290.                        
  291.             for(i = 0 ; i < vet_h.GetLength(0); i++)
  292.             {
  293.                 Hash += vet_h[i];
  294.             }
  295. System.Console.WriteLine("hash: "+ Hash);            
  296.             hash = int.Parse(Hash);
  297.             i = 0;
  298.             for (j = 3; j < vet_k.Length + 3; j++)
  299.             {
  300.                 vet_k[i] =vet_hk[j];
  301.                 i++;
  302.             }
  303. System.Console.WriteLine("vet-k");
  304. for(i = 0 ; i < vet_k.Length; i++)
  305.     {
  306.         System.Console.Write(vet_k[i] + " - ");
  307.     }System.Console.WriteLine();            
  308.  
  309.             for (i = 0; i < vet_k.Length; i++)
  310.             {
  311.                 k += vet_k[i];
  312.             }
  313. System.Console.WriteLine("key - " + k);            
  314.  
  315.             System.Console.WriteLine("Digite a Key por favor");
  316.             key_usuario = Console.ReadLine();
  317.             System.Console.WriteLine();
  318.  
  319.             if(key_usuario == k)
  320.             {
  321.                 vet_d[0] = string.Empty;
  322.                 for(i = pos + 1; i < (23 + pos + 1) ; i++)
  323.                 {
  324.                     vet_d[i] = string.Empty;
  325.                 }
  326. System.Console.WriteLine("Vet_d: ");
  327. for(i = 0 ; i < vet_d.Length; i++)
  328. {
  329.     System.Console.Write(vet_d[i] + " ");
  330.    
  331. }
  332.                 for(i = 0 ; i < vet_d.Length; i++)
  333.                 {
  334.                     msg_d += vet_d[i];
  335.                 }
  336. System.Console.WriteLine("msg_d = " + msg_d);                
  337.                
  338.                 /* colocar msg_d em um vetor */
  339.                 vet_text_d = new string[msg_d.Length];
  340.                 pos_before = new int[msg_d.Length];
  341.                 pos_after = new int[msg_d.Length];
  342.                 descrip = new string[msg_d.Length];
  343.                 i = 0;
  344.                 foreach(char t in msg_d)
  345.                 {
  346.                     vet_text_d[i] = t.ToString();
  347.                     i++;
  348.                 }
  349.                 aux = 0;
  350.  
  351. System.Console.WriteLine("vet_text_d: ");
  352. for(i = 0 ; i < vet_text_d.Length; i++)
  353. {
  354.     System.Console.Write(vet_text_d[i] + " ");
  355.    
  356. }
  357. System.Console.WriteLine();
  358. System.Console.WriteLine("Tamanhos");
  359. System.Console.WriteLine(vet_d.Length);
  360. System.Console.WriteLine(vet_text_d.Length);
  361. System.Console.WriteLine(msg_d.Length);
  362. System.Console.WriteLine(pos_after.Length);
  363. System.Console.WriteLine(pos_before.Length);
  364. System.Console.WriteLine();
  365.                 /* OK */
  366.  
  367.                 for (j = 0; j < vet_text_d.Length; j++)
  368.                 {            
  369.                     for(i = 0; i < vet_ascii.Length; i++)
  370.                     {
  371.                         if(vet_text_d[j] == vet_ascii[i])
  372.                         {                    
  373.                             pos_before[aux] = i;
  374.                             aux++;
  375.                         }    
  376.                     }
  377.                 }
  378.  
  379.                 /*
  380.                 for (int j = 0; j < vet_text.Length; j++)
  381.                 {            
  382.                     for(i = 0; i < vet_ascii.Length; i++)
  383.                     {
  384.                         if(vet_text[j] == vet_ascii[i])
  385.                         {                    
  386.                             cript[k] = i;
  387.                             k++;
  388.                         }    
  389.                     }
  390.                 }
  391.                  */
  392.                
  393.                
  394.                 System.Console.WriteLine("pos before");
  395.                 for(i = 0 ; i < vet_text_d.Length; i++)
  396.                 {
  397.                     System.Console.Write(pos_before[i] + " ");
  398.                 }
  399.                 System.Console.WriteLine();
  400.                
  401.                 /* FAZER */
  402.                 /*
  403.                     hash 880
  404.                     6     17    9
  405.                     76    6     64
  406.                     12    32    60    
  407.  
  408.                     64
  409.                  */                
  410.                
  411.                 for ( i = 0 ;  i < pos_before.Length ; i++)
  412.                 {
  413.                     pos_after[i] = (pos_before[i] - (hash % 78))%78;
  414.                 }                   //((cript[i] * hash)-hash) % 78;
  415.  
  416.                 System.Console.WriteLine();
  417.                 System.Console.WriteLine(hash % 78);
  418.                 System.Console.WriteLine();
  419.                 System.Console.WriteLine("pos_after:");
  420.  
  421.                 for(i = 0; i < pos_after.Length;i++)
  422.                 {
  423.                     System.Console.Write(pos_after[i] + " ");
  424.                 }
  425.  
  426.                 System.Console.WriteLine();
  427.                
  428.                
  429.                 for (j = 0; j < pos_after.Length; j++)
  430.                 {            
  431.                     for(i = 0; i < vet_ascii.Length; i++)
  432.                     {
  433.                         descrip[j] = vet_ascii[pos_after[j]];
  434.                     }
  435.                 }
  436.                 for(i = 0; i < msg_d.Length; i++)
  437.                 {
  438.                     System.Console.Write(descrip[i]);
  439.                 }
  440.                 System.Console.WriteLine();
  441.                
  442.  
  443.  
  444.  
  445.                 aut = "Texto Descriptografado com sucesso!!!!!!! ";
  446.             }
  447.             else
  448.             {
  449.                 aut = "Chave de acesso incorreto!!\nVocê não esta autorizado a acessar esse documento!";
  450.             }
  451.  
  452. // for(i = 0; i < vet_h.Length; i++ )          
  453. // {
  454. //     System.Console.Write(vet_h[i]);
  455. // }
  456. // System.Console.WriteLine();
  457.  
  458. // for(i = 0; i < vet_k.Length; i++ )          
  459. // {
  460. //     System.Console.Write(vet_k[i]);
  461. // }
  462.  
  463.            
  464.  
  465. //          6ooooooxxxxxxxxxxxxxxxxxxxxxxxooooooooooooo
  466. //          0123456789012345678901234567890123456789012
  467. //                    111111111122222222223333333333444
  468.             return aut;
  469.         }
  470.  
  471.         static void Main(string[] args)
  472.         {                    
  473.             int cript;        
  474.             System.Console.WriteLine("(1) Criptografar\n(2) Descriptografar");
  475.             cript = int.Parse(Console.ReadLine());
  476.            
  477.             switch(cript)
  478.             {
  479.                 case 1:
  480.                     System.Console.WriteLine(Criptografar());
  481.                 break;
  482.                 case 2:
  483.                     System.Console.WriteLine(Descriptografar());
  484.                 break;
  485.                 default:
  486.                     System.Console.WriteLine("Opção invalida");
  487.                 break;
  488.             }
  489.                
  490.             /*  hash: 295
  491.                 hash 2 :6
  492.                 Chave: N96cDMdcghek99g4a4ge */
  493.  
  494.             /* DESCRIPTOGRAFAR */    
  495.            
  496.            
  497.             // bool continuar = false;
  498.  
  499.             // System.Console.WriteLine("Deseja Descriptografar?");
  500.             // if (continuar)
  501.             // {
  502.                
  503.             // }
  504.  
  505.         }
  506.     }
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement