[Veranschaulichung des Public-Key-Verfahrens]

KRYPTOLOGIE

Einstiegsaufgabe Lösung in C

[Veranschaulichung des Public-Key-Verfahrens]

Lösung in C:

#include <stdio.h>

#include <string.h>
#include <stdlib.h>

char* verschluesseln
   (char* satz, int codewort) {

    char buchstabe;
    int asciizahl = 0;
    int stelle = 0;
    int versatz = codewort;

    while (stelle < strlen(satz)) {
        buchstabe = satz[stelle];
        asciizahl = (int) buchstabe;
        asciizahl = asciizahl + versatz;
        buchstabe = (char) asciizahl;
        satz[stelle] = buchstabe;
        stelle = stelle + 1;
    }
}

char* entschluesseln
   (char* satz, int codewort) {

    char buchstabe;
    int asciizahl = 0;
    int stelle = 0;
    int versatz = codewort;

    while (stelle < strlen(satz)) {
        buchstabe = satz[stelle];
        asciizahl = (int) buchstabe;
        asciizahl = asciizahl - versatz;
        buchstabe = (char) asciizahl;
        satz[stelle] = buchstabe;
        stelle = stelle + 1;
    }
}


int main() {
char satz[100];

printf
 ("\n\nCaesar-Verschluesselung\n\n");
printf
 ("========================\n");

printf("Satz eingeben:      ");
fgets (satz, 100, stdin);
printf
 ("=========================\n\n\n");

verschluesseln(satz, 2);
printf
 ("Der verschluesselte Satz lautet: %s\n",
 satz);
entschluesseln(satz, 2);
printf
 ("Der entschluesselte Satz lautet: %s\n\n",
 satz);
printf
 ("========================\n\n\n");

return 0;
}

Lösung in C:

#include <stdio.h>

#include <string.h>
#include <stdlib.h>

char* verschluesseln(char* satz, int codewort) {
    char buchstabe;
    int asciizahl = 0;
    int stelle = 0;
    int versatz = codewort;

    while (stelle < strlen(satz)) {
        buchstabe = satz[stelle];
        asciizahl = (int) buchstabe;
        asciizahl = asciizahl + versatz;
        buchstabe = (char) asciizahl;
        satz[stelle] = buchstabe;
        stelle = stelle + 1;
    }
}

char* entschluesseln(char* satz, int codewort) {
    char buchstabe;
    int asciizahl = 0;
    int stelle = 0;
    int versatz = codewort;

    while (stelle < strlen(satz)) {
        buchstabe = satz[stelle];
        asciizahl = (int) buchstabe;
        asciizahl = asciizahl - versatz;
        buchstabe = (char) asciizahl;
        satz[stelle] = buchstabe;
        stelle = stelle + 1;
    }
}

int main() {
    char satz[100];

    printf("\n\nCaesar-Verschluesselung\n\n");
    printf("========================\n");

    printf("Satz eingeben:      ");
    fgets (satz, 100, stdin);
    printf("=========================\n\n\n");

    verschluesseln(satz, 2);
    printf("Der verschluesselte Satz lautet: %s\n", satz);
    entschluesseln(satz, 2);
    printf("Der entschluesselte Satz lautet: %s\n\n", satz);
    printf("========================\n\n\n");

    return 0;
}