Помогите найти ошибку! Исходник прикрепил...
Вроде всё шифрует и дешифрует... Но в зависимости от введённого пароля он не всегда шифрует текст полностью... Ума не приложу, в чём может быть ошибка...
Вот код (лучше посмотрите по исходнику):
| Код | //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "CSPIN" #pragma resource "*.dfm"
#define F(x) (((SB[0][x.byte.b1] + SB[1][x.byte.b2]) ^ SB[2][x.byte.b3]) + SB[3][x.byte.b4])
TForm1 *Form1;
TStringList *Inp = new TStringList; String text; String txt; unsigned int PA[18]; unsigned int SB[4][256];
struct BlockByte { unsigned int b1:8; unsigned int b2:8; unsigned int b3:8; unsigned int b4:8; };
union Block { unsigned int word; BlockByte byte; };
struct DblBlock { Block word1; Block word2; };
inline void BF_En(Block *x1, Block *x2); inline void BF_De(Block *x1, Block *x2); char Check_Inp_Sym(int i); char Check_Out_Sym(int i);
//--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- char Check_Sym(int i) { if (i <= text.Length()) { return text[i]; } else { return NULL; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Encrypt() {
DblBlock words;
txt = "";
for (int i=1; i<=text.Length(); i += 8) { words.word1.byte.b1 = Check_Sym(i); words.word1.byte.b2 = Check_Sym(i+1); words.word1.byte.b3 = Check_Sym(i+2); words.word1.byte.b4 = Check_Sym(i+3);
words.word2.byte.b1 = Check_Sym(i+4); words.word2.byte.b2 = Check_Sym(i+5); words.word2.byte.b3 = Check_Sym(i+6); words.word2.byte.b4 = Check_Sym(i+7);
BF_En(&words.word1, &words.word2);
txt += (char)words.word1.byte.b1; txt += (char)words.word1.byte.b2; txt += (char)words.word1.byte.b3; txt += (char)words.word1.byte.b4; txt += (char)words.word2.byte.b1; txt += (char)words.word2.byte.b2; txt += (char)words.word2.byte.b3; txt += (char)words.word2.byte.b4; } txt_out->Text = txt; } //--------------------------------------------------------------------------- void __fastcall TForm1::Decrypt() {
DblBlock words;
txt = "";
for (int i=1; i<=text.Length(); i += 8) { words.word1.byte.b1 = Check_Sym(i); words.word1.byte.b2 = Check_Sym(i+1); words.word1.byte.b3 = Check_Sym(i+2); words.word1.byte.b4 = Check_Sym(i+3);
words.word2.byte.b1 = Check_Sym(i+4); words.word2.byte.b2 = Check_Sym(i+5); words.word2.byte.b3 = Check_Sym(i+6); words.word2.byte.b4 = Check_Sym(i+7);
BF_De(&words.word1, &words.word2);
txt += (char)words.word1.byte.b1; txt += (char)words.word1.byte.b2; txt += (char)words.word1.byte.b3; txt += (char)words.word1.byte.b4; txt += (char)words.word2.byte.b1; txt += (char)words.word2.byte.b2; txt += (char)words.word2.byte.b3; txt += (char)words.word2.byte.b4; } txt_blow->Text = txt; } //--------------------------------------------------------------------------- void __fastcall TForm1::Gen_Subkey(char * Pass) { unsigned int pas_len = strlen(Pass), j; Block pas_block, null_block1, null_block2;
j = 0;
for (int i=0; i<18; i++) { pas_block.byte.b1 = Pass[(j++)%pas_len]; pas_block.byte.b2 = Pass[(j++)%pas_len]; pas_block.byte.b3 = Pass[(j++)%pas_len]; pas_block.byte.b4 = Pass[(j++)%pas_len]; PA[i] ^= pas_block.word; }
null_block1.word = null_block2.word = 0;
for (int i=0; i<18; i+=2) { BF_En(&null_block1, &null_block2); PA[i] = null_block1.word; PA[i+1] = null_block2.word; }
for (int j=0; j<4; j++) { for (int i=0; i<256; i+=2) { BF_En(&null_block1, &null_block2); SB[j][i] = null_block1.word; SB[j][i+1] = null_block2.word; } }
pas_block.word = null_block1.word = null_block2.word = 0; Pass = NULL; pas_len = 0; j = 0; } //--------------------------------------------------------------------------- void BF_En(Block *x1, Block *x2) { Block w1 = *x1, w2 = *x2;
w1.word ^= PA[0]; w2.word ^= F(w1)^PA[1]; w1.word ^= F(w2)^PA[2]; w2.word ^= F(w1)^PA[3]; w1.word ^= F(w2)^PA[4]; w2.word ^= F(w1)^PA[5]; w1.word ^= F(w2)^PA[6]; w2.word ^= F(w1)^PA[7]; w1.word ^= F(w2)^PA[8]; w2.word ^= F(w1)^PA[9]; w1.word ^= F(w2)^PA[10]; w2.word ^= F(w1)^PA[11]; w1.word ^= F(w2)^PA[12]; w2.word ^= F(w1)^PA[13]; w1.word ^= F(w2)^PA[14]; w2.word ^= F(w1)^PA[15]; w1.word ^= F(w2)^PA[16]; w2.word ^= PA[17];
*x1 = w2; *x2 = w1; } //--------------------------------------------------------------------------- void BF_De(Block *x1, Block *x2) { Block w1 = *x1, w2 = *x2;
w1.word ^= PA[17]; w2.word ^= F(w1)^PA[16]; w1.word ^= F(w2)^PA[15]; w2.word ^= F(w1)^PA[14]; w1.word ^= F(w2)^PA[13]; w2.word ^= F(w1)^PA[12]; w1.word ^= F(w2)^PA[11]; w2.word ^= F(w1)^PA[10]; w1.word ^= F(w2)^PA[9]; w2.word ^= F(w1)^PA[8]; w1.word ^= F(w2)^PA[7]; w2.word ^= F(w1)^PA[6]; w1.word ^= F(w2)^PA[5]; w2.word ^= F(w1)^PA[4]; w1.word ^= F(w2)^PA[3]; w2.word ^= F(w1)^PA[2]; w1.word ^= F(w2)^PA[1]; w2.word ^= PA[0];
*x1 = w2; *x2 = w1; } //--------------------------------------------------------------------------- void __fastcall TForm1::Reset() {
unsigned int i,j;
unsigned int PA_Init[18] = { 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b };
unsigned int SB_Init[4][256] = { 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, ...И так далее... Там 1024 строчки, поэтому все сюда не стал писать=) };
for (i=0; i<18; i++) { PA[i] = PA_Init[i]; }
for (j=0; j<4; j++) { for (i=0; i<256; i++) { SB[j][i] = SB_Init[j][i]; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { OpenD->Execute(); Inp->LoadFromFile(OpenD->FileName); txt_inp->Text = Inp->Text; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { Inp->LoadFromFile("text.txt"); txt_inp->Text = Inp->Text; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { text = txt_inp->Text; char * Passwd = Pass->Text.c_str(); Reset(); Gen_Subkey(Passwd); Encrypt(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button4Click(TObject *Sender) { text = txt_out->Text; char * Passwd = Pass->Text.c_str(); Reset(); Gen_Subkey(Passwd); Decrypt(); } //---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender) { for (int i=0; i<18; i++) { P_Arr->Cells[0][i] = (char)PA[i]; } Edit1->Text = txt_inp->Text.Length(); Edit2->Text = txt_out->Text.Length(); Edit3->Text = txt_blow->Text.Length(); } //---------------------------------------------------------------------------
|
|