Autor
Nachricht
Tim06TR
Mitglied
Benutzerprofil
Anmeldungsdatum: 14.03.2009
Beiträge: 1308
Tim06TR Mitglied
18:45:42 09.03.2010 Titel:
Unicode Zeichensatz nutzen (siehe unten)
Zitieren
Der folgende Code ist ein sehr simples, aber effektives (zeitlich) Verschlüsselungsverfahren.
Sobald aber der zu verschlüsselnde "Text" zu groß wird (Datei), beginnt er mir immer zu sagen "" sei kein gültiger integer. Ich kann mir das nur erklären, dass Zeichen fehlen, wo kann ich nicht sagen.
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include <vcl.h>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
class CX4Verschlüsselung
{
public :
String Encrypt(String Text, String Schlüssel);
String Decrypt(String Text, String Schlüssel);
};
String CX4Verschlüsselung::Encrypt(String Text, String Schlüssel)
{
int i = 0;
std::vector <int > Save;
String Result;
std::ofstream SaveKey;
std::ofstream SaveResult;
SaveKey.open(Schlüssel.c_str(), ios::binary);
SaveResult.open((Schlüssel + " Result ").c_str(), ios::binary);
while (i < Text.Length())
{
i++;
unsigned char Zeichen = Text[i];
int Zahl = int (Zeichen);
int neu = 0;
if (Zahl%2 == 1)
{
neu = Zahl + 1;
int Rand = rand()%(256-Zahl);
Save.push_back(Rand);
neu = neu + Rand;
Result += IntToStr(neu);
Result += "ea ";
SaveKey << IntToStr(Save[i-1]).c_str() << "ea " << "\n ";
SaveResult << IntToStr(neu).c_str() << "ea " << "\n ";
}
else
{
neu = Zahl;
int Rand = rand()%(256-Zahl);
Save.push_back(Rand);
neu = neu + Rand;
Result += IntToStr(neu);
Result += "eo ";
SaveKey << IntToStr(Save[i-1]).c_str() << "eo " << "\n ";
SaveResult << IntToStr(neu).c_str() << "eo " << "\n ";
}
}
SaveKey.close();
SaveResult.close();
return Result;
}
String CX4Verschlüsselung::Decrypt(String Text, String Schlüssel)
{
int i = 0;
std::ifstream Read;
Read.open(Schlüssel.c_str(), ios::binary);
std::string line;
std::vector <std::string> Code;
std::vector <std::string> Availables;
int Size = 0;
if (Read)
{
while (std::getline(Read,line))
{
Size++;
int a = 0;
String LineStr;
while (a < line.size())
{
a++;
if (line[a-1] != 'e ')
LineStr += line[a-1];
else
break ;
}
Code.push_back(LineStr.c_str());
}
Read.close();
int begin = 0, len = 0;
String Zahl;
ShowMessage("GotIt ");
bool firstpush = true ;
try
{
while (i < Text.Length())
{
i++;
if (Text.SubString(i-1,1) != "e " && Text.SubString(i-1,1) != "o " && Text.SubString(i-1,1) != "a ")
{
len = i - begin;
Zahl = Text.SubString(begin,len);
}
else
{
if (firstpush)
{
Zahl = Zahl.SubString(0,Zahl.Length()-1);
}
firstpush = false ;
i += 1;
begin = (i);
if (Text.SubString(i-1,1) == "o ")
Zahl = IntToStr(StrToInt(Zahl) + 1);
Zahl = IntToStr(StrToInt(Zahl) - 1);
Availables.push_back(Zahl.c_str());
}
}
}
catch (...)
{
ShowMessage("Fehler geortet ");
}
i = 0;
String Ret;
while (i < Size)
{
i++;
std::string CH;
int New = (StrToInt(Availables[i-1].c_str())-StrToInt(Code[i-1].c_str()));
CH = static_cast <unsigned char > (New);
Ret += CH.c_str();
}
return Ret;
}
return "Fehler ";
}
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include <vcl.h>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
class CX4Verschlüsselung
{
public :
String Encrypt(String Text, String Schlüssel);
String Decrypt(String Text, String Schlüssel);
};
String CX4Verschlüsselung::Encrypt(String Text, String Schlüssel)
{
int i = 0;
std::vector <int > Save;
String Result;
std::ofstream SaveKey;
std::ofstream SaveResult;
SaveKey.open(Schlüssel.c_str(), ios::binary);
SaveResult.open((Schlüssel + " Result ").c_str(), ios::binary);
while (i < Text.Length())
{
i++;
unsigned char Zeichen = Text[i];
int Zahl = int (Zeichen);
int neu = 0;
if (Zahl%2 == 1)
{
neu = Zahl + 1;
int Rand = rand()%(256-Zahl);
Save.push_back(Rand);
neu = neu + Rand;
Result += IntToStr(neu);
Result += "ea ";
SaveKey << IntToStr(Save[i-1]).c_str() << "ea " << "\n ";
SaveResult << IntToStr(neu).c_str() << "ea " << "\n ";
}
else
{
neu = Zahl;
int Rand = rand()%(256-Zahl);
Save.push_back(Rand);
neu = neu + Rand;
Result += IntToStr(neu);
Result += "eo ";
SaveKey << IntToStr(Save[i-1]).c_str() << "eo " << "\n ";
SaveResult << IntToStr(neu).c_str() << "eo " << "\n ";
}
}
SaveKey.close();
SaveResult.close();
return Result;
}
String CX4Verschlüsselung::Decrypt(String Text, String Schlüssel)
{
int i = 0;
std::ifstream Read;
Read.open(Schlüssel.c_str(), ios::binary);
std::string line;
std::vector <std::string> Code;
std::vector <std::string> Availables;
int Size = 0;
if (Read)
{
while (std::getline(Read,line))
{
Size++;
int a = 0;
String LineStr;
while (a < line.size())
{
a++;
if (line[a-1] != 'e ')
LineStr += line[a-1];
else
break ;
}
Code.push_back(LineStr.c_str());
}
Read.close();
int begin = 0, len = 0;
String Zahl;
ShowMessage("GotIt ");
bool firstpush = true ;
try
{
while (i < Text.Length())
{
i++;
if (Text.SubString(i-1,1) != "e " && Text.SubString(i-1,1) != "o " && Text.SubString(i-1,1) != "a ")
{
len = i - begin;
Zahl = Text.SubString(begin,len);
}
else
{
if (firstpush)
{
Zahl = Zahl.SubString(0,Zahl.Length()-1);
}
firstpush = false ;
i += 1;
begin = (i);
if (Text.SubString(i-1,1) == "o ")
Zahl = IntToStr(StrToInt(Zahl) + 1);
Zahl = IntToStr(StrToInt(Zahl) - 1);
Availables.push_back(Zahl.c_str());
}
}
}
catch (...)
{
ShowMessage("Fehler geortet ");
}
i = 0;
String Ret;
while (i < Size)
{
i++;
std::string CH;
int New = (StrToInt(Availables[i-1].c_str())-StrToInt(Code[i-1].c_str()));
CH = static_cast <unsigned char > (New);
Ret += CH.c_str();
}
return Ret;
}
return "Fehler ";
}
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include <vcl.h>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
class CX4Verschlüsselung
{
public :
String Encrypt(String Text, String Schlüssel);
String Decrypt(String Text, String Schlüssel);
};
String CX4Verschlüsselung::Encrypt(String Text, String Schlüssel)
{
int i = 0;
std::vector <int > Save;
String Result;
std::ofstream SaveKey;
std::ofstream SaveResult;
SaveKey.open(Schlüssel.c_str(), ios::binary);
SaveResult.open((Schlüssel + " Result ").c_str(), ios::binary);
while (i < Text.Length())
{
i++;
unsigned char Zeichen = Text[i];
int Zahl = int (Zeichen);
int neu = 0;
if (Zahl%2 == 1)
{
neu = Zahl + 1;
int Rand = rand()%(256-Zahl);
Save.push_back(Rand);
neu = neu + Rand;
Result += IntToStr(neu);
Result += "ea ";
SaveKey << IntToStr(Save[i-1]).c_str() << "ea " << "\n ";
SaveResult << IntToStr(neu).c_str() << "ea " << "\n ";
}
else
{
neu = Zahl;
int Rand = rand()%(256-Zahl);
Save.push_back(Rand);
neu = neu + Rand;
Result += IntToStr(neu);
Result += "eo ";
SaveKey << IntToStr(Save[i-1]).c_str() << "eo " << "\n ";
SaveResult << IntToStr(neu).c_str() << "eo " << "\n ";
}
}
SaveKey.close();
SaveResult.close();
return Result;
}
String CX4Verschlüsselung::Decrypt(String Text, String Schlüssel)
{
int i = 0;
std::ifstream Read;
Read.open(Schlüssel.c_str(), ios::binary);
std::string line;
std::vector <std::string> Code;
std::vector <std::string> Availables;
int Size = 0;
if (Read)
{
while (std::getline(Read,line))
{
Size++;
int a = 0;
String LineStr;
while (a < line.size())
{
a++;
if (line[a-1] != 'e ')
LineStr += line[a-1];
else
break ;
}
Code.push_back(LineStr.c_str());
}
Read.close();
int begin = 0, len = 0;
String Zahl;
ShowMessage("GotIt ");
bool firstpush = true ;
try
{
while (i < Text.Length())
{
i++;
if (Text.SubString(i-1,1) != "e " && Text.SubString(i-1,1) != "o " && Text.SubString(i-1,1) != "a ")
{
len = i - begin;
Zahl = Text.SubString(begin,len);
}
else
{
if (firstpush)
{
Zahl = Zahl.SubString(0,Zahl.Length()-1);
}
firstpush = false ;
i += 1;
begin = (i);
if (Text.SubString(i-1,1) == "o ")
Zahl = IntToStr(StrToInt(Zahl) + 1);
Zahl = IntToStr(StrToInt(Zahl) - 1);
Availables.push_back(Zahl.c_str());
}
}
}
catch (...)
{
ShowMessage("Fehler geortet ");
}
i = 0;
String Ret;
while (i < Size)
{
i++;
std::string CH;
int New = (StrToInt(Availables[i-1].c_str())-StrToInt(Code[i-1].c_str()));
CH = static_cast <unsigned char > (New);
Ret += CH.c_str();
}
return Ret;
}
return "Fehler ";
}
Aufruf:
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
void __fastcall TCodeX4::Button1Click(TObject *Sender)
{
CX4Verschlüsselung* CX4 = new CX4Verschlüsselung;
String Schlüssel = ModuleInformation("Appi ",1).c_str();
Schlüssel += "Schlüssel\\Code X4\\ ";
Schlüssel += KeyName->Text;
Schlüssel += "\\ ";
Schlüssel += KeyName->Text;
Schlüssel += ".cx4 ";
CreateDirectory(Schlüssel.SubString(0,Schlüssel.Length()-4-KeyName->Text.Length()).c_str(),NULL);
Ausgang->Text = CX4->Encrypt(Eingang->Text,Schlüssel);
delete CX4;
}
//---------------------------------------------------------------------------
void __fastcall TCodeX4::FormHide(TObject *Sender)
{
Main->Show();
}
//---------------------------------------------------------------------------
void __fastcall TCodeX4::Button2Click(TObject *Sender)
{
CX4Verschlüsselung* CX4 = new CX4Verschlüsselung;
String Schlüssel = ModuleInformation("Appi ",1).c_str();
Schlüssel += "Schlüssel\\Code X4\\ ";
Schlüssel += KeyName->Text;
Schlüssel += "\\ ";
Schlüssel += KeyName->Text;
Schlüssel += ".cx4 ";
Ausgang->Text = CX4->Decrypt(Eingang->Text,Schlüssel);
delete CX4;
}
//---------------------------------------------------------------------------
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
void __fastcall TCodeX4::Button1Click(TObject *Sender)
{
CX4Verschlüsselung* CX4 = new CX4Verschlüsselung;
String Schlüssel = ModuleInformation("Appi ",1).c_str();
Schlüssel += "Schlüssel\\Code X4\\ ";
Schlüssel += KeyName->Text;
Schlüssel += "\\ ";
Schlüssel += KeyName->Text;
Schlüssel += ".cx4 ";
CreateDirectory(Schlüssel.SubString(0,Schlüssel.Length()-4-KeyName->Text.Length()).c_str(),NULL);
Ausgang->Text = CX4->Encrypt(Eingang->Text,Schlüssel);
delete CX4;
}
//---------------------------------------------------------------------------
void __fastcall TCodeX4::FormHide(TObject *Sender)
{
Main->Show();
}
//---------------------------------------------------------------------------
void __fastcall TCodeX4::Button2Click(TObject *Sender)
{
CX4Verschlüsselung* CX4 = new CX4Verschlüsselung;
String Schlüssel = ModuleInformation("Appi ",1).c_str();
Schlüssel += "Schlüssel\\Code X4\\ ";
Schlüssel += KeyName->Text;
Schlüssel += "\\ ";
Schlüssel += KeyName->Text;
Schlüssel += ".cx4 ";
Ausgang->Text = CX4->Decrypt(Eingang->Text,Schlüssel);
delete CX4;
}
//---------------------------------------------------------------------------
C/C++ Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
void __fastcall TCodeX4::Button1Click(TObject *Sender)
{
CX4Verschlüsselung* CX4 = new CX4Verschlüsselung;
String Schlüssel = ModuleInformation("Appi ",1).c_str();
Schlüssel += "Schlüssel\\Code X4\\ ";
Schlüssel += KeyName->Text;
Schlüssel += "\\ ";
Schlüssel += KeyName->Text;
Schlüssel += ".cx4 ";
CreateDirectory(Schlüssel.SubString(0,Schlüssel.Length()-4-KeyName->Text.Length()).c_str(),NULL);
Ausgang->Text = CX4->Encrypt(Eingang->Text,Schlüssel);
delete CX4;
}
//---------------------------------------------------------------------------
void __fastcall TCodeX4::FormHide(TObject *Sender)
{
Main->Show();
}
//---------------------------------------------------------------------------
void __fastcall TCodeX4::Button2Click(TObject *Sender)
{
CX4Verschlüsselung* CX4 = new CX4Verschlüsselung;
String Schlüssel = ModuleInformation("Appi ",1).c_str();
Schlüssel += "Schlüssel\\Code X4\\ ";
Schlüssel += KeyName->Text;
Schlüssel += "\\ ";
Schlüssel += KeyName->Text;
Schlüssel += ".cx4 ";
Ausgang->Text = CX4->Decrypt(Eingang->Text,Schlüssel);
delete CX4;
}
//---------------------------------------------------------------------------
Ich sehe absolut kein Systematischen Fehler.
EDIT: Jupp wenn ich den inhalt in einer Datei speicher (Eingang->SaveToFile) dann sind das exakt 64,1 KB rund 65. der schlüssel ist 261 KB groß. eine kleine Abweichung darf / kann sein aber das ist 100 % eine Grenze des RichEdits.
Soll ich Dateien Seperat verschlüsseln lassen ? Oder irre mich ja.
_________________ "Wie ein Komponist an seinem Klavier gleitet der Programmierer über die Tasten auf der Suche nach der perfekten Komposition" ~me --- Wehe ihr lest meine alten posts !
Zuletzt bearbeitet von Tim06TR am 22:02:33 09.03.2010, insgesamt 2-mal bearbeitet
akari
Moderator
Benutzerprofil
Anmeldungsdatum: 27.11.2004
Beiträge: 11253
akari Moderator
20:59:47 09.03.2010 Titel:
Zitieren
Hallo
Ja, das normale TRichEdit hat von Windows aus eine Maximalgrenze von 64 KByte Zeichen. Hier findest du einen Weg, wie du diese Grenze erhöhen kannst.
bis bald
akari
_________________ In der nächsten Version wird alles besser!
Tim06TR
Mitglied
Benutzerprofil
Anmeldungsdatum: 14.03.2009
Beiträge: 1308
Tim06TR Mitglied
22:00:59 09.03.2010 Titel:
Zitieren
Danke funktioniert so, aber das ist möglicherweise sowieso egal, weil meine Zeichenbandbreite von 0 bis 255 sowieso nicht für Dateien reicht.
Was ich da machen soll weiß ich noch nicht.
Kann ich Unicode benutzen ?, das reicht auf alle fälle aus (glaub ich), ASCII nicht.
Doch weiß ich nicht wie ich aus einem Zeichen eine Zahl mache und umgekehrt
_________________ "Wie ein Komponist an seinem Klavier gleitet der Programmierer über die Tasten auf der Suche nach der perfekten Komposition" ~me --- Wehe ihr lest meine alten posts !
Zuletzt bearbeitet von Tim06TR am 22:05:01 09.03.2010, insgesamt 2-mal bearbeitet
akari
Moderator
Benutzerprofil
Anmeldungsdatum: 27.11.2004
Beiträge: 11253
akari Moderator
11:03:34 10.03.2010 Titel:
Zitieren
Hallo
Zitat:
Danke funktioniert so, aber das ist möglicherweise sowieso egal, weil meine Zeichenbandbreite von 0 bis 255 sowieso nicht für Dateien reicht.
Was ich da machen soll weiß ich noch nicht.
Kann ich Unicode benutzen ?, das reicht auf alle fälle aus (glaub ich), ASCII nicht.
Doch weiß ich nicht wie ich aus einem Zeichen eine Zahl mache und umgekehrt
Wenn du eine Unicode-Textdatei hast, dann solltest du wenigstens zum Anzeigen auch Unicode-kompatible Controls benutzen.
Wenn du aber binäre Dateien hast, dann nützt dir UnicodeString genausowenig wie AnsiString, diese Dateien solltest du nicht mit String-Klassen oder TStringList behandeln, sondern nur durch Streams (TFileStreams oder std::fstream) sowie char/w_char_t-Arrays.
Ein char/w_char_t must du nicht extra in eine Zahl umwandeln, diese Datentypen sind selber schon Zahlen.
bis bald
akari
_________________ In der nächsten Version wird alles besser!
akari
Moderator
Benutzerprofil
Anmeldungsdatum: 27.11.2004
Beiträge: 11253
akari Moderator
21:30:02 13.03.2010 Titel:
Zitieren
Hallo
Die Diskussion um die Effektivität des Verschlüsselungsverfahrens wird hier fortgesetzt.
bis bald
akari
_________________ In der nächsten Version wird alles besser!
Nächstes Thema anzeigen
Vorheriges Thema anzeigen
Sie können Beiträge in dieses Forum schreiben. Sie können auf Beiträge in diesem Forum antworten. Sie können Ihre Beiträge in diesem Forum nicht bearbeiten. Sie können Ihre Beiträge in diesem Forum nicht löschen. Sie können an Umfragen in diesem Forum nicht mitmachen.