Registriert seit: 16. Jul 2003
170 Beiträge
Delphi 6 Enterprise
|
Re: Dateien verschlüsseln - aber wie?
5. Okt 2003, 17:00
Ein Mann, ein Wort...
Hier ist der veränderte Code:
Delphi-Quellcode:
const
EXTENSION = '.cryptmaster';
APPNAME = 'CryptMaster';
APPVER = 'Beta 1';
DefCipherClass : TCipherClass = TCipher_Rijndael;
DefHashClass : THashClass = THash_SHA1;
DefStringFormat = fmtMIME64;
DefCipherMode = cmCTS;
CipherTextBegin = '[BEGIN-CryptMaster-Message]';
CipherTextEnd = '[CryptMaster-Message-END]';
implementation
{$R *.dfm}
// Function zum Generieren der Zufallswerte
function TFrmCipher.RandomString(Len: Integer): String;
begin
SetLength(Result, Len);
Rnd.Buffer(Result[1], Len);
end;
// function zum Verschlüsseln von Text
function TFrmCipher.EncodeString(Klartext, Passwort : string) : string;
var
Hashstring : string;
begin
with DefCipherClass.Create('',nil) do
begin
try
Mode := DefCipherMode;
HashClass := DefHashClass;
InitKey(Passwort,nil);
Klartext := RandomString(BufSize)+Klartext;
with DefHashClass.Create(nil) do
begin
try
Hashstring := CalcString(Klartext,nil,DefStringFormat);
finally
Free;
end;
end;
Klartext := Hashstring+Klartext;
Result := CodeString(Klartext,paEncode,DefStringFormat);
finally
Free;
end;
end;
// Marker für den Ciphertext setzen
Result := CipherTextBegin+Result+CipherTextEnd;
end;
// String dekodieren | Result[1] (0,1) gibt an, ob es einen Fehler gab
function TFrmCipher.DecodeString(Ciphertext,Passwort : string):string;
var
Hashlength : word;
OldHash, NewHash : string;
RemBufSize : Int64;
begin
// Marker für den Ciphertext entfernen
Delete(Ciphertext,1,pos(CipherTextBegin,Ciphertext)+length(CipherTextBegin)-1);
Delete(Ciphertext,pos(CipherTextEnd,Ciphertext),length(Ciphertext)-pos(CipherTextEnd,Ciphertext)+1);
with DefHashClass.Create(nil) do
begin
try
Hashlength := length(CalcString(Ciphertext,nil,DefStringFormat));
finally
Free;
end;
end;
with DefCipherClass.Create('',nil) do
begin
try
Mode := DefCipherMode;
HashClass := DefHashClass;
InitKey(Passwort,nil);
Result := CodeString(Ciphertext,paDecode,DefStringFormat);
RemBufSize := BufSize;
finally
Free;
end;
end;
OldHash := Copy(Result,1,Hashlength);
Delete(Result,1,Hashlength);
with DefHashClass.Create(nil) do
begin
try
NewHash := CalcString(Result,nil,DefStringFormat);
finally
Free;
end;
end;
Delete(Result,1,RemBufSize);
If OldHash = NewHash then Result := '1'+Result else Result := '0'+Result;
end;
MfG,
Daniel.
|
|
Zitat
|