Kommentare zu kleinem Programm

Maledictus

FreeBSD ftw
Hi Leute,
ich hab aus lauter Spass an der Freude mal dieses kleine Programm geschmiedet.
Man kann es benutzen um Daten zu verschlüsseln.
Vorausgesetzt die Schlüsseldatei ist mindestens genauso groß wie die zu verschlüsselnden Daten, nennt man diese Verschlüsselungsart One-Time-Pad. Es ist die einzige Verschlüsselungsmethode, die mathematisch beweisbar 100% sicher ist. (Natürlich nur solange niemand den Schlüssel in die Hände bekommt :) )

Lasst mal euren Senf dazu ab, was meint ihr ist gut, was nicht so gut?
Benutzen dürft ihr es natürlich auch, steht schliesslich unter Beer-Ware Lizenz ;)

gruss
Male

P.S. leider hab ich die formatierung etwas zerschossen :(

Code:
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <skalla.raabjorn@gmx.de> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Skalla
 * ----------------------------------------------------------------------------
 */

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/mman.h>

int
main(int argc, char *argv[])
{

        int keyFile;
        int inputBufferOffset = 0;
        int inputBufferIndex = 0;
        int keyLength = 0;
        int keyIndex = 0;

        char *inputBuffer = NULL;
        char *keyBuffer = NULL;

        ssize_t nRead, nWrite;
        size_t inputBufferSize;

        struct stat fileInfo;

        if (argc != 2) {
           errx(EXIT_FAILURE, "usage: xor keyfile");
        }

        /* read the keyfile in keyBuffer */
        keyFile = open(argv[1], O_RDONLY);
        if (keyFile < 0) {
                err(EXIT_FAILURE, "keyfile");
        }
        if (fstat(keyFile, &fileInfo)) {
                err(EXIT_FAILURE, "keyfile");
        }
        keyLength = fileInfo.st_size;
        if (keyLength < 1) {
           errx(EXIT_FAILURE, "keyfile size is zero");
        }
        keyBuffer = mmap(0, keyLength, PROT_READ, 0, keyFile, 0);
        if (keyBuffer == MAP_FAILED) {
           err(EXIT_FAILURE, "keyBuffer");
        }
        close(keyFile);

        if (fstat(STDOUT_FILENO, &fileInfo)) {
           err(EXIT_FAILURE, "stdout");
        }
        inputBufferSize = MAX(fileInfo.st_blksize, 1024);
        inputBuffer = malloc(inputBufferSize);
        if (inputBuffer == NULL) {
           err(EXIT_FAILURE, "buffer");
        }
        while ((nRead = read(STDIN_FILENO, inputBuffer, inputBufferSize)) > 0) {

           /* xor'ing inputBuffer with keyBuffer */
           for (inputBufferIndex = 0; inputBufferIndex < nRead; inputBufferIndex++) {
                inputBuffer[inputBufferIndex] ^= keyBuffer[keyIndex];
                keyIndex++;
                if (keyIndex == keyLength) {
                 keyIndex = 0;
                }
           }

           /* writing the inputBuffer to stdout */
           for (inputBufferOffset = 0; nRead; nRead -= nWrite, inputBufferOffset += nWrite) {
                if ((nWrite = write(STDOUT_FILENO, inputBuffer + inputBufferOffset, nRead)) < 0) {
                 err(EXIT_FAILURE, "stdout");
                }
           }
        }
        if (nRead < 0) {
           errx(EXIT_FAILURE, "stdin");
        }
        if (munmap(keyBuffer, keyLength)) {
           err(EXIT_FAILURE, "keyBuffer");
        }
        return EXIT_SUCCESS;
}
 
Zuletzt bearbeitet:
Oben