#include <stdio.h>
#include <stdlib.h>
#define SHIFT 3
void criptografarArquivo( const char * arquivoEntrada, const char * arquivoSaida) {
FILE
* entrada
= fopen ( arquivoEntrada
, "r" ) ; FILE
* saida
= fopen ( arquivoSaida
, "w" ) ;
if ( entrada == NULL || saida == NULL) {
printf ( "Erro ao abrir os arquivos.\n " ) ; }
char ch;
while ( ( ch
= fgetc ( entrada
) ) != EOF
) { fputc ( ch
+ SHIFT
, saida
) ; }
printf ( "Arquivo criptografado com sucesso.\n " ) ; }
void descriptografarArquivo( const char * arquivoEntrada, const char * arquivoSaida) {
FILE
* entrada
= fopen ( arquivoEntrada
, "r" ) ; FILE
* saida
= fopen ( arquivoSaida
, "w" ) ;
if ( entrada == NULL || saida == NULL) {
printf ( "Erro ao abrir os arquivos.\n " ) ; }
char ch;
while ( ( ch
= fgetc ( entrada
) ) != EOF
) { fputc ( ch
- SHIFT
, saida
) ; }
printf ( "Arquivo descriptografado com sucesso.\n " ) ; }
int main( ) {
int opcao;
char arquivoEntrada[ 100 ] , arquivoSaida[ 100 ] ;
printf ( "Escolha uma opção:\n " ) ; printf ( "1. Criptografar um arquivo\n " ) ; printf ( "2. Descriptografar um arquivo\n " ) ;
printf ( "Digite o nome do arquivo de entrada: " ) ; scanf ( "%s" , arquivoEntrada
) ; printf ( "Digite o nome do arquivo de saÃda: " ) ; scanf ( "%s" , arquivoSaida
) ;
if ( opcao == 1 ) {
criptografarArquivo( arquivoEntrada, arquivoSaida) ;
} else if ( opcao == 2 ) {
descriptografarArquivo( arquivoEntrada, arquivoSaida) ;
} else {
printf ( "Opção inválida.\n " ) ; }
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCiNkZWZpbmUgU0hJRlQgMyAKCnZvaWQgY3JpcHRvZ3JhZmFyQXJxdWl2byhjb25zdCBjaGFyICphcnF1aXZvRW50cmFkYSwgY29uc3QgY2hhciAqYXJxdWl2b1NhaWRhKSB7CiAgICBGSUxFICplbnRyYWRhID0gZm9wZW4oYXJxdWl2b0VudHJhZGEsICJyIik7CiAgICBGSUxFICpzYWlkYSA9IGZvcGVuKGFycXVpdm9TYWlkYSwgInciKTsKCiAgICBpZiAoZW50cmFkYSA9PSBOVUxMIHx8IHNhaWRhID09IE5VTEwpIHsKICAgICAgICBwcmludGYoIkVycm8gYW8gYWJyaXIgb3MgYXJxdWl2b3MuXG4iKTsKICAgICAgICBleGl0KDEpOwogICAgfQoKICAgIGNoYXIgY2g7CiAgICB3aGlsZSAoKGNoID0gZmdldGMoZW50cmFkYSkpICE9IEVPRikgewogICAgICAgIGZwdXRjKGNoICsgU0hJRlQsIHNhaWRhKTsgCiAgICB9CgogICAgZmNsb3NlKGVudHJhZGEpOwogICAgZmNsb3NlKHNhaWRhKTsKICAgIHByaW50ZigiQXJxdWl2byBjcmlwdG9ncmFmYWRvIGNvbSBzdWNlc3NvLlxuIik7Cn0KCnZvaWQgZGVzY3JpcHRvZ3JhZmFyQXJxdWl2byhjb25zdCBjaGFyICphcnF1aXZvRW50cmFkYSwgY29uc3QgY2hhciAqYXJxdWl2b1NhaWRhKSB7CiAgICBGSUxFICplbnRyYWRhID0gZm9wZW4oYXJxdWl2b0VudHJhZGEsICJyIik7CiAgICBGSUxFICpzYWlkYSA9IGZvcGVuKGFycXVpdm9TYWlkYSwgInciKTsKCiAgICBpZiAoZW50cmFkYSA9PSBOVUxMIHx8IHNhaWRhID09IE5VTEwpIHsKICAgICAgICBwcmludGYoIkVycm8gYW8gYWJyaXIgb3MgYXJxdWl2b3MuXG4iKTsKICAgICAgICBleGl0KDEpOwogICAgfQoKICAgIGNoYXIgY2g7CiAgICB3aGlsZSAoKGNoID0gZmdldGMoZW50cmFkYSkpICE9IEVPRikgewogICAgICAgIGZwdXRjKGNoIC0gU0hJRlQsIHNhaWRhKTsgCiAgICB9CgogICAgZmNsb3NlKGVudHJhZGEpOwogICAgZmNsb3NlKHNhaWRhKTsKICAgIHByaW50ZigiQXJxdWl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7CgkJZXhpdChFWElUX0ZBSUxVUkUpOwoJfQoKCWk9MDsKCXByaW50ZigmcXVvdDsxLlplaWxlIGVpbmxlc2VuLlxuJnF1b3Q7KTsKCiAJd2hpbGUoKGhleD1mZ2V0YyhmcCkpIT0nXG4nICZhbXA7JmFtcDsgaGV4ICE9IEVPRikKICAgIHsKICAgICAgICBhW2ldPXN0cnRvbCgmYW1wO2hleCwwLDE2KTsKCQlpKys7CiAgICB9CglpPTA7CglwcmludGYoJnF1b3Q7Mi5aZWlsZSBlaW5sZXNlbi5cbiZxdW90Oyk7CgogCXdoaWxlKChoZXg9ZmdldGMoZnApKSE9J1xuJyAmYW1wOyZhbXA7IGhleCAhPSBFT0YpCiAgICB7CiAgICAJYltpXT1zdHJ0b2woJmFtcDtoZXgsMCwxNik7CiAgICAgICAgaSsrOwogICAgfQoJZmNsb3NlKGZwKTsKCglwcmludGYoJnF1b3Q7SGFtbWluZy1BYndlaWNodW5nIHBybyBOaWJibGU6XG4mcXVvdDspOwoJZm9yIChpID0gMDsgaSAmbHQ7IEFSUkFZX1NJWkU7ICsraSkKCXsKCQlwcmludGYgKCZxdW90OyVpXHQlaVx0JWlcbiZxdW90OyxhW2ldLGJbaV0sSGFtZGlzdChhW2ldLGJbaV0pKTsKCQloYW1EaXN0ICs9IEhhbWRpc3QoYVtpXSxiW2ldKTsKCX0KCXByaW50ZiAoJnF1b3Q7XG5IYW1taW5nLUFid2VpY2h1bmcgZGVyIEhhc2gtV2VydGU6JWRcbiZxdW90OyxoYW1EaXN0KTsKfQoK
/* Berechnung des Hamming-Abstandes zwischen zwei 128-Bit Werten in */
/* einer Textdatei. */
/* Die Werte müssen auf einer separaten Zeile gespeichert sein */
/* */
/* Erstellt: 17.5.2010 */
/* Autor: Thomas Scheffler */
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 32
unsigned Hamdist(unsigned x, unsigned y)
{
unsigned dist = 0, val = x ^ y;
// Count the number of set bits
while(val)
{
++dist;
val &= val - 1;
}
return dist;
}
int main (void)
{
char hex;
int i;
int a[ARRAY_SIZE];
int b[ARRAY_SIZE];
int hamDist = 0;
FILE* fp;
//Arrays mit 0 initialisieren
for (i = 0; i < ARRAY_SIZE; ++i)
{
a[i] = 0;
b[i] = 0;
}
fp = fopen("hex.txt","r");
if (fp == NULL)
{
printf("Die Datei hex.txt wurde nicht gefunden!");
exit(EXIT_FAILURE);
}
i=0;
printf("1.Zeile einlesen.\n");
while((hex=fgetc(fp))!='\n' && hex != EOF)
{
a[i]=strtol(&hex,0,16);
i++;
}
i=0;
printf("2.Zeile einlesen.\n");
while((hex=fgetc(fp))!='\n' && hex != EOF)
{
b[i]=strtol(&hex,0,16);
i++;
}
fclose(fp);
printf("Hamming-Abweichung pro Nibble:\n");
for (i = 0; i < ARRAY_SIZE; ++i)
{
printf ("%i\t%i\t%i\n",a[i],b[i],Hamdist(a[i],b[i]));
hamDist += Hamdist(a[i],b[i]);
}
printf ("\nHamming-Abweichung der Hash-Werte:%d\n",hamDist);
}