PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Verschlüsselungsproggi


Solstice
18.05.2009, 13:19
Ein ganz einfaches Verschlüsselungsproggi, written by me da ich mal bock zu hatte. (Konsolenproggi)

Man kann auch ganz einfach wieder entschlüsseln und es funzt so weit ich weiß einwandfrei...

Verschlüsselung mit Passwort bis 127 Zeichen sowie deren Entschlüsselung.
klein fein und ich finds toll.

Funktioniert bis Dato nur auf Windoof 32, nen andern Compiler hab ich grad nich...

Code:
#include <iostream>
#include <math.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fstream>


using namespace std;

void encode(char *stream,char vrs_file[128]);
void decode(char *stream,char vrs_file[128]);
bool handle(char src_file[128],char tgt_file[128],char vrs_file[128]);

void main ()
{
char src_file[128];
char tgt_file[128];
char vrs_file[128];
unsigned long i;

while (1)
{
src_file[128] = NULL;
tgt_file[128] = NULL;
vrs_file[128] = NULL;
i = 0;
system("cls");

cout << "Quelldatei angeben (mit Pfad): " << flush << endl;
cin.getline(src_file,127);

if (!strlen(src_file))
strcpy_s(src_file,"D:/MTD.txt");

cout << "Zieldatei angeben (mit Pfad(Datei wird erstellt falls nicht vorhanden))" << endl;
cout << "Format: *Laufwerk*:/*Pfad*" << endl;
cout << "Falls Quelldatei ueberschrieben werden soll, nichts eingeben:" << endl;
cin.getline(tgt_file,127);
if (!strlen(tgt_file))
strcpy_s(tgt_file,src_file);

cout << "Verschluesselungspasswort angeben (max. 100 Zeichen): " << endl;
cin.getline(vrs_file,127);
if (!strlen(vrs_file))
strcpy_s(vrs_file,"1234567890");

cout << endl;
if (handle(src_file,tgt_file,vrs_file)) {
cout << "Datei erfolgreich verarbeitet und gespeichert. " << endl;
cout << "Press any key to continue " << endl;
} else
cout << "Datei nicht gefunden. " << endl;
fgetc(stdin);

} // Ende While Schleife


} // Ende Main

void encode(char *stream,char vrs_file[128])
{ int vrsint = 0;
for (unsigned int i = 0;i < strlen(vrs_file);i++)
{
vrsint += vrs_file[i] - 47;
}

vrsint += (vrsint/strlen(vrs_file))+vrsint+1;
vrsint = vrsint%101;
for (unsigned int i = 0;i<strlen(stream);i++)
{
stream[i] = stream[i]-vrsint+i;
}

}


void decode(char *stream,char vrs_file[128])
{
int vrsint = 0;
for (unsigned int i = 0;i < strlen(vrs_file);i++)
{
vrsint += vrs_file[i] - 47;
}

vrsint += (vrsint/strlen(vrs_file))+vrsint+1;
vrsint = vrsint%101;
for (unsigned int i = 0;i<strlen(stream);i++)
{
stream[i] = stream[i]+vrsint-i;
}
}

bool handle (char src_file[128],char tgt_file[128],char vrs_file[128])
{
fstream f;
fstream o;
char c;
string cont = "";
unsigned long i = 0;
unsigned long e = 0;
char * content = NULL;
char * erg = NULL;

// Textdatei zum Lesen öffnen.
f.open(src_file, ios::in | ios::binary);
if (f.fail())
return false;

while( f.read((char*)&c, sizeof(char)) )
cont += c;

f.close();
content = new char[cont.size()];
strcpy(content,cont.c_str());

cout << "Datei erfolgreich geoeffnet. " << endl;
cout << "Verschluesseln 'V' eingeben" << endl;
cout << "Entschluesseln: 'E' eingeben" << endl;

if (tolower(fgetc(stdin)) == 'e')
decode(content,vrs_file);
else
encode(content,vrs_file);



// Textdatei zum Schreiben öffnen.
o.open(tgt_file, ios::out | ios::binary);
if (o.fail())
return false;

for(unsigned int i = 0; i < strlen(content); i++)
{
o.write((char*)&content[i], sizeof(char));
}
o.close();

return true;
}
Rückmeldungen sehr erwünscht...

Edit von Mir: Das Tool ist NICHT leistungsfähig, es hängt sich bereits bei 70 kB Dateigröße auf, vermute Buffer overflow.
Um jedoch kleine Textdateien (spasseshalber?) zu verschlüsseln, reicht es.