You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
ASYD/ASYD_Cryptograhpy/SW01-XTEA-Cipher/encrypt.c

90 lines
2.5 KiB

#include <stdint.h> /* Funktionsbibliothek: Integer */
#include <stdlib.h> /* Funktionsbibliothek: Hilfsfunktionen */
#include <stdio.h> /* Funktionsbibliothek: Standard Ein- Ausgabe */
#include "main.h"
#include "encrypt.h"
// disable deprecated warning
#pragma warning(disable : 4996)
void encipher(uint32_t num_cycles, uint32_t v[2], uint32_t const k[4])
{
unsigned int i;
const uint32_t delta = 0x9E3779B9; // decipher changes:
uint32_t v0 = v[0], v1 = v[1], sum = 0; // sum = delta * num_cycles;
for (i = 0; i < num_cycles; i++)
{
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); //-=
sum += delta; //-=, exchange lines above and below
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]); //-=
}
v[0] = v0;
v[1] = v1;
}
int encrypt(char *LoadFName, char *SaveFName, uint32_t key[4])
{
int ErrorCode = NO_ERROR; /* Error-Code-Variable */
FILE *LoadFile; /* File welches geladen wird */
FILE *SaveFile; /* File welches gespeichert wird */
char block[2] = {0};
uint32_t block_uint32[2] = {0};
uint32_t num_cycles = 16;
/* Kontrolliertes oeffnen der Files */
if ((LoadFile = fopen(LoadFName, "rb")) == NULL)
ErrorCode = ERROR_OPEN_LOAD_FILE;
if ((SaveFile = fopen(SaveFName, "wb")) == NULL)
ErrorCode = ERROR_OPEN_SAVE_FILE;
/* Alle Filezugriffe moeglich ? */
if (ErrorCode == NO_ERROR)
{
do
{
// reset vars
block[0] = 0;
block[1] = 0;
block_uint32[0] = 0;
block_uint32[1] = 0;
block[0] = fgetc(LoadFile);
// if eof => quit
if(block[0] == EOF)
{
break;
}
block[1] = fgetc(LoadFile);
// if eof => replace with space to get previous character within encryption
if(block[1] == EOF)
{
block[1] = ' ';
}
// convert
block_uint32[0] = (uint32_t)block[0];
block_uint32[1] = (uint32_t)block[1];
encipher(num_cycles, block_uint32, key);
// write encrypted block to file
int x = 0;
while (x < 2)
{
fprintf(SaveFile, "%u ", block_uint32[x]);
x++;
}
} while (!feof(LoadFile));
}
/* Wenn Orignalfile geoffnet werden konnte */
if (ErrorCode != ERROR_OPEN_LOAD_FILE)
fclose(LoadFile);
/* Wenn Ausgabefile geschrieben werden konnte */
if (ErrorCode != ERROR_OPEN_SAVE_FILE)
fclose(SaveFile);
return (ErrorCode); /* Rueckgabe des Fehlercodes */
}