diff --git a/Criptografia-Itau.sln b/Criptografia-Itau.sln new file mode 100644 index 0000000..a2a99f9 --- /dev/null +++ b/Criptografia-Itau.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34322.80 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Criptografia-Itau", "Criptografia-Itau\Criptografia-Itau.csproj", "{61BE2EAC-2222-4FA7-A0AF-34043D4AFD63}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {61BE2EAC-2222-4FA7-A0AF-34043D4AFD63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {61BE2EAC-2222-4FA7-A0AF-34043D4AFD63}.Debug|Any CPU.Build.0 = Debug|Any CPU + {61BE2EAC-2222-4FA7-A0AF-34043D4AFD63}.Release|Any CPU.ActiveCfg = Release|Any CPU + {61BE2EAC-2222-4FA7-A0AF-34043D4AFD63}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {98D5C336-A7AE-4A54-BD50-876FA438ECD4} + EndGlobalSection +EndGlobal diff --git a/Criptografia-Itau/Criptografia-Itau.csproj b/Criptografia-Itau/Criptografia-Itau.csproj new file mode 100644 index 0000000..973e612 --- /dev/null +++ b/Criptografia-Itau/Criptografia-Itau.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + Criptografia_Itau + enable + enable + + + diff --git a/Criptografia-Itau/Program.cs b/Criptografia-Itau/Program.cs new file mode 100644 index 0000000..0866d88 --- /dev/null +++ b/Criptografia-Itau/Program.cs @@ -0,0 +1,36 @@ +using Criptografia_Itau; + +try +{ + Console.WriteLine("\n========================================="); + Console.WriteLine("Informe as informações recebidas no e-mail"); + Console.WriteLine("========================================="); + + Console.Write("\nClientId: "); + string clientIdCifrado = Console.ReadLine().Trim(); + + Console.Write("\nToken Temporário: "); + string tokenCifrado = Console.ReadLine().Trim(); + + Console.Write("\nChave Sessão: "); + string chaveSessaoCifrada = Console.ReadLine().Trim(); + + Console.Write("\nCaminho chave privada: "); + string caminhoChavePrivada = Console.ReadLine().Trim(); + + Console.WriteLine("\n====================================="); + Console.WriteLine(" Processo de Decriptografia "); + Console.WriteLine("====================================="); + + // Decifra a chave de sessao AES com a chave RSA privada + byte[] chaveSessaoDecifrada = Utils.DecriptografiaRsa(caminhoChavePrivada, chaveSessaoCifrada); + // Decriptografa a credencial através da chave de sessão AES + string clientIdDecifrada = Utils.DecriptografiaAes(chaveSessaoDecifrada, clientIdCifrado); + Console.WriteLine("\nClient id decifrado com a chave de sessao AES:\n[ " + clientIdDecifrada + " ]"); + string tokenDecifrado = Utils.DecriptografiaAes(chaveSessaoDecifrada, tokenCifrado); + Console.WriteLine("\nToken decifrado com a chave de sessao AES:\n[ " + tokenDecifrado + " ]"); +} +catch (Exception e) +{ + Console.WriteLine(e.Message); +} \ No newline at end of file diff --git a/Criptografia-Itau/Utils.cs b/Criptografia-Itau/Utils.cs new file mode 100644 index 0000000..4303e77 --- /dev/null +++ b/Criptografia-Itau/Utils.cs @@ -0,0 +1,101 @@ +using System.Security.Cryptography; +using System.Text; + +namespace Criptografia_Itau +{ + public static class Utils + { + public static string ExtrairChaveRsaPem(string tipoChave, string arquivoChavesRsa) + { + try + { + using (var reader = new StreamReader(arquivoChavesRsa)) + { + StringBuilder sb = new StringBuilder(); + bool inKey = false; + string line; + while ((line = reader.ReadLine()) != null) + { + if (!inKey) + { + if (line.StartsWith("-----BEGIN ") && line.EndsWith(" " + tipoChave + " KEY-----")) + { + inKey = true; + } + } + else + { + if (line.StartsWith("-----END ") && line.EndsWith(" " + tipoChave + " KEY-----")) + { + break; + } + sb.Append(line); + } + } + return sb.ToString(); + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + + return null; + } + + public static string DecriptografiaAes(byte[] key, string cipherText) + { + try + { + using (Aes aesAlg = Aes.Create()) + { + aesAlg.Key = key; + aesAlg.IV = new byte[16]; // Default IV with zeroes + aesAlg.Mode = CipherMode.CBC; + aesAlg.Padding = PaddingMode.PKCS7; + + ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); + byte[] cipherBytes = Convert.FromBase64String(cipherText); + using (var msDecrypt = new MemoryStream(cipherBytes)) + { + using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) + { + using (var srDecrypt = new StreamReader(csDecrypt)) + { + return srDecrypt.ReadToEnd(); + } + } + } + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + + return null; + } + + public static byte[] DecriptografiaRsa(string caminhoChavePrivada, string dadosCifrados) + { + try + { + string chavePrivadaPem = ExtrairChaveRsaPem("PRIVATE", caminhoChavePrivada); + byte[] chavePrivadaBytes = Convert.FromBase64String(chavePrivadaPem); + + using (RSA rsa = RSA.Create()) + { + rsa.ImportPkcs8PrivateKey(chavePrivadaBytes, out _); + byte[] dadosCifradosBytes = Convert.FromBase64String(dadosCifrados); + return rsa.Decrypt(dadosCifradosBytes, RSAEncryptionPadding.Pkcs1); + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + + return null; + } + } +}