Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored StegoEntry, added blazor example #10

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactored StegoEntry
  • Loading branch information
paw3lx committed Mar 11, 2024
commit 8ade3be8847212844a853ad11a56d43ecf61820b
12 changes: 12 additions & 0 deletions examples/StegoBlazorApp/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
70 changes: 70 additions & 0 deletions examples/StegoBlazorApp/Pages/Decode.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
@page "/decode"
@using System.IO
@using Microsoft.AspNetCore.Components.Forms

<div class="container">
<div class="row">
<InputFile OnChange="HandleSelected" accept=".jpg,.jpeg,.png,.bmp" />
</div>
<div class="row">
<div class="col-md">
@if (imageBase64 != null)
{
<div>
<img src="@imageBase64" />
</div>
<div class="d-flex justify-content-center mt-2">
<button class="btn btn-primary" @onclick="Decrypt">Decrypt</button>
</div>

}
</div>
<div class="col-md">
<div class="h-100 d-flex flex-column align-items-center justify-content-center">
@if (secret != null)
{
<div>
<p>Your secret</p>
</div>
<input class="form-control" type="text" value="@secret" readonly>
}
</div>
</div>
</div>

</div>

@code {
private string? imageBase64;
private byte[]? imageBytes;

private string? secret;

private async Task HandleSelected(InputFileChangeEventArgs e)
{
var imageFile = e.File;
if (imageFile != null)
{
using MemoryStream ms = new();
using var stream = imageFile.OpenReadStream();
await stream.CopyToAsync(ms);
imageBytes = ms.ToArray();
imageBase64 = $"data:image/png;base64,{Convert.ToBase64String(imageBytes)}";
}
}

private void Decrypt()
{
if (imageBytes == null)
{
return;
}

using var stream = new MemoryStream(imageBytes);
using var stego = new StegoCore.Stego(stream);
var result = stego.Decode(StegoCore.Algorithms.AlgorithmEnum.Lsb);

// convert result to string
secret = System.Text.Encoding.UTF8.GetString(result);
}
}
93 changes: 93 additions & 0 deletions examples/StegoBlazorApp/Pages/Embed.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
@page "/embed"
@using System.IO
@using Microsoft.AspNetCore.Components.Forms
@inject IJSRuntime JS

<div class="container">
<div class="row">
<InputFile OnChange="HandleSelected" accept=".jpg,.jpeg,.png,.bmp" />
</div>
<div class="row">
<div class="col-md">
@if (imageBase64 != null)
{
<div>
<img src="@imageBase64" />
</div>
<div class="d-flex justify-content-center mt-2">
<input type="text" @bind="secret" id="secret" placeholder="Enter your secret" class="m-2" />
<button class="btn btn-primary m-2" @onclick="Encrypt">Encrypt</button>
</div>

}
</div>
<div class="col-md">
@if (imageEncrypted != null)
{
<img src="@imageEncrypted" />
<div class="d-flex justify-content-center mt-2">
<button class="btn btn-primary" @onclick="Download">Download</button>
</div>
}
</div>
</div>

</div>

@code {
private string? imageBase64;
private byte[]? imageBytes;
private string? secret;
private byte[]? imageEncryptedBytes;
private string? imageEncrypted;


private async Task HandleSelected(InputFileChangeEventArgs e)
{
var imageFile = e.File;
if (imageFile != null)
{
using MemoryStream ms = new();
var format = "image/png";
using var stream = imageFile.OpenReadStream();
await stream.CopyToAsync(ms);
imageBytes = ms.ToArray();
imageBase64 = $"data:{format};base64,{Convert.ToBase64String(imageBytes)}";
}
}

private async Task Encrypt()
{
if (secret is null || imageBytes is null)
{
return;
}

var secretData = System.Text.Encoding.UTF8.GetBytes(secret);
using (var stream = new MemoryStream(imageBytes))
{
using (var stego = new StegoCore.Stego(stream))
{
var imageWithSecret = stego.Embed(new StegoCore.Core.SecretData(secretData), StegoCore.Algorithms.AlgorithmEnum.Lsb);
using var outputStream = new MemoryStream();
await imageWithSecret.SaveAsync(outputStream, new SixLabors.ImageSharp.Formats.Png.PngEncoder());
imageEncryptedBytes = outputStream.ToArray();
imageEncrypted = $"data:image/png;base64,{Convert.ToBase64String(imageEncryptedBytes)}";
}
}
}

private async Task Download()
{
if (imageEncryptedBytes is null)
{
return;
}

var stream = new MemoryStream(imageEncryptedBytes);

using var streamRef = new DotNetStreamReference(stream: stream);

await JS.InvokeVoidAsync("downloadFileFromStream", "encrypted.png", streamRef);
}
}
42 changes: 42 additions & 0 deletions examples/StegoBlazorApp/Pages/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@page
@model StegoBlazorApp.Pages.ErrorModel

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Error</title>
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true" />
</head>

<body>
<div class="main">
<div class="content px-4">
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
</div>
</div>
</body>

</html>
26 changes: 26 additions & 0 deletions examples/StegoBlazorApp/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace StegoBlazorApp.Pages;

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

private readonly ILogger<ErrorModel> _logger;

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
7 changes: 7 additions & 0 deletions examples/StegoBlazorApp/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@page "/"

<PageTitle>Index</PageTitle>

<h1>Hello from StegoCore!</h1>

This example show how to use StegoCore library
47 changes: 47 additions & 0 deletions examples/StegoBlazorApp/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
@page "/"
@using Microsoft.AspNetCore.Components.Web
@namespace StegoBlazorApp.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="StegoBlazorApp.styles.css" rel="stylesheet" />
<link rel="icon" type="image/png" href="favicon.png"/>
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
</head>
<body>
<component type="typeof(App)" render-mode="ServerPrerendered" />

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.server.js"></script>
<script>
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
}
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions examples/StegoBlazorApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddHttpClient();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}


app.UseStaticFiles();

app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
28 changes: 28 additions & 0 deletions examples/StegoBlazorApp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:14315",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5051",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
19 changes: 19 additions & 0 deletions examples/StegoBlazorApp/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@inherits LayoutComponentBase

<PageTitle>StegoBlazorApp</PageTitle>

<div class="page">
<div class="sidebar">
<NavMenu />
</div>

<main>
<div class="top-row px-4">
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
</div>

<article class="content px-4">
@Body
</article>
</main>
</div>
Loading
Loading