diff --git a/.gitignore b/.gitignore index d94b32a..e6b27b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +KeyInfo.xml + + # ---> macOS # General .DS_Store @@ -5,7 +8,8 @@ .LSOverride # Icon must end with two \r -Icon +Icon + # Thumbnails ._* diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 74f273a..aaba79f 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -11,7 +11,8 @@ namespace StateJobsNYSubmit.Pages public class IndexModel : PageModel { private readonly ILogger _logger; - + public int counter = 1; + private PrivateData p = new PrivateData(); public IndexModel(ILogger 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}"); + } } } diff --git a/PrivateData.cs b/PrivateData.cs new file mode 100644 index 0000000..e22a245 --- /dev/null +++ b/PrivateData.cs @@ -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 ""; + } + } +} \ No newline at end of file