feat: add ImageDataEncoder — LSB steganography encoder

Hides text messages in PNG images using least significant bit
steganography with configurable RGB channel selection.
This commit is contained in:
2026-05-04 12:51:59 +02:00
parent 2114c3743b
commit 90d82dbcc4
4 changed files with 10124 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#include <cstdlib>
#include <locale>
#include <codecvt>
#include <windows.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
int width, height, channels;
std::string inputPath, outputPath, message;
bool useR, useG, useB;
unsigned char* data;
std::string readPath(const std::string& prompt) {
std::string path;
std::cout << prompt;
std::getline(std::cin, path);
if (path.size() >= 2 && path.front() == '"' && path.back() == '"') {
path = path.substr(1, path.size() - 2);
}
return path;
}
void getInput() {
inputPath = readPath("Enter source image path: ");
outputPath = readPath("Enter output image path (.png): ");
std::string channelInput;
std::cout << "Channels to use (e.g. RGB, RG, B): ";
std::getline(std::cin, channelInput);
useR = channelInput.find('R') != std::string::npos || channelInput.find('r') != std::string::npos;
useG = channelInput.find('G') != std::string::npos || channelInput.find('g') != std::string::npos;
useB = channelInput.find('B') != std::string::npos || channelInput.find('b') != std::string::npos;
if (!useR && !useG && !useB) {
throw std::invalid_argument("At least one channel (R, G, or B) must be selected.");
}
std::string sourceChoice;
std::cout << "Message from (T)ext input or (F)ile? ";
std::getline(std::cin, sourceChoice);
if (sourceChoice == "F" || sourceChoice == "f") {
std::string filePath = readPath("Enter message file path: ");
int wlen = MultiByteToWideChar(CP_ACP, 0, filePath.c_str(), -1, nullptr, 0);
std::wstring wpath(wlen - 1, L'\0');
MultiByteToWideChar(CP_ACP, 0, filePath.c_str(), -1, &wpath[0], wlen);
std::ifstream file(wpath);
if (!file.is_open()) {
throw std::runtime_error("Failed to open message file: " + filePath);
}
message.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
} else {
std::cout << "Enter message: ";
std::getline(std::cin, message);
}
if (message.empty()) {
throw std::invalid_argument("Message is empty.");
}
}
void loadImage() {
data = stbi_load(inputPath.c_str(), &width, &height, &channels, 0);
if (!data) {
throw std::runtime_error("Failed to load image: " + inputPath);
}
if (channels < 3) {
stbi_image_free(data);
throw std::runtime_error("Image must have at least 3 channels (RGB). Found: " + std::to_string(channels));
}
int totalPixels = width * height;
int activeChannels = useR + useG + useB;
int availableBits = (totalPixels - 1) * activeChannels;
int requiredBits = (static_cast<int>(message.size()) + 1) * 8;
if (requiredBits > availableBits) {
stbi_image_free(data);
throw std::runtime_error(
"Message too large. Need " + std::to_string(requiredBits) +
" bits, but only " + std::to_string(availableBits) + " available."
);
}
std::cout << "Image loaded: " << width << "x" << height << " (" << channels << " channels)\n";
std::cout << "Capacity: " << availableBits << " bits available, " << requiredBits << " bits needed\n";
}
void writeChannelHeader() {
data[0] = useR ? (data[0] | 1) : (data[0] & ~1);
data[1] = useG ? (data[1] | 1) : (data[1] & ~1);
data[2] = useB ? (data[2] | 1) : (data[2] & ~1);
}
void encodeBit(int channelOffset, int base, int& bitIndex) {
int bytePos = bitIndex / 8;
int bitPos = 7 - (bitIndex % 8);
int bit = (message[bytePos] >> bitPos) & 1;
data[base + channelOffset] = (data[base + channelOffset] & ~1) | bit;
bitIndex++;
}
void encodeMessage() {
message.push_back('\0');
int totalPixels = width * height;
int bitIndex = 0;
int totalBits = static_cast<int>(message.size()) * 8;
for (int pixel = 1; pixel < totalPixels && bitIndex < totalBits; pixel++) {
int base = pixel * channels;
if (useR && bitIndex < totalBits) encodeBit(0, base, bitIndex);
if (useG && bitIndex < totalBits) encodeBit(1, base, bitIndex);
if (useB && bitIndex < totalBits) encodeBit(2, base, bitIndex);
}
std::cout << "Encoded " << bitIndex << " bits into image.\n";
}
void saveImage() {
int result = stbi_write_png(outputPath.c_str(), width, height, channels, data, width * channels);
stbi_image_free(data);
if (!result) {
throw std::runtime_error("Failed to write output image: " + outputPath);
}
std::cout << "Message successfully hidden in: " << outputPath << "\n";
}
int main() {
try {
getInput();
loadImage();
writeChannelHeader();
encodeMessage();
saveImage();
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
std::cin.get();
return EXIT_FAILURE;
}
std::cin.get();
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,139 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>18.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{a1b2c3d4-e5f6-7890-abcd-ef1234567890}</ProjectGuid>
<RootNamespace>ImageDataEncoder</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="ImageDataEncoder.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="stb_image.h" />
<ClInclude Include="stb_image_write.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

8104
ImageDataEncoder/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff