Skip to content

Instantly share code, notes, and snippets.

@jairzhino jairzhino/IcipherData.cs
Last active Dec 8, 2019

Embed
What would you like to do?
cipher data
using System;
using System.Collections.Generic;
namespace blazor3.providers
{
public class ListData : IListData
{
public List<string> lista { get; set; } = new List<string>();
private byte[] base64tosArrayByte(string strbase64)
{
byte[] decodedBytes = Convert.FromBase64String(strbase64);
return decodedBytes;
}
public string Decrypt(string plaintext, string strkey)
{
try
{
string strResult = "";
if (strkey.Length > 16)
strkey = strkey.Substring(0, 16);
string straux = "ADOTRMDWOD1QWELK";
byte[] key = System.Text.Encoding.UTF8.GetBytes(strkey);
byte[] IV = System.Text.Encoding.UTF8.GetBytes(straux);
byte[] encryptedText = base64tosArrayByte(plaintext);
using (var aes = System.Security.Cryptography.Aes.Create())
{
aes.IV = IV;
aes.Key = key;
var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (var ms = new System.IO.MemoryStream(encryptedText))
{
using (var csDecrypt = new System.Security.Cryptography.CryptoStream(ms, decryptor,
System.Security.Cryptography.CryptoStreamMode.Read))
{
using (var swDecrypt = new System.IO.StreamReader(csDecrypt))
{
strResult = swDecrypt.ReadToEnd();
Console.WriteLine("decrypt : " + strResult);
}
}
}
}
return strResult;
}
catch (System.Exception ex)
{
return ex.Message;
}
}
public string Encrypt(string plaintext, string strkey)
{
try
{
string strResult = "";
if (strkey.Length > 16)
strkey = strkey.Substring(0, 16);
string straux = "ADOTRMDWOD1QWELK";
byte[] key = System.Text.Encoding.UTF8.GetBytes(strkey);
byte[] IV = System.Text.Encoding.UTF8.GetBytes(straux);
byte[] encrypted;
using (var aes = System.Security.Cryptography.Aes.Create())
{
aes.IV = IV;
aes.Key = key;
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (var ms = new System.IO.MemoryStream())
{
using (var csEncrypt = new System.Security.Cryptography.CryptoStream(ms, encryptor,
System.Security.Cryptography.CryptoStreamMode.Write))
{
using (var swEncrypt = new System.IO.StreamWriter(csEncrypt))
{
swEncrypt.Write(plaintext);
}
encrypted = ms.ToArray();
this.lista = new List<string>();
strResult = Convert.ToBase64String(encrypted);
}
}
}
return strResult;
}
catch (System.Exception ex)
{
return ex.Message;
}
}
}
}
@page "/View"
@using blazor3.providers;
@inject ListData dataService;
<h1>Encrypt and Decrypt</h1>
<form>
<div class="form-group">
<label for="exampleFormControlInput1">Key</label>
<input type="text" class='form-control @((strkey.Length==16? "" : "border border-danger"))'
maxlength="16" @bind="strkey" id="exampleFormControlInput1"
placeholder="key 16 chars" />
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<button type="button" class="btn btn-success m-3" disabled="@(strkey.Length!=16)" @onclick=EncryptData>Encrypt</button>
</div>
<div class="col-md-6">
<button type="button" class="btn btn-warning m-3" disabled="@(strkey.Length!=16)" @onclick=DecryptData>Decrypt</button>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6 border bg-success">
<div class="card-body">
<label for="exampleFormControlTextarea1">Plain Text</label>
<textarea class="form-control m-3" @bind="plainText" id="exampleFormControlTextarea1" rows="3"></textarea>
</div>
</div>
<div class="form-group col-md-6 border bg-warning">
<div class="card-body">
<label for="exampleFormControlTextarea2">Encrypted Text</label>
<textarea class="form-control m-3" @bind="encryptedText" id="exampleFormControlTextarea2" rows="3"></textarea>
</div>
</div>
</div>
</form>
@code{
public string strkey{get; set;} = "1234567890123456";
public string plainText{get; set;} = "";
public string encryptedText{get; set;} = "";
void EncryptData(){
encryptedText=dataService.Encrypt(plainText,strkey);
}
void DecryptData(){
plainText=dataService.Decrypt(encryptedText,strkey);
}
}
using System.Collections.Generic;
namespace blazor3.providers
{
public interface IListData
{
List<string> lista { get; set; }
string Encrypt(string plaintext, string strkey);
string Decrypt(string plaintext, string strkey);
}
}
<div class="top-row pl-4 navbar navbar-dark">
<a class="navbar-brand" href="">blazor3</a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>
<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
<ul class="nav flex-column">
<li class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="counter">
<span class="oi oi-plus" aria-hidden="true"></span> Counter
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="fetchdata">
<span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
</NavLink>
</li>
<!--Add this line-->
<li class="nav-item px-3">
<NavLink class="nav-link" href="View">
<span class="oi oi-list-rich" aria-hidden="true"></span> View
</NavLink>
</li>
</ul>
</div>
@code {
private bool collapseNavMenu = true;
private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;
private void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
services.AddSingleton<ListData>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.