Basic encrypted cookies.

This commit is contained in:
2021-07-30 16:28:23 -04:00
parent 93a3b140d3
commit 637170ee34
3 changed files with 73 additions and 3 deletions

4
.gitignore vendored
View File

@ -1,3 +1,6 @@
KeyInfo.xml
# ---> macOS
# General
.DS_Store
@ -7,6 +10,7 @@
# Icon must end with two \r
Icon
# Thumbnails
._*

View File

@ -11,7 +11,8 @@ namespace StateJobsNYSubmit.Pages
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public int counter = 1;
private PrivateData p = new PrivateData();
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
@ -19,6 +20,14 @@ namespace StateJobsNYSubmit.Pages
public void OnGet()
{
string cookieValue = Request.Cookies["PrivateData"];
if (cookieValue != null) {
string test = p.DecryptString(cookieValue);
counter = Int32.Parse(test);
counter++;
}
Response.Cookies.Append("PrivateData", p.EncryptString($"{counter}"));
Console.WriteLine($"Value of counter: {counter}");
}
}

57
PrivateData.cs Normal file
View File

@ -0,0 +1,57 @@
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System;
class PrivateData
{
private RSACryptoServiceProvider RSA;
public PrivateData()
{
RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(File.ReadAllText("KeyInfo.xml"));
}
public string EncryptString(string input)
{
return Encryption(Encoding.UTF8.GetBytes(input), RSA.ExportParameters(false), false);
}
public string DecryptString(string input)
{
return Decryption(Convert.FromBase64String(input), RSA.ExportParameters(false), false);
}
private string Encryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
{
try
{
Console.WriteLine(RSAKey.ToString());
byte[] encryptedData;
encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
return Convert.ToBase64String(encryptedData);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
public string Decryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
{
try
{
byte[] decryptedData;
decryptedData = RSA.Decrypt(Data, DoOAEPPadding);
return Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length); ;
}
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());
return "";
}
}
}