Basic encrypted cookies.
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,3 +1,6 @@
|
||||
KeyInfo.xml
|
||||
|
||||
|
||||
# ---> macOS
|
||||
# General
|
||||
.DS_Store
|
||||
@ -5,7 +8,8 @@
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
Icon
|
||||
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
@ -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
|
||||
|
||||
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
57
PrivateData.cs
Normal 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 "";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user