├── 03-Random └── random.cs ├── 04-RSA ├── comandos.sh ├── rsa.cs └── rsa_parametros.cs ├── 02-AES ├── ecb.cs └── gcm.cs └── 01-vigenere └── analise.md /03-Random/random.cs: -------------------------------------------------------------------------------- 1 | var random = new Random(10); 2 | for (int i = 0; i < 10; i++) 3 | { 4 | Console.WriteLine(random.Next()); 5 | } -------------------------------------------------------------------------------- /04-RSA/comandos.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Gerar chave RSA de 2048 bits 4 | openssl genrsa -out rsa-private-key.pem 2048 5 | 6 | # Exibir informacoes da chave 7 | openssl rsa -in rsa-private-key.pem -text -noout 8 | 9 | # Gerar chave publica 10 | openssl rsa -in rsa-private-key.pem -pubout -out rsa-public-key.pem 11 | 12 | # Exibir informacoes da chave publica 13 | openssl rsa -pubin -in rsa-public-key.pem -text -noout -------------------------------------------------------------------------------- /02-AES/ecb.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | 4 | var mensagem = "desenvolvedor.io 654654"; 5 | byte[] chave = new byte[16]; // <- Tamanho da chave 6 | RandomNumberGenerator.Fill(chave); // <- Preenche a chave com números verdadeiramente aleatórios 7 | 8 | Console.WriteLine("============== CRIPTOGRAFANDO =============="); 9 | 10 | Aes aes = Aes.Create(); 11 | aes.Key = chave; // Oracle Padding 12 | var ciphertext = aes.EncryptEcb(Encoding.UTF8.GetBytes(mensagem), PaddingMode.PKCS7); // <- Criptografa utilizando o ECB (Não recomendado) / PaddingMode.PKCS7 13 | 14 | Console.WriteLine("Mensagem: {0}", mensagem); 15 | Console.WriteLine("Senha: {0}", Convert.ToHexString(chave)); 16 | Console.WriteLine("Cipher: {0}", Convert.ToHexString(ciphertext)); 17 | Console.WriteLine(); 18 | 19 | Console.WriteLine("============== DESCRIPTOGRAFANDO =============="); 20 | 21 | Console.WriteLine(Encoding.UTF8.GetString(aes.DecryptEcb(ciphertext, PaddingMode.PKCS7))); -------------------------------------------------------------------------------- /02-AES/gcm.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | static void WriteByteArray(string name, byte[] byteArray) 4 | { 5 | Console.Write("{0}: {1} bytes, {2}-bit:", name, byteArray.Length, byteArray.Length << 3); 6 | Console.WriteLine(" -> {0} - base64: {1}", BitConverter.ToString(byteArray), Convert.ToBase64String(byteArray)); 7 | } 8 | 9 | // variaveis 10 | string mensagem = "desenvolvedor.io"; 11 | byte[] chave = new byte[16]; 12 | byte[] initializationVector = new byte[12]; 13 | byte[] authTag = new byte[16]; 14 | 15 | // Gera a chave e o iv 16 | RandomNumberGenerator.Fill(chave); 17 | RandomNumberGenerator.Fill(initializationVector); 18 | 19 | // Exibe as informacoes na tela 20 | Console.WriteLine("============== CRIPTOGRAFANDO =============="); 21 | Console.WriteLine("mensagem: {0}", mensagem); 22 | WriteByteArray("chave", chave); 23 | Console.WriteLine(); 24 | 25 | // converte a mensagem para bytes 26 | byte[] plainBytes = Encoding.UTF8.GetBytes(mensagem); 27 | byte[] cipher = new byte[plainBytes.Length]; 28 | 29 | // Criptografia 30 | using (var aesgcm = new AesGcm(chave)) 31 | aesgcm.Encrypt(initializationVector, plainBytes, cipher, authTag); 32 | 33 | WriteByteArray("cipher", cipher); 34 | WriteByteArray("iv", initializationVector); 35 | WriteByteArray("authTag", authTag); 36 | 37 | Console.WriteLine(); 38 | 39 | Console.WriteLine("============== DESCRIPTOGRAFANDO =============="); 40 | // Transforma em base64 para poder transmitir 41 | Console.WriteLine("cipher: {0}", Convert.ToBase64String(cipher)); 42 | Console.WriteLine("iv: {0}", Convert.ToBase64String(initializationVector)); 43 | Console.WriteLine("authTag: {0}", Convert.ToBase64String(authTag)); 44 | 45 | 46 | Console.WriteLine(); 47 | // allocate the decrypted text byte array as the same size as the plain text byte array 48 | byte[] decryptedBytes = new byte[cipher.Length]; 49 | // perform decryption 50 | using (AesGcm aesgcm = new AesGcm(chave)) 51 | aesgcm.Decrypt(initializationVector, cipher, authTag, decryptedBytes); 52 | 53 | // convert the byte array to the plain text string 54 | string decryptedText = Encoding.UTF8.GetString(decryptedBytes); 55 | Console.WriteLine("mensagem: {0}", decryptedText); 56 | Console.WriteLine(); 57 | 58 | -------------------------------------------------------------------------------- /01-vigenere/analise.md: -------------------------------------------------------------------------------- 1 | Cryptool 2 2 | ----------- 3 | 4 | 5 | Texto 6 | 7 | ``` 8 | Bem vindo a desenvolvedor.io 9 | 10 | Somos uma plataforma de cursos de programação e tecnologias no modelo EAD. Nosso diferencial está em oferecer cursos completos, baseados em cenários reais do dia-a-dia e na grande experiência de mercado de nossos instrutores. 11 | 12 | Todos os nossos cursos estão 100% alinhados com as expectativas do mercado e com certeza irão agregar em um grande diferencial na sua carreira e sucesso profissional ou do seu time. 13 | 14 | Fundada em 2018 o desenvolvedor.io é uma plataforma que oferece cursos exclusivos com o foco em desenvolver todo o potencial dos atuais e futuros desenvolvedores de software. 15 | 16 | Estabelecida em São Paulo, possuímos estúdio próprio onde desenvolvemos e gravamos nossos materiais com grande qualidade de som e imagem. Todo processo de criação de conteúdo, gravação e edição passa por diversas fases e validações para garantir a melhor qualidade possível nos nossos conteúdos. 17 | 18 | Nossa história começa em 2012 quando o fundador Eduardo Pires iniciou na área da educação realizando treinamentos para as maiores empresas do Brasil e também para grupos privados. Acumulando mais de 6 anos de experiência em formação de pessoas fundou a desenvolvedor.io para democratizar ainda mais o acesso ao conhecimento. 19 | ``` 20 | 21 | Senha: BRUNO 22 | 23 | ```cipher 24 | Cvg iwoui n rfjyajpcprrpi.cb 25 | 26 | Gpdif inr jyourzbfnr xr qvimbg ev jechiuzoçãp v nrqoffbujrm ac nfxrzp VUQ. Bpjmb rjwyesotcnz fjná ra pwyesdvl pisjif qpdjysufm, ootvuqct vg psoáicbg svuvg ef xvo-b-ucn s or aeoouy rlqvlvêbdzu qs nvlpoef xr bpjmbg jemgfvkiest. 27 | 28 | Kiqct fm actjif qvimbg fjnãb 100% omzhuoefm pcn rm rlqvwgouzpng ef grfdrxb s dfg psskymo jiãi nusvanf fd oz usrhqs ezzrffewvom eu fib tueffzln s tlwrgtf jecgzmfwpeuy cv ui fsv kczs. 29 | 30 | Glhqoer yz 2018 c evmrbwffisefl.vc é vdu czbkuscsdu dif fzrffty pisjif sytfhgjmif qpd i scdf yz rfjyajpcprf ufxb c qfnrbdzuy rpj ugibzm r tvkoect uyfsomiyjfuiest uy fcgkqnff. 31 | 32 | Vmgocvfrqjuu ra Tãf Jnimf, jbgtlígbg fjnúqwp glócfjf iarf uyfsomiyjfdif s hiuionfm actjif abkyewbzm pcn xlnbev khomzxnrf uy fcn v czohvg. Gcef jecdvmfc ev wewbçãf xr qpenrúrp, xlnjbçãf y rrjçãf jngtr jbf ezprftrm sotvm r jbccqoçõfj jnfb xueookce o nvfucs honzjuuqs qfmfíjfc hbg ofmfct tiahfúuif. 33 | 34 | Bpjmn vjjnóewb tizsçb vg 2012 dibexb c glhqoefl Rrvrlqc Qzlrg jecpwpl hn áffr xn selwnçãc svuywarhqc uiyvbbdyahpj jnfb rm zojflrg fdjestrm qc Ciufwm v nnacéd jnfb xlhdpj jewwrxbg. Btozimrhqc nrcf rf 6 rhbg ev ykdficêaqjr yz tpignçãc ev jrgtfuf tvexbi b uyfsomiyjfuie.wp gueo evgbqsrnvnbi uvber gnwt f upstji nc dfhusdzgrbuf. 35 | ``` 36 | -------------------------------------------------------------------------------- /04-RSA/rsa.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | using System.Text.Json; 4 | static void WriteByteArray(string name, byte[] byteArray) 5 | { 6 | Console.Write("{0}: {1} bytes, {2}-bit:", name, byteArray.Length, byteArray.Length << 3); 7 | Console.WriteLine(" -> {0}", BitConverter.ToString(byteArray)); 8 | } 9 | 10 | 11 | var rsa = RSA.Create(); 12 | var pem = File.ReadAllText("rsa-private-key.pem"); 13 | rsa.ImportFromPem(pem); 14 | 15 | var mensagem = "desenvolvedorio"; 16 | 17 | 18 | Console.WriteLine("========================== CRIPTOGRAFAR ========================="); 19 | 20 | var cypher = rsa.Encrypt(Encoding.UTF8.GetBytes(mensagem), RSAEncryptionPadding.Pkcs1); 21 | WriteByteArray("Cypher", cypher); 22 | 23 | Console.WriteLine("========================== DESCRIPTOGRAFAR ========================="); 24 | 25 | var clearText = rsa.Decrypt(cypher, RSAEncryptionPadding.Pkcs1); 26 | Console.WriteLine($"Clear text: {Encoding.UTF8.GetString(clearText)}"); 27 | 28 | 29 | Console.WriteLine("========================== ASSINAR DIGITALMENTE ========================="); 30 | 31 | var assinatura = rsa.SignData(Encoding.UTF8.GetBytes(mensagem), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); 32 | var documento = new { Mensagem = mensagem, Assinatura = assinatura }; 33 | Console.WriteLine($"Documento:"); 34 | Console.WriteLine(JsonSerializer.Serialize(documento, new JsonSerializerOptions() { WriteIndented = true })); 35 | 36 | Console.WriteLine("========================== VALIDAR ASSINATURA ========================="); 37 | 38 | var assinaturaEstaCorreta = rsa.VerifyData(Encoding.UTF8.GetBytes(mensagem), assinatura, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); 39 | Console.WriteLine($"Assinatura esta correta: {assinaturaEstaCorreta}"); 40 | 41 | 42 | Console.WriteLine("========================== CARREGAR CHAVE PUBLICA ========================="); 43 | 44 | rsa = RSA.Create(); 45 | pem = File.ReadAllText("rsa-public-key.pem"); 46 | rsa.ImportFromPem(pem); 47 | Console.WriteLine("Utilizando objeto RSA apenas com chave publica"); 48 | 49 | 50 | Console.WriteLine("========================== CRIPTOGRAFAR COM CHAVE PUBLICA ========================="); 51 | cypher = rsa.Encrypt(Encoding.UTF8.GetBytes(mensagem), RSAEncryptionPadding.Pkcs1); 52 | WriteByteArray("Cypher", cypher); 53 | 54 | Console.WriteLine("========================== VALIDAR ASSINATURA COM CHAVE PUBLICA ========================="); 55 | assinaturaEstaCorreta = rsa.VerifyData(Encoding.UTF8.GetBytes(mensagem), assinatura, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); 56 | Console.WriteLine($"Assinatura esta correta: {assinaturaEstaCorreta}"); 57 | 58 | Console.WriteLine("========================== TENTAR DESCRIPTOGRAFAR COM CHAVE PUBLICA ========================="); 59 | try 60 | { 61 | rsa.Decrypt(cypher, RSAEncryptionPadding.Pkcs1); 62 | } 63 | catch (Exception e) 64 | { 65 | Console.WriteLine(e); 66 | } 67 | 68 | Console.WriteLine("========================== TENTAR ASSINAR COM CHAVE PUBLICA ========================="); 69 | try 70 | { 71 | rsa.SignData(Encoding.UTF8.GetBytes(mensagem), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); 72 | } 73 | catch (Exception e) 74 | { 75 | Console.WriteLine(e); 76 | } -------------------------------------------------------------------------------- /04-RSA/rsa_parametros.cs: -------------------------------------------------------------------------------- 1 | #r "nuget: RSAExtensions, 1.0.3" 2 | using System.Numerics; 3 | using System.Security.Cryptography; 4 | using System.Text; 5 | using RSAExtensions; 6 | 7 | var asNumber = (byte[] byteRep) => new BigInteger(byteRep.Reverse().Concat(new byte[1]).ToArray()); 8 | 9 | // Cria o RSA. O algoritmo automaticamente cria uma chave de 2048 bits 10 | var rsa = RSA.Create(2048); 11 | var pem = File.ReadAllText("rsa-private-key.pem"); 12 | rsa.ImportFromPem(pem); 13 | 14 | var chavePublica = rsa.ExportParameters(false); 15 | var chavePrivada = rsa.ExportParameters(true); 16 | 17 | // Obtem os parametros como números 18 | var n = asNumber(chavePublica.Modulus); 19 | var p = asNumber(chavePrivada.P); 20 | var q = asNumber(chavePrivada.Q); 21 | 22 | // Criptografar 23 | var mensagem = "desenvolvedorio"; 24 | var cypher = rsa.Encrypt(Encoding.UTF8.GetBytes(mensagem), RSAEncryptionPadding.Pkcs1); 25 | 26 | // Descriptografar 27 | 28 | Console.ForegroundColor = ConsoleColor.White; 29 | Console.WriteLine("========================== PARAMETROS DO RSA =========================="); 30 | Console.WriteLine(); 31 | Console.ResetColor(); 32 | 33 | Console.ForegroundColor = ConsoleColor.Magenta; 34 | Console.WriteLine($" CHAVE PUBLICA (e,n):"); 35 | Console.WriteLine(); 36 | Console.ForegroundColor = ConsoleColor.DarkYellow; 37 | Console.Write("Expoente (e) = "); 38 | Console.ForegroundColor = ConsoleColor.Green; 39 | Console.WriteLine(asNumber(chavePublica.Exponent)); 40 | Console.ForegroundColor = ConsoleColor.DarkYellow; 41 | Console.Write("Modulo (n) = "); 42 | Console.ForegroundColor = ConsoleColor.Blue; 43 | Console.WriteLine(n); 44 | Console.ResetColor(); 45 | 46 | Console.WriteLine(); 47 | Console.WriteLine(rsa.ExportPublicKey(RSAKeyType.Pkcs1, true)); 48 | 49 | Console.ForegroundColor = ConsoleColor.Magenta; 50 | Console.WriteLine($" CHAVE PRIVADA (d,n):"); 51 | Console.WriteLine(); 52 | Console.ForegroundColor = ConsoleColor.DarkYellow; 53 | Console.Write("Expoente privado (d) = "); 54 | Console.ForegroundColor = ConsoleColor.Red; 55 | Console.WriteLine(asNumber(chavePrivada.D)); 56 | Console.ForegroundColor = ConsoleColor.DarkYellow; 57 | Console.Write("Inverso D = "); 58 | Console.ForegroundColor = ConsoleColor.Red; 59 | Console.WriteLine(asNumber(chavePrivada.InverseQ)); 60 | Console.ForegroundColor = ConsoleColor.DarkYellow; 61 | Console.Write("Modulo (n) = "); 62 | Console.ForegroundColor = ConsoleColor.Blue; 63 | Console.WriteLine(n); 64 | Console.ResetColor(); 65 | 66 | Console.WriteLine(); 67 | Console.WriteLine(rsa.ExportPrivateKey(RSAKeyType.Pkcs1, true)); 68 | Console.WriteLine(); 69 | 70 | 71 | 72 | Console.ForegroundColor = ConsoleColor.White; 73 | Console.WriteLine("========================== VALIDANDO OS PARAMETROS =========================="); 74 | Console.ResetColor(); 75 | 76 | Console.WriteLine(); 77 | Console.Write($"Primo 1 (p) = "); 78 | Console.ForegroundColor = ConsoleColor.Cyan; 79 | Console.WriteLine(p); 80 | Console.ResetColor(); 81 | Console.WriteLine(); 82 | Console.Write($"Primo 2 (q) = "); 83 | Console.ForegroundColor = ConsoleColor.Cyan; 84 | Console.WriteLine(q); 85 | Console.ResetColor(); 86 | Console.WriteLine(); 87 | Console.Write($"Modulo (n) = "); 88 | Console.ForegroundColor = ConsoleColor.Blue; 89 | Console.WriteLine(n); 90 | Console.ResetColor(); 91 | 92 | Console.WriteLine(); 93 | Console.WriteLine($"p * q = n ? {p * q == n}"); 94 | Console.WriteLine(); 95 | 96 | 97 | Console.ForegroundColor = ConsoleColor.White; 98 | Console.WriteLine("========================== CRIPTOGRAFAR =========================="); 99 | Console.ResetColor(); 100 | 101 | Console.Write($"Cypher: "); 102 | Console.ForegroundColor = ConsoleColor.Magenta; 103 | Console.WriteLine(BitConverter.ToString(cypher).ToLower().Replace("-", string.Empty)); 104 | Console.WriteLine(); 105 | Console.ForegroundColor = ConsoleColor.White; 106 | 107 | 108 | 109 | Console.ForegroundColor = ConsoleColor.White; 110 | Console.WriteLine("========================== DESCRIPTOGRAFAR =========================="); 111 | Console.ResetColor(); 112 | 113 | Console.Write($"Clear Text: "); 114 | Console.ForegroundColor = ConsoleColor.Yellow; 115 | var clearText = rsa.Decrypt(cypher, RSAEncryptionPadding.Pkcs1); 116 | Console.WriteLine(Encoding.UTF8.GetString(clearText)); 117 | Console.ResetColor(); --------------------------------------------------------------------------------