-
Notifications
You must be signed in to change notification settings - Fork 2
/
cBlowfish.cpp
72 lines (61 loc) · 2.13 KB
/
cBlowfish.cpp
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
#pragma hdrstop
#include "cBlowfish.h"
#pragma package(smart_init)
AnsiString cBlowfish::EncodeChypher( const AnsiString MegaText )
{
unsigned buf_size = MegaText.Length();
buf_size += (8-(buf_size%8));
char* buf = new char[ buf_size ];
for( unsigned i=0; i<buf_size; i++ )
buf[ i ] = '\0';
strcpy( buf, MegaText.c_str() );
Encode( buf, buf, buf_size );
return AnsiString(buf);
}
//---------------------------------------------------------------------------
AnsiString cBlowfish::DecodeChypher( const AnsiString MegaText )
{
unsigned buf_size = MegaText.Length();
buf_size += (8-(buf_size%8));
char* buf = new char[ buf_size ];
for( unsigned i=0; i<buf_size; i++ )
buf[ i ] = '\0';
strcpy( buf, MegaText.c_str() );
Decode( buf, buf, buf_size );
return AnsiString(buf);
}
//---------------------------------------------------------------------------
bool cBlowfish::SaveToFile( const AnsiString &FileName ){
AnsiString MegaText = Lines->Text;
unsigned buf_size = MegaText.Length(); buf_size += (8-(buf_size%8));
char* buf = new char[ buf_size ];
for( unsigned i=0;i<buf_size;i++ )buf[ i ] = '\0';
strcpy( buf, MegaText.c_str() );
FILE *fn = fopen( FileName.c_str(), "wb" );
if( !fn )return false;
Encode( buf, buf, buf_size );
fwrite( buf, buf_size, 1, fn );
delete []buf;
return fclose( fn ), true;
}
//---------------------------------------------------------------------------
bool cBlowfish::LoadFromFile( const AnsiString &FileName ){
const unsigned short buf_size = 1024;
char buf[ buf_size + 8 ] = { 0 };
char decode_buf[ buf_size + 8 ] = { 0 };
FILE *fn = fopen( FileName.c_str(), "rb" );
if( !fn )return false;
fseek( fn, SEEK_SET, SEEK_END );
int len = ftell( fn );
fseek( fn, SEEK_SET, 0 );
AnsiString MegaText;
do{
int decode_size = buf_size < len ? buf_size : len;
fread( buf, decode_size, 1, fn );
Decode( buf, decode_buf, decode_size ); decode_buf[ decode_size ] = '\0';
MegaText += decode_buf;
} while( (len-=buf_size) > 0 );
Lines->Text = MegaText;
return fclose( fn ), true;
}
//---------------------------------------------------------------------------