Skip to content

Commit

Permalink
Transformar programa Criptografia.java em um projeto C#
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelo-cintra committed Dec 13, 2023
1 parent fadf0b4 commit 9328e92
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Criptografia-Itau.sln
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions Criptografia-Itau/Criptografia-Itau.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Criptografia_Itau</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
36 changes: 36 additions & 0 deletions Criptografia-Itau/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
}
101 changes: 101 additions & 0 deletions Criptografia-Itau/Utils.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 9328e92

Please sign in to comment.