Browse Source

Dateien können nun verschlüsselt werden

kolja 7 years ago
parent
commit
f585ae5698
3 changed files with 89 additions and 8 deletions
  1. 81 7
      Datei.cpp
  2. 7 0
      Datei.h
  3. 1 1
      Schluessel.cpp

+ 81 - 7
Datei.cpp

@@ -1,6 +1,7 @@
 #include "Datei.h"
 #include "Text.h"
 #include "Zeit.h"
+#include "Schluessel.h"
 #ifdef WIN32
 #include <direct.h>
 #include <Shlwapi.h>
@@ -12,6 +13,7 @@
 #endif
 
 using namespace Framework;
+using namespace Verschlüsselung;
 
 // Inhalt der Datei Klasse aus Datei.h
 // Konstruktor 
@@ -23,12 +25,15 @@ Datei::Datei()
     tmpLByte( 0 ),
     tmpLBPos( 7 ),
     tmpSByte( 0 ),
-    tmpSBPos( -1 )
+    tmpSBPos( -1 ),
+	key( 0 )
 {}
 
 // Destruktor 
 Datei::~Datei()
 {
+	if( key )
+		key->release();
     if( stream )
         delete stream;
     if( pfad )
@@ -167,15 +172,34 @@ void Datei::schreibe( char *bytes, int l
         stream->write( &tmpSByte, 1 );
         tmpSByte = 0;
     }
-    stream->write( bytes, län );
+	if( key )
+	{
+		key->setPos( getSPosition() );
+		Bytes *n = new Bytes( bytes, län );
+		key->codieren( n->getThis() );
+		stream->write( n->getBytes(), län );
+		n->release();
+	}
+	else
+		stream->write( bytes, län );
 }
 
 void Datei::lese( char *bytes, int län ) // ließt bytes aus datei
 {
     if( !pfad )
         return;
-    if( stream )
-        stream->read( bytes, län );
+	if( stream )
+	{
+		int tmp = getLPosition();
+		stream->read( bytes, län );
+		if( key )
+		{
+			key->setPos( tmp );
+			Bytes *n = new Bytes();
+			n->setBytesZ( bytes, län );
+			key->decodieren( n );
+		}
+	}
     tmpLBPos = 7;
     tmpSBPos = -1;
 }
@@ -190,7 +214,15 @@ Text *Datei::leseZeile() // lie
     __int64 län = getGröße();
     for( char c = 0; c != '\n' && stream->tellg() < län; )
     {
+		int tmp = getLPosition();
         stream->read( &c, 1 );
+		if( key )
+		{
+			key->setPos( tmp );
+			Bytes *n = new Bytes();
+			n->setBytesZ( &c, 1 );
+			key->decodieren( n );
+		}
         if( c )
             ret->anhängen( (const char*)&c, 1 );
     }
@@ -203,8 +235,19 @@ void Datei::schlie
 {
     if( !pfad || !stream )
         return;
-    if( tmpSBPos >= 0 )
-        stream->write( &tmpSByte, 1 );
+	if( tmpSBPos >= 0 )
+	{
+		if( key )
+		{
+			key->setPos( getSPosition() );
+			Bytes *n = new Bytes( &tmpSByte, 1 );
+			key->codieren( n->getThis() );
+			stream->write( n->getBytes(), 1 );
+			n->release();
+		}
+		else
+			stream->write( &tmpSByte, 1 );
+	}
     stream->close();
     delete stream;
     stream = 0;
@@ -267,7 +310,15 @@ bool Datei::getNextBit( bool &bit ) // Datei Bitweise auslesen
     if( tmpLBPos == 7 )
     {
         tmpLBPos = -1;
+		int tmp = getLPosition();
         stream->read( &tmpLByte, 1 );
+		if( key )
+		{
+			key->setPos( tmp );
+			Bytes *n = new Bytes();
+			n->setBytesZ( &tmpLByte, 1 );
+			key->decodieren( n );
+		}
     }
     tmpLBPos++;
     bit = ( tmpLByte >> ( 7 - tmpLBPos ) ) & 1;
@@ -283,12 +334,35 @@ bool Datei::setNextBit( bool bit ) // Datei Bitweise speichern
     if( tmpSBPos == 7 )
     {
         tmpSBPos = -1;
-        stream->write( &tmpSByte, 1 );
+		if( key )
+		{
+			key->setPos( getSPosition() );
+			Bytes *n = new Bytes( &tmpSByte, 1 );
+			key->codieren( n->getThis() );
+			stream->write( n->getBytes(), 1 );
+			n->release();
+		}
+		else
+			stream->write( &tmpSByte, 1 );
         tmpSByte = 0;
     }
     return 1;
 }
 
+// Setzt den Schlüssel für die Datei
+void Datei::setSchlüssel( char *s, int l )
+{
+	if( l == 0 )
+	{
+		key = key->release();
+		return;
+	}
+	if( key )
+		key->setSchlüssel( s, l );
+	else
+		key = new Schlüssel( s, l );
+}
+
 // constant 
 bool Datei::istOrdner() const // prüft, ob die Datei ein Ordner ist
 {

+ 7 - 0
Datei.h

@@ -8,6 +8,10 @@ namespace Framework
 {
     class Text; // Text.h
     class Zeit; // Zeit.h
+	namespace Verschlüsselung
+	{
+		class Schlüssel; // Schlüssel.h
+	}
     class Datei; // aus dieser Datei
 
     // Ließt und schreibt in eine Datei
@@ -30,6 +34,7 @@ namespace Framework
         char tmpLBPos;
         char tmpSByte;
         char tmpSBPos;
+		Verschlüsselung::Schlüssel *key;
 
     public:
         // Konstruktor 
@@ -81,6 +86,8 @@ namespace Framework
         __declspec( dllexport ) Text *leseZeile();
         // Schließt die datei
         __declspec( dllexport ) void schließen();
+		// Setzt den Schlüssel für die Datei
+		__declspec( dllexport ) void setSchlüssel( char *s, int l );
 #ifdef WIN32
         // Setzt den Zeitpunkt der letzten Änderung der Datei (nur für Windows)
         //  zeit: den Zeitpunkt der letzten Änderung

+ 1 - 1
Schluessel.cpp

@@ -171,7 +171,7 @@ void Schl
 {
     if( p < 0 )
         p = 0;
-    pos = p > län ? 0 : p;
+    pos = p % län;
 }
 
 void Schlüssel::setSchlüssel( const char *s, int län )