Skip to content

Commit bbb2022

Browse files
committed
Improved error handling for invalid or corrupt png input images
1 parent 1f4ddea commit bbb2022

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

Stegosaurus/Core/AppRunner.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Runtime.InteropServices;
21
using Microsoft.Extensions.Logging;
32
using SixLabors.ImageSharp;
43
using SixLabors.ImageSharp.PixelFormats;
@@ -10,7 +9,6 @@ namespace Stegosaurus.Core
109
{
1110
public class AppRunner
1211
{
13-
1412
private readonly ILogger<AppRunner> _logger;
1513

1614
public AppRunner(ILogger<AppRunner> logger)
@@ -60,7 +58,6 @@ public string ResolveOutfilePath(string outfilePath, bool requirePng)
6058
return resolvedOutFilePath;
6159
}
6260

63-
6461
private string GenerateOutfilePath(string filePath, bool isEncrypt)
6562
{
6663
string fileName = Path.GetFileNameWithoutExtension(filePath);
@@ -83,7 +80,6 @@ private void SaveEncodedFile(Image<Rgba32> image, string outfilePath)
8380
}
8481
}
8582

86-
8783
private void SaveDecodedFile(string outfilePath, string message)
8884
{
8985
try
@@ -113,14 +109,15 @@ public void RunEncrypt(string filePath, string password, string message, string?
113109
outfilePath = GenerateOutfilePath(filePath, true);
114110
}
115111
outfilePath = ResolveOutfilePath(outfilePath, true);
116-
Image<Rgba32>? encodedImage = null;
117-
118-
var encryptedMsg = AesCryptoService.Encrypt(password, message);
119-
var encoder = new LsbEncoder(filePath, encryptedMsg, password);
120112

121113
try
122114
{
123-
encodedImage = encoder.Encode();
115+
var encryptedMsg = AesCryptoService.Encrypt(password, message);
116+
var encoder = new LsbEncoder(filePath, encryptedMsg, password);
117+
Image<Rgba32> encodedImage = encoder.Encode();
118+
SaveEncodedFile(encodedImage, outfilePath);
119+
_logger.LogInformation("Message successfully encrypted and encoded.");
120+
_logger.LogInformation("Saved outfile to: {Outfile}", outfilePath);
124121
}
125122
catch (InvalidOperationException)
126123
{
@@ -129,10 +126,20 @@ public void RunEncrypt(string filePath, string password, string message, string?
129126
}
130127
catch (TimeoutException)
131128
{
132-
_logger.LogError("Encoding is taking too long. Try using a larger image.");
129+
_logger.LogError("Encoding is taking too long. This means the image file is too small, and the PRNG (pseudo-random number generator) is taking too long to find available bits. Try using a larger image.");
130+
Environment.Exit(1);
131+
}
132+
catch (UnknownImageFormatException)
133+
{
134+
_logger.LogError("Can not load image file. Ensure you are using a PNG file. If you are using a PNG then your image may be corrupted.");
135+
Environment.Exit(1);
136+
}
137+
catch (Exception ex)
138+
{
139+
_logger.LogError("An unexpected error has occurred: {Message}", ex.Message);
133140
Environment.Exit(1);
134141
}
135-
SaveEncodedFile(encodedImage, outfilePath);
142+
136143
}
137144

138145
public void RunDecrypt(string filePath, string password, string? outfilePath)
@@ -145,20 +152,24 @@ public void RunDecrypt(string filePath, string password, string? outfilePath)
145152
outfilePath = ResolveOutfilePath(outfilePath, false);
146153
password = ResolvePassword(password);
147154

148-
var decoder = new LsbDecoder(filePath, password);
149-
byte[]? decodedMessage = null;
150155
try
151156
{
152-
decodedMessage = decoder.Decode();
153-
157+
var decoder = new LsbDecoder(filePath, password);
158+
byte[] decodedMessage = decoder.Decode();
159+
string message = AesCryptoService.Decrypt(password, decodedMessage);
160+
SaveDecodedFile(outfilePath, message);
161+
_logger.LogInformation("Message successfully saved to: {Outfile}", outfilePath);
162+
}
163+
catch (UnknownImageFormatException)
164+
{
165+
_logger.LogError("Can not load image file. Ensure you are using a PNG file. If you are using a PNG then your image may be corrupted.");
166+
Environment.Exit(1);
154167
}
155168
catch (Exception ex)
156169
{
157170
_logger.LogError("Decoding failed due to the following exception: {Message}", ex.Message);
158171
Environment.Exit(1);
159172
}
160-
string message = AesCryptoService.Decrypt(password, decodedMessage);
161-
SaveDecodedFile(outfilePath, message);
162173
}
163174
}
164175
}

Stegosaurus/Stego/LsbEncoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public LsbEncoder(string filePath, byte[] ciphertext, string password)
1414
{
1515
_ciphertext = ciphertext;
1616
_prng = StegoConstants.Prng(password);
17-
_image = Image.Load<Rgba32>(filePath);
17+
_image = Image.Load<Rgba32>(filePath); // May throw UnknownImageFormatException if image is invalid or not a PNG
1818
}
1919

2020
private byte[] PrefixCiphertext()

0 commit comments

Comments
 (0)