Skip to content

Commit

Permalink
Fix: Hash invalidando formato da url.
Browse files Browse the repository at this point in the history
  • Loading branch information
LibardiFelipe committed Oct 10, 2022
1 parent cb2deee commit cd7db04
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions TemplateBase.Domain/Utils/Crypt.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.AspNetCore.WebUtilities;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
Expand All @@ -21,11 +22,11 @@ public static string EncryptString(string key, string text)

ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

using (MemoryStream memoryStream = new MemoryStream())
using (MemoryStream memoryStream = new())
{
using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
using (CryptoStream cryptoStream = new(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
using (StreamWriter streamWriter = new(cryptoStream))
{
streamWriter.Write(text);
}
Expand All @@ -35,10 +36,11 @@ public static string EncryptString(string key, string text)
}
}

return Convert.ToBase64String(array).Replace("/", "_");
return WebEncoders.Base64UrlEncode(array);
}
catch
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "";
}
}
Expand All @@ -48,28 +50,29 @@ public static string DecryptString(string key, string text)
try
{
byte[] iv = new byte[16];
byte[] buffer = Convert.FromBase64String(text.Replace("_", "/"));
byte[] buffer = WebEncoders.Base64UrlDecode(text);

using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(key);
aes.IV = iv;
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

using (MemoryStream memoryStream = new MemoryStream(buffer))
using (MemoryStream memoryStream = new(buffer))
{
using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
using (CryptoStream cryptoStream = new(memoryStream, decryptor, CryptoStreamMode.Read))
{
using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
using (StreamReader streamReader = new(cryptoStream))
{
return streamReader.ReadToEnd();
}
}
}
}
}
catch
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "";
}
}
Expand Down

0 comments on commit cd7db04

Please sign in to comment.