Browse Source

Basic encrypted cookies.

master
Gregory Rudolph 3 years ago
parent
commit
637170ee34
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 6
      .gitignore
  2. 13
      Pages/Index.cshtml.cs
  3. 57
      PrivateData.cs

6
.gitignore vendored

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

13
Pages/Index.cshtml.cs

@ -11,7 +11,8 @@ namespace StateJobsNYSubmit.Pages @@ -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,7 +20,15 @@ namespace StateJobsNYSubmit.Pages @@ -19,7 +20,15 @@ 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

@ -0,0 +1,57 @@ @@ -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 "";
}
}
}
Loading…
Cancel
Save