⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 DES完全自己实现!能对输入字符串或者选定实现CBC
💻 CPP
📖 第 1 页 / 共 4 页
字号:
                                        bool_ptext=(byte_ptext>>(7-j%8))&1;
                                        bool_ptext=bool_ptext^c_tmp[0];
                                        bool_crytext[j%8]=bool_ptext;
                                        if(j%8==7)
                                        {
                                                btbyte(&byte_crytext,bool_crytext,8);
                                        }
                                        for(k=0;k<63;k++)
                                                iv[k]=iv[k+1];
                                        iv[63]=bool_ptext;
                                }
                                plaintext[i]=byte_crytext;
                        }
                        fwrite(plaintext,1,dwCount,fDestination);
                }
        }
        else if(RadioButton7->Checked==true)
        {
                BOOL c_bool[8];
                BYTE c_print;
                BOOL c_tmp[64];                           //CFB-8模式
                BYTE plaintext[64];
                DWORD dwCount;
                Screen->Cursor=crHourGlass;
                while(feof(fSourse)==0)
                {
                        dwCount=fread(plaintext,1,64,fSourse);
                        if(dwCount==0)
                                break;
                        all_len+=dwCount;
                        for(i=0;i<dwCount;i++)
                        {
                                for(k=0;k<64;k++)
                                        c_tmp[k]=iv[k];
                                en_decrypt(c_tmp,key,'e');
                                btbit(c_bool,&plaintext[i],8);
                                xor(c_tmp,c_bool,8);
                                btbyte(&c_print,c_tmp,8);
                                for(k=0;k<56;k++)
                                        iv[k]=iv[k+8];
                                for(k=0;k<8;k++)
                                        iv[56+k]=c_tmp[k];
                                plaintext[i]=c_print;
                        }
                        fwrite(plaintext,1,dwCount,fDestination);
                }
                Screen->Cursor=crDefault;
        }
        else if(RadioButton8->Checked==true)
        {
                BOOL c_bool[64];
                BYTE c_print[8];
                BOOL c_tmp[64];
                BYTE plaintext[64];                          //CFB-64模式
                DWORD dwCount;

                Screen->Cursor=crHourGlass;
                while(feof(fSourse)==0)
                {
                        dwCount=fread(plaintext,1,64,fSourse);
                        if(dwCount==0)
                                break;
                        all_len+=dwCount;
                        for(i=0;i<(dwCount-1)/8+1;i++)
                        {
                                for(k=0;k<64;k++)
                                        c_tmp[k]=iv[k];
                                en_decrypt(c_tmp,key,'e');
                                btbit(c_bool,&plaintext[8*i],64);
                                xor(c_tmp,c_bool,64);
                                btbyte(c_print,c_tmp,64);
                                for(k=0;k<64;k++)
                                        iv[k]=c_tmp[k];
                                if(i==(dwCount-1)/8)
                                {
                                        j=(dwCount%8==0)?8:dwCount%8;
                                        for(k=0;k<j;k++)
                                                plaintext[8*i+k]=c_print[k];
                                }
                                else
                                {
                                        for(k=0;k<8;k++)
                                                plaintext[8*i+k]=c_print[k];
                                }
                          }
                        fwrite(plaintext,1,dwCount,fDestination);
                }
                Screen->Cursor=crDefault;
        }
        fclose(fSourse);
        fclose(fDestination);

        end=GetTickCount();
        effic=(end-start)*1.0/1000/all_len;
        String tmp="              加密完成!";
        tmp+='\n';
        tmp+="运行速度:";
        tmp+=FloatToStrF(effic,ffFixed,0,7);
        tmp+="sec/byte!";
        MessageBox(Handle,tmp.c_str(),"提示:",MB_OK|MB_ICONEXCLAMATION);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Decrypt2Click(TObject *Sender)
{
        if(fileopen=="")
        {
                Application->MessageBoxA("请先设置源文件!","提示:",0);
                return;
        }
        if(filesave=="")
        {
                Application->MessageBoxA("请先设置目标文件!","提示:",0);
                return;
        }

        DWORD len,len2,i,j,k,pi;
        BYTE crypt[8];

        DWORD start,end;
        DWORD all_len=0;
        double effic;
        start=GetTickCount();


        BOOL key[64];
        BOOL iv[64];
        iv_keychange(key,iv,Edit3->Text.c_str(),Edit4->Text.c_str());


        FILE *fSourse,*fDestination;
        fSourse=fopen(fileopen.c_str(),"rb");
        fDestination=fopen(filesave.c_str(),"wb");

        if(RadioButton5->Checked==true)
        {
                BOOL text[64];
                BOOL iv_tmp[64];
                BYTE plaintext[64];
                DWORD dwCount;

                Screen->Cursor=crHourGlass;
                while(feof(fSourse)==0)
                {
                        dwCount=fread(plaintext,1,64,fSourse);
                        if(dwCount==0)
                                break;
                        all_len+=dwCount;
                        for(i=0;i<dwCount/8;i++)
                        {
                                btbit(text,&plaintext[8*i],64);
                                for(j=0;j<64;j++)
                                {
                                        iv_tmp[j]=text[j];
                                }
                                en_decrypt(text,key,'d');
                                xor(text,iv,64);
                                for(j=0;j<64;j++)
                                {
                                        iv[j]=iv_tmp[j];
                                }
                                btbyte(crypt,text,64);
                                for(j=0;j<8;j++)
                                        plaintext[8*i+j]=crypt[j];
                        }
                        if(crypt[7]>=1&&crypt[7]<=7)
                                k=crypt[7];
                        else
                                k=8;
                        fwrite(plaintext,1,dwCount-k%8,fDestination);
                }
                Screen->Cursor=crDefault;
        }
        else if(RadioButton6->Checked==true)
        {
                BOOL c_tmp[64];
                BYTE byte_ptext;
                BOOL bool_ptext;
                BOOL bool_crytext[8];
                BYTE byte_crytext;
                BYTE plaintext[64];
                DWORD dwCount;

                Screen->Cursor=crHourGlass;
                while(feof(fSourse)==0)
                {
                        dwCount=fread(plaintext,1,64,fSourse);
                        if(dwCount==0)
                                break;
                        all_len+=dwCount;
                        for(i=0;i<dwCount;i++)
                        {
                                for(j=0;j<8;j++)
                                {
                                        for(k=0;k<64;k++)
                                                c_tmp[k]=iv[k];
                                        en_decrypt(c_tmp,key,'e');
                                        byte_ptext=plaintext[i];
                                        bool_ptext=(byte_ptext>>(7-j%8))&1;
                                        for(k=0;k<63;k++)
                                                iv[k]=iv[k+1];
                                        iv[63]=bool_ptext;
                                        bool_ptext=bool_ptext^c_tmp[0];
                                        bool_crytext[j%8]=bool_ptext;
                                        if(j%8==7)
                                        {
                                                btbyte(&byte_crytext,bool_crytext,8);
                                        }
                                }
                                plaintext[i]=byte_crytext;
                        }
                        fwrite(plaintext,1,dwCount,fDestination);
                }
                Screen->Cursor=crDefault;
        }
        else if(RadioButton7->Checked==true)
        {
                BOOL c_bool[8];
                BYTE c_print;
                BOOL c_tmp[64];
                BYTE plaintext[64];
                DWORD dwCount;

                Screen->Cursor=crHourGlass;
                while(feof(fSourse)==0)
                {
                        dwCount=fread(&plaintext,1,64,fSourse);
                        if(dwCount==0)
                                break;
                        all_len+=dwCount;
                        for(i=0;i<dwCount;i++)
                        {
                                for(k=0;k<64;k++)
                                        c_tmp[k]=iv[k];
                                en_decrypt(c_tmp,key,'e');
                                btbit(c_bool,&plaintext[i],8);
                                xor(c_tmp,c_bool,8);
                                btbyte(&c_print,c_tmp,8);
                                for(k=0;k<56;k++)
                                        iv[k]=iv[k+8];
                                for(k=0;k<8;k++)
                                        iv[56+k]=c_bool[k];
                                plaintext[i]=c_print;
                        }
                        fwrite(plaintext,1,dwCount,fDestination);
                }
                Screen->Cursor=crDefault;
        }
        else if(RadioButton8->Checked==true)
        {
                BOOL c_bool[64];
                BYTE c_print[8];
                BOOL c_tmp[64];
                BYTE plaintext[64];
                DWORD dwCount;

                Screen->Cursor=crHourGlass;
                while(feof(fSourse)==0)
                {
                        dwCount=fread(plaintext,1,64,fSourse);
                        if(dwCount==0)
                                break;
                        all_len+=dwCount;
                        for(i=0;i<(dwCount-1)/8+1;i++)
                        {
                                for(k=0;k<64;k++)
                                        c_tmp[k]=iv[k];
                                en_decrypt(c_tmp,key,'e');
                                btbit(c_bool,&plaintext[8*i],64);
                                xor(c_tmp,c_bool,64);
                                btbyte(c_print,c_tmp,64);
                                for(k=0;k<64;k++)
                                        iv[k]=c_bool[k];
                                if(i==(dwCount-1)/8)
                                {
                                        j=(dwCount%8==0)?8:dwCount%8;
                                        for(k=0;k<j;k++)
                                                plaintext[8*i+k]=c_print[k];
                                }
                                else
                                {
                                        for(k=0;k<8;k++)
                                                plaintext[8*i+k]=c_print[k];
                                }
                        }
                        fwrite(plaintext,1,dwCount,fDestination);
                }
                Screen->Cursor=crDefault;
        }
        fclose(fSourse);
        fclose(fDestination);


        end=GetTickCount();
        effic=(end-start)*1.0/1000/all_len;
        String tmp="              解密完成!";
        tmp+='\n';
        tmp+="运行速度:";
        tmp+=FloatToStrF(effic,ffFixed,0,7);
        tmp+="sec/byte!";
        MessageBox(Handle,tmp.c_str(),"提示:",MB_OK|MB_ICONEXCLAMATION);

}
//---------------------------------------------------------------------------



void __fastcall TForm1::Exit1Click(TObject *Sender)
{
        Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Exit2Click(TObject *Sender)
{
        Close();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -