Шустрый

Профиль
Группа: Участник
Сообщений: 62
Регистрация: 9.5.2006
Репутация: нет Всего: нет
|
привет всем! подскажите пожалуйста: вот исходник алгоритма IDEA на C++Builder | Код | //---------------------------------------------------------------------------
#ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <fstream.h> #include <iomanip.h> #include <iostream.h> #include <io.h> #include <string.h> #include <stdio.h> #include <fcntl.h> #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Dialogs.hpp> #include <string> #include <math.h>
#include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TButton *Button2; TEdit *Edit1; TEdit *Edit2; TButton *Button3; TButton *Button4; TEdit *Edit3; TButton *Button5; TOpenDialog *OpenDialog2; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); void __fastcall Button3Click(TObject *Sender); void __fastcall Button4Click(TObject *Sender); void __fastcall Button5Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
void LeftShift(__int16 SimpleKey[]){ __int64 temp[2]; __int64 t1, t0, s1, s0; temp[0] = temp[1] = 0; __int64 x1, x2, x3, x4;
x1=x2=x3=x4=0; x1 = (__int64)(SimpleKey[0]&0x000000000000ffff); x2 = (__int64)(SimpleKey[1]&0x000000000000ffff); x3 = (__int64)(SimpleKey[2]&0x000000000000ffff); x4 = (__int64)(SimpleKey[3]&0x000000000000ffff); temp[0] = ((x1)<<48) | ((x2)<<32) | ((x3)<<16) | ((x4)); x1=x2=x3=x4=0; x1 = (__int64)(SimpleKey[4]&0x000000000000ffff); x2 = (__int64)(SimpleKey[5]&0x000000000000ffff); x3 = (__int64)(SimpleKey[6]&0x000000000000ffff); x4 = (__int64)(SimpleKey[7]&0x000000000000ffff); temp[1] = ((x1)<<48) | ((x2)<<32) | ((x3)<<16) | ((x4));
t1 = t0 = 0; __int64 l1, l0; l1=l0=0; l1 = temp[1] >> 39; l0 = temp[0] >> 39; t1 = l1&0x0000000001ffffff; t0 = l0&0x0000000001ffffff; l1 = l0 = 0; l1 = temp[1] << 25; l0 = temp[0] << 25; s1 = l1&0xfffffffffe000000; s0 = l0&0xfffffffffe000000; temp[0] = s0 | t1; temp[1] = s1 | t0;
int iL=0; for(int jL=0; jL < 8; jL += 4) { SimpleKey[jL] =(__int16)(temp[iL] >> 48); SimpleKey[jL+1] = (__int16)((temp[iL] >> 32)); SimpleKey[jL+2] = (__int16)((temp[iL] >> 16)); SimpleKey[jL+3] = (__int16)((temp[iL])); iL++; }
}
void makeKey(__int16 KeySheduleCrypt[52], __int16 SimpleKey[8]){
int j=0; for(int i=0; i<52; i++){ if(j ==8 ){ LeftShift(SimpleKey); j=0;} KeySheduleCrypt[i] = SimpleKey[j]; j++; } }
__int16 Mul(__int16 a, __int16 b){ unsigned __int32 buf; unsigned __int16 t;
if(a){ if(b){ buf=(((__int32)(a))&0x0000ffff)*(((__int32)(b))&0x0000ffff); buf = (unsigned __int32)buf % 0x10001; t = buf; }else t = 1-a; }else t = 1-b;
/* buf = 0; if (a) { if (b) { buf=(((__int32)(a))&0x0000ffff)*(((__int32)(b))&0x0000ffff); b=(unsigned __int16)buf; // a присвоить значение младшего байта a=(unsigned __int16)(buf>>16); // b присвоить значение старшего байта t = b-a; if((unsigned __int16)(b) < (unsigned __int16)(a)) t++ ; } else t = 1-a; } else t = 1-b; */ return t;
}
void IdeaCipher(__int16 KeyShedule[52], __int16 Input[4], __int16 OutPut[]){
__int16 x1, x2, x3, x4, s1, s2; int j=0; x1 = Input[0]; x2 = Input[1]; x3 = Input[2]; x4 = Input[3];
for(int i=0; i<8; i++){ x1 = Mul(x1,KeyShedule[j++]); x2+=KeyShedule[j++]; x3+=KeyShedule[j++]; x4 = Mul(x4,KeyShedule[j++]); s2=x1^x3; s2 = Mul(s2,KeyShedule[j++]); s1 = s2+(x2^x4); s1 = Mul(s1,KeyShedule[j++]); s2 = s1 + s2; x1^=s1; x4^=s2; s2^=x2; x2=x3^s1; x3=s2; } x1 = Mul(x1,KeyShedule[j++]); OutPut[0] = x1; OutPut[1] = (x3+KeyShedule[j++]); OutPut[2] = (x2+KeyShedule[j++]); x4 = Mul(x4,KeyShedule[j]); OutPut[3] = x4; }
bool readBlock(unsigned char clearBlock[],string fname, long &fPointer, int bSize){
long fSize; int i, subF; ifstream fobj(fname.begin(), ios::in | ios::binary); fobj.seekg(0, ios::end); fSize = fobj.tellg(); fobj.seekg(fPointer); subF = fSize - fPointer;
if(subF <= 0) return false; fobj.read(clearBlock, bSize);
if(bSize > subF){ for(i=subF; i<8; i++) clearBlock[i]=0; fobj.seekg(0); }
fPointer += bSize; return true; }
void MakeDigMas(__int16 digMas[], unsigned char a[], int BytesCount){
int j=0; __int16 t1, t2; t1 = t2 = 0; for(int i=0; i<BytesCount; i+=2){ t1 = ((__int16)(a[i]<<8))&0xff00; t2 = ((__int16)(a[i+1]))&0x00ff; digMas[j] = t1 | t2; j++; } }
void MakeDecMas(__int16 block[], unsigned char a[], int BytesCount){ int j=0, k=0; __int16 t; for(int i=0; i<BytesCount; i+=2){ t = (block[j]) >> 8; a[i] = (__int8)t; a[i+1] = (__int8)block[j]; j++; } }
void AddToCryptedFile(string fname, unsigned char block[], int bSize, int fl ){
long fSize; int i, subF; if(fl == 0) ofstream fobj(fname.begin(), ios::out | ios::binary); ofstream fobj(fname.begin(), ios::app | ios::binary); fobj.write(block, bSize); }
__int16 Nod(Word b){ __int32 a = 0x10001; __int32 q, r; __int32 x1, x2, y1, y2, x, y; if(b == 0) return 0;
x2 = 1; x1 = 0; y2 = 0; y1 = 1;
while(b > 0){ q = a/b; r = a - q*b; x = x2 - q*x1; y = y2 - q*y1; a = b; b = r; x2 = x1; x1 = x; y2 = y1; y1 = y; } return y2; }
void MakeInvKey(__int16 KeyShedule[52], __int16 KeySheduleInv[52]){ int j, k=0;
for(int i=0; i<6; i++){ j=0; if(i >=4){ while(j<8){ KeySheduleInv[i+j*6] = KeyShedule[(46 + k - j*6)]; j++; } k++; } else while(j<9){ if( (i == 0) || (i == 3)){ //инверсия по умножению if(KeyShedule[(48+i-j*6)] == 0) KeySheduleInv[i+j*6] = 0; else KeySheduleInv[i+j*6] = Nod(KeyShedule[(48 + i - j*6)]); if(KeySheduleInv[i+j*6] < 0) KeySheduleInv[i+j*6] +=0x10001; } else{ if((j == 0) || (j == 8)) KeySheduleInv[i+j*6] = (0x10000 - KeyShedule[(48+i-j*6)])&0xffff; else{ if(i == 1) KeySheduleInv[i+j*6] = (0x10000 - KeyShedule[(48+i-j*6+1)])&0xffff; else KeySheduleInv[i+j*6] = (0x10000 - KeyShedule[(48+i-j*6-1)])&0xffff; } } j++; } } }
void CryptIdea(string fname1, string fname2, string fnameKey, bool way){ unsigned char a[8], aKey[16], b[8], tKey[16]; long fPointer=0; __int16 clearBlock[4], cipherBlock[4], key[8], keyShedule[52], keySheduleInv[52]; int fl=0;
readBlock(aKey, fnameKey, fPointer, 16); MakeDigMas(key, aKey, 16); makeKey(keyShedule, key);
MakeInvKey(keyShedule, keySheduleInv);
if(!way) for(int i=0; i<52; i++) keyShedule[i] = keySheduleInv[i];
fPointer = 0; while(readBlock(a, fname1, fPointer, 8)){ MakeDigMas(clearBlock, a, 8); IdeaCipher(keyShedule, clearBlock, cipherBlock); MakeDecMas(cipherBlock, b, 8); AddToCryptedFile(fname2, b, 8, fl ); fl++; } }
|
ну и вызов: | Код | void __fastcall TForm1::Button1Click(TObject *Sender) { CryptIdea(Edit1->Text.c_str(), Edit2->Text.c_str(), Edit3->Text.c_str(), true); MessageDlg("Шифрование успешно завершено.", mtInformation, TMsgDlgButtons() << mbOK, 0); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { CryptIdea(Edit1->Text.c_str(), Edit2->Text.c_str(), Edit3->Text.c_str(), false); MessageDlg("Дефрование успешно завершено.", mtInformation, TMsgDlgButtons() << mbOK, 0); }
|
помогите пожалуйста найти ошибку. на тестовом примере работает неправильно...
|