-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transformar programa Criptografia.java em um projeto C#
- Loading branch information
1 parent
fadf0b4
commit 9328e92
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |