parent
a1e00b5716
commit
5c24e160bc
@ -0,0 +1,141 @@ |
||||
<?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>16.0</VCProjectVersion> |
||||
<Keyword>Win32Proj</Keyword> |
||||
<ProjectGuid>{101384c8-6d89-428a-a336-d82a488483b2}</ProjectGuid> |
||||
<RootNamespace>SW02XTEACBC</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>v143</PlatformToolset> |
||||
<CharacterSet>Unicode</CharacterSet> |
||||
</PropertyGroup> |
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> |
||||
<ConfigurationType>Application</ConfigurationType> |
||||
<UseDebugLibraries>false</UseDebugLibraries> |
||||
<PlatformToolset>v143</PlatformToolset> |
||||
<WholeProgramOptimization>true</WholeProgramOptimization> |
||||
<CharacterSet>Unicode</CharacterSet> |
||||
</PropertyGroup> |
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> |
||||
<ConfigurationType>Application</ConfigurationType> |
||||
<UseDebugLibraries>true</UseDebugLibraries> |
||||
<PlatformToolset>v143</PlatformToolset> |
||||
<CharacterSet>Unicode</CharacterSet> |
||||
</PropertyGroup> |
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> |
||||
<ConfigurationType>Application</ConfigurationType> |
||||
<UseDebugLibraries>false</UseDebugLibraries> |
||||
<PlatformToolset>v143</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> |
||||
</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> |
||||
</ClCompile> |
||||
<Link> |
||||
<SubSystem>Console</SubSystem> |
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding> |
||||
<OptimizeReferences>true</OptimizeReferences> |
||||
<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> |
||||
</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> |
||||
</ClCompile> |
||||
<Link> |
||||
<SubSystem>Console</SubSystem> |
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding> |
||||
<OptimizeReferences>true</OptimizeReferences> |
||||
<GenerateDebugInformation>true</GenerateDebugInformation> |
||||
</Link> |
||||
</ItemDefinitionGroup> |
||||
<ItemGroup> |
||||
<ClCompile Include="main.c" /> |
||||
<ClCompile Include="XTEA-CBC.c" /> |
||||
<ClCompile Include="XTEA.c" /> |
||||
</ItemGroup> |
||||
<ItemGroup> |
||||
<ClInclude Include="XTEA-CBC.h" /> |
||||
<ClInclude Include="XTEA.h" /> |
||||
</ItemGroup> |
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> |
||||
<ImportGroup Label="ExtensionTargets"> |
||||
</ImportGroup> |
||||
</Project> |
||||
@ -0,0 +1,37 @@ |
||||
/**
|
||||
*--------------------------------------------------------------------\n |
||||
* HSLU T&A Hochschule Luzern Technik+Architektur \n |
||||
*--------------------------------------------------------------------\n |
||||
* |
||||
* \brief model solution for ASYD assignment crypto 01 |
||||
* \file |
||||
* \author SIMON FREI, stefano.nicora@hslu.ch |
||||
* \date 04.10.22 |
||||
* |
||||
*-------------------------------------------------------------------- |
||||
*/ |
||||
|
||||
#include <stdint.h> |
||||
#include "XTEA-CBC.h" |
||||
#include "XTEA.h" |
||||
|
||||
#define INITIALIZATION_VECTOR { 'COFE', 'SIMI' } |
||||
|
||||
void encipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) { |
||||
static uint32_t v_prev[2] = INITIALIZATION_VECTOR; |
||||
v[0] = v[0] ^ v_prev[0]; |
||||
v[1] = v[1] ^ v_prev[1]; |
||||
encipher(num_cycles, v, k); |
||||
v_prev[0] = v[0]; |
||||
v_prev[1] = v[1]; |
||||
} |
||||
|
||||
void decipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) { |
||||
static uint32_t v_prev[2] = INITIALIZATION_VECTOR; |
||||
uint32_t v_input[2] = { v[0], v[1] }; |
||||
decipher(num_cycles, v, k); |
||||
v[0] = v[0] ^ v_prev[0]; |
||||
v[1] = v[1] ^ v_prev[1]; |
||||
v_prev[0] = v_input[0]; |
||||
v_prev[1] = v_input[1]; |
||||
} |
||||
@ -0,0 +1,25 @@ |
||||
/**
|
||||
*--------------------------------------------------------------------\n |
||||
* HSLU T&A Hochschule Luzern Technik+Architektur \n |
||||
*--------------------------------------------------------------------\n |
||||
* |
||||
* \brief model solution for ASYD assignment crypto 01 |
||||
* \file |
||||
* \author Stefano Nicora, stefano.nicora@hslu.ch |
||||
* \date 04.10.22 |
||||
* |
||||
*-------------------------------------------------------------------- |
||||
*/ |
||||
|
||||
#ifndef XTEA_CBC_H_ |
||||
#define XTEA_CBC_H_ |
||||
#define _CRT_SECURE_NO_WARININGS |
||||
#pragma warning(disable:4996) |
||||
|
||||
#include <stdint.h> |
||||
|
||||
void encipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]); |
||||
|
||||
void decipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]); |
||||
|
||||
#endif // !XTEA_CBC_H_
|
||||
@ -0,0 +1,39 @@ |
||||
/**
|
||||
*--------------------------------------------------------------------\n |
||||
* HSLU T&A Hochschule Luzern Technik+Architektur \n |
||||
*--------------------------------------------------------------------\n |
||||
* |
||||
* \brief model solution for ASYD assignment crypto 01 |
||||
* \file |
||||
* \author Stefano Nicora, stefano.nicora@hslu.ch |
||||
* \date 04.10.22 |
||||
* |
||||
*-------------------------------------------------------------------- |
||||
*/ |
||||
|
||||
#include <stdint.h> |
||||
#include "XTEA.h" |
||||
|
||||
void encipher(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) { |
||||
unsigned int i; |
||||
const uint32_t delta = 0x9E3779B9; |
||||
uint32_t v0 = v[0], v1 = v[1], sum = 0; |
||||
for (i = 0; i < num_cycles; i++) { |
||||
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); |
||||
sum += delta; |
||||
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]); |
||||
} |
||||
v[0] = v0; v[1] = v1; |
||||
} |
||||
|
||||
void decipher(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) { |
||||
unsigned int i; |
||||
const uint32_t delta = 0x9E3779B9; |
||||
uint32_t v0 = v[0], v1 = v[1], sum = delta * num_cycles; |
||||
for (i = 0; i < num_cycles; i++) { |
||||
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]); |
||||
sum -= delta; |
||||
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); |
||||
} |
||||
v[0] = v0; v[1] = v1; |
||||
} |
||||
@ -0,0 +1,26 @@ |
||||
/**
|
||||
*--------------------------------------------------------------------\n |
||||
* HSLU T&A Hochschule Luzern Technik+Architektur \n |
||||
*--------------------------------------------------------------------\n |
||||
* |
||||
* \brief model solution for ASYD assignment crypto 01 |
||||
* \file |
||||
* \author Stefano Nicora, stefano.nicora@hslu.ch |
||||
* \date 04.10.22 |
||||
* |
||||
*-------------------------------------------------------------------- |
||||
*/ |
||||
|
||||
#ifndef XTEA_H_ |
||||
#define XTEA_H_ |
||||
#define _CRT_SECURE_NO_WARININGS |
||||
#pragma warning(disable:4996) |
||||
|
||||
#include <stdint.h> |
||||
|
||||
void encipher(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]); |
||||
|
||||
void decipher(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]); |
||||
|
||||
#endif // !XTEA_H_
|
||||
|
||||
@ -0,0 +1,163 @@ |
||||
/**
|
||||
*--------------------------------------------------------------------\n |
||||
* HSLU T&A Hochschule Luzern Technik+Architektur \n |
||||
*--------------------------------------------------------------------\n |
||||
* |
||||
* \brief model solution for ASYD assignment crypto 01 |
||||
* \file |
||||
* \author Stefano Nicora, stefano.nicora@hslu.ch |
||||
* \date 12.10.22 |
||||
* |
||||
*-------------------------------------------------------------------- |
||||
*/ |
||||
|
||||
#include <stdint.h> |
||||
#include <stdlib.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include "XTEA-CBC.h" |
||||
|
||||
void encrypt(uint32_t key[4], uint8_t numberOfCycles); |
||||
void decrypt(uint32_t key[4], uint8_t numberOfCycles); |
||||
|
||||
/* source: https://en.wikipedia.org/wiki/XTEA */ |
||||
|
||||
/* filenames as of this code:
|
||||
* plaintext.txt => holds the message you want to encrypt |
||||
* encrypted.cip => holds the encrypted message |
||||
* decrypted.txt => holds the decrypted output from "encrypted.cip" |
||||
*/ |
||||
|
||||
void main(void) { |
||||
uint32_t key[4] = { 1, 3, 1, 2}; |
||||
encrypt(key, 32); |
||||
decrypt(key, 32); |
||||
} |
||||
|
||||
void encrypt(uint32_t key[4], uint8_t numberOfCycles) { |
||||
|
||||
/* duplicate code that hasn't been moved to a separate function for readability reasons */ |
||||
/* -------------------------------------------------------------------- */ |
||||
|
||||
/* variables */ |
||||
uint32_t amountOfCharsInFile = 0; |
||||
/* https://www.programiz.com/c-programming/c-file-input-output */ |
||||
FILE* fileHandle = fopen("plaintext.txt", "r"); |
||||
if (fileHandle == NULL) { |
||||
return; |
||||
} |
||||
|
||||
/* get total number of characters in file to encrypt */ |
||||
while (getc(fileHandle) != EOF) { |
||||
amountOfCharsInFile++; |
||||
} |
||||
|
||||
/* allocate enough memory for the whole file to be read */ |
||||
char* inputText = (char*)calloc(amountOfCharsInFile + 8, sizeof(char)); /* add 8 for extra padding in case the file contains some sort of rest in regards to amountOfCharsInFile % 8 */ |
||||
if (inputText == NULL) { |
||||
return; |
||||
} |
||||
|
||||
/* close and reopen file to start at the beginning of the file again */ |
||||
fclose(fileHandle); |
||||
fileHandle = fopen("plaintext.txt", "r"); |
||||
if (fileHandle == NULL) { |
||||
return; |
||||
} |
||||
|
||||
/* read the whole file into memory */ |
||||
for (uint32_t i = 0; i < amountOfCharsInFile; i++) { |
||||
inputText[i] = getc(fileHandle); |
||||
} |
||||
fclose(fileHandle); |
||||
/* -------------------------------------------------------------------- */ |
||||
|
||||
/* loop through the whole array to encrypt the whole message
|
||||
This happens two blocks of 32bit (= 8 characters) at a time |
||||
Hence the increase of "i" by 8 |
||||
*/ |
||||
for (uint32_t i = 0; i < amountOfCharsInFile; i += 8) { /* + 8 to compensate for the requested element size of the encipher function (32 bit) */ |
||||
/* dereferencing and forwarding the address of the key ensures,
|
||||
* that we don't ready from a random address in memory. Rookie mistakes happen :) */ |
||||
encipher_CBC(numberOfCycles, &(inputText[i]), &(*key)); |
||||
} |
||||
|
||||
/* generate new file where encryption is stored */ |
||||
fileHandle = fopen("encrypted.cip", "w"); |
||||
if (fileHandle == NULL) { |
||||
return; |
||||
} |
||||
/* fill new file with content */ |
||||
/* to get the extra character padding we might unintentionally drop
|
||||
* we need to read up to a multiple of 8 (32 bit) |
||||
*/ |
||||
for (uint32_t i = 0; i < (amountOfCharsInFile + (8-(amountOfCharsInFile%8))); i++) { |
||||
putc(inputText[i], fileHandle); |
||||
} |
||||
fclose(fileHandle); |
||||
free(inputText); |
||||
} |
||||
|
||||
void decrypt(uint32_t key[4], uint8_t numberOfCycles) { |
||||
|
||||
/* duplicate code that hasn't been moved to a separate function for readability reasons */ |
||||
/* -------------------------------------------------------------------- */ |
||||
|
||||
/* variables */ |
||||
uint32_t amountOfCharsInFile = 0; |
||||
|
||||
/* https://www.programiz.com/c-programming/c-file-input-output */ |
||||
FILE* fileHandle = fopen("encrypted.cip", "r"); |
||||
if (fileHandle == NULL) { |
||||
return; |
||||
} |
||||
|
||||
/* get total number of characters in file to decrypt */ |
||||
while (getc(fileHandle) != EOF) { |
||||
amountOfCharsInFile++; |
||||
} |
||||
|
||||
/* allocate memory for the whole file to be read */ |
||||
char* inputText = (char*)calloc(amountOfCharsInFile + 10, sizeof(char)); /* add 10 for extra padding */ |
||||
if (inputText == NULL) { |
||||
return; |
||||
} |
||||
|
||||
/* close and reopen file to start at the beginning of the file again */ |
||||
fclose(fileHandle); |
||||
fileHandle = fopen("encrypted.cip", "r"); |
||||
if (fileHandle == NULL) { |
||||
return; |
||||
} |
||||
|
||||
/* read the whole file into memory */ |
||||
for (uint32_t i = 0; i < amountOfCharsInFile; i++) { |
||||
inputText[i] = getc(fileHandle); |
||||
} |
||||
fclose(fileHandle); |
||||
|
||||
/* -------------------------------------------------------------------- */ |
||||
|
||||
/* loop through the whole array to encrypt the whole message
|
||||
This happens two blocks of 32bit (= 4 characters) at a time |
||||
Hence the increase of "i" by 8 |
||||
*/ |
||||
|
||||
for (uint32_t i = 0; i < amountOfCharsInFile; i += 8) { /* i += 8*/ |
||||
/* dereferencing and forwarding the address of the key ensures,
|
||||
* that we don't ready from a random address in memory. Rookie mistakes happen :) */ |
||||
decipher_CBC(numberOfCycles, &(inputText[i]), &(*key)); |
||||
} |
||||
|
||||
/* generate new file where the decryption is stored */ |
||||
fileHandle = fopen("decrypted.txt", "w"); |
||||
if (fileHandle == NULL) { |
||||
return; |
||||
} |
||||
/* fill new file with content */ |
||||
for (uint32_t i = 0; i < amountOfCharsInFile; i++) { |
||||
putc(inputText[i], fileHandle); |
||||
} |
||||
fclose(fileHandle); |
||||
free(inputText); |
||||
} |
||||
Binary file not shown.
Loading…
Reference in new issue