首页
社区
课程
招聘
[求助]812加密算法的另一子方案
发表于: 2013-8-22 14:51 6311

[求助]812加密算法的另一子方案

2013-8-22 14:51
6311
  812加密算法的另一种子方案,这里面没有用到随机函数,加密速度较快。欢迎感兴趣者讨论、批评。
  算法包含在下面的控制台小程序里。
运行方式
加密:
XXXX 被加密文件名  密码  1
解密:
XXXX 被解密文件名  密码  -1
注意:密码不得小于10个字节。
将下面的东西,拷贝到XXXX.CPP 或 XXXX.C 编译即可 用VC C-Free 集成编译器都可以,别的没试过。
/////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
void HanShu(char *filename,char *mima,int biaozhi);        //加密解密函数
int main(void)
{
char filename[240],mima[240];
int biaozhi;
        printf("请输入被加密文件名  密码  标志 : ");
        scanf("%s  %s  %d" ,&filename,&mima,&biaozhi);
        HanShu(filename,mima,biaozhi);       
        getch();                                //暂停显示
        return 0;
}
void HanShu(char *filename,char *mima,int biaozhi)
{
FILE *f;
unsigned char *ss;                                //文件容器
unsigned char *ss1;                        //密钥容器
unsigned char ch;
unsigned char ch1='\0';
unsigned char ss_my[256] ={
0x4f,0x1d,0x03,0xc7,0xa2,0x0d,0xd7,0xbb,0xd9,0x17,0x33,0x19,0x82,0xbf,0x9d,0x4a,0x2a,
0x51,0xe4,0x2e,0x9a,0x47,0xd4,0x3e,0x2b,0x59,0x43,0xc2,0xc9,0x92,0x00,0x07,0xf7,0xa3,
0x2c,0x10,0xc0,0x97,0xa7,0x1f,0x55,0x1c,0x21,0x49,0x58,0xa0,0xd2,0x0e,0x13,0x40,0x6c,
0xe6,0x22,0xdd,0x05,0x08,0x75,0xc6,0xd8,0x7a,0xcc,0x8d,0x37,0x70,0x8c,0x72,0x32,0x28,
0x53,0x6f,0x30,0x7b,0x80,0x6b,0x62,0xd0,0xec,0x9b,0x61,0xb9,0x52,0x93,0x74,0xb4,0xf2,
0xa9,0xe1,0x06,0xbd,0x41,0xcb,0x7f,0x36,0x25,0x35,0x46,0x77,0x54,0x73,0xf6,0x85,0x1a,
0xf0,0x26,0xb3,0x6e,0xac,0xf4,0xe2,0x90,0x88,0xce,0x71,0xcd,0x9c,0x09,0x18,0x9f,0x38,
0xb6,0x86,0x69,0xfd,0xde,0x99,0x57,0x76,0xee,0xb1,0x01,0xa8,0xd1,0x39,0x02,0x4c,0xfc,
0x94,0xef,0x7d,0x3b,0x48,0xc1,0x7e,0x24,0x15,0x5a,0x3d,0xc8,0x0b,0xaa,0x83,0x29,0xa6,
0x5d,0xe3,0x14,0x67,0xa5,0x23,0xbe,0x11,0xe5,0xab,0x79,0xda,0x8b,0xe9,0x44,0x1e,0xc3,
0xc4,0x95,0x84,0xbc,0xf9,0xcf,0x42,0xfe,0x2d,0x68,0xae,0x64,0xb0,0x04,0x16,0x4d,0x5b,
0x31,0x34,0xdb,0x0f,0x8f,0x0c,0x3c,0xf5,0xe0,0xb5,0x66,0xd6,0x5e,0x65,0xad,0x60,0xd3,
0x12,0x20,0x2f,0x87,0x6d,0x3a,0xd5,0x8e,0xb7,0x5c,0x5f,0x4e,0xf1,0xeb,0xc5,0x96,0x63,
0xdf,0xdc,0xe8,0xff,0x45,0x27,0x1b,0x4b,0xca,0x81,0x8a,0x89,0x78,0xaf,0x6a,0xfb,0x9e,
0x98,0xba,0x56,0xf8,0xa1,0x0a,0xb2,0xa4,0xb8,0x91,0xed,0x7c,0xea,0xf3,0x3f,0x50,0xe7,
0xfa};
unsigned char ss_my1[256]={0x51,0xe4,0x2e,0x9a,0x47,0xd4,0x3e,0x2b,0x59,0x43,0xc2,0xc9,0x92,0x4d,0xdf,0xfb,0xdc,0xe8,0xff,0x45,0x27,0x1b,0x4b,0xca,0x81,0x8a,0x89,0x78,0xaf,0x6a,0x2c,0x10,0xc0,0x97,0xa7,0x00,0x1f,0x55,0x1c,0x21,0x49,0x58,0xa0,0xd2,0x0e,0x13,0x40,0xb6,0x86,0x69,0xfd,0xde,0x99,0x57,0x76,0xee,0xb1,0x01,0xa8,0xd1,0x39,0x02,0x4c,0x9e,0xe6,0x22,0xdd,0x05,0x08,0x75,0xc6,0xd8,0x7a,0xcc,0x8d,0x37,0x70,0x8c,0x72,0x32,0x6c,0xa9,0x6f,0x30,0x7b,0x80,0x6b,0x62,0xd0,0xec,0x9b,0x61,0xb9,0x52,0x93,0x74,0xb4,0xfc,0x53,0xe1,0x06,0xbd,0x41,0xcb,0x7f,0x36,0x25,0x35,0x46,0x77,0x54,0x73,0xf6,0x85,0x28,0xf0,0x26,0xb3,0x6e,0xac,0xf4,0xe2,0x90,0x88,0xce,0x71,0xcd,0x9c,0x09,0x18,0x9f,0xf2,0x4f,0x1d,0x03,0xc7,0xa2,0x0d,0xd7,0xbb,0xd9,0x17,0x33,0x19,0x82,0xbf,0x9d,0x4a,0x1a,0x94,0xef,0x7d,0x3b,0x48,0xc1,0x7e,0x24,0x15,0x5a,0x3d,0xc8,0x0b,0xaa,0x83,0x29,0x38,0x5d,0xe3,0x14,0x67,0xa5,0x23,0xbe,0x11,0xe5,0xab,0x79,0xda,0x8b,0xe9,0x44,0x1e,0xa3,0x31,0x95,0x84,0xbc,0xf9,0xcf,0x42,0xfe,0x2d,0x68,0xae,0x64,0xb0,0x04,0x07,0x16,0xf7,0xc4,0x34,0xdb,0x0f,0x8f,0x0c,0x3c,0xf5,0xe0,0xb5,0x66,0xd6,0x5e,0x65,0xad,0x60,0x2a,0x12,0x20,0x2f,0x87,0x6d,0x3a,0xd5,0x8e,0xb7,0x5c,0x5f,0x4e,0xf1,0xeb,0xc5,0x96,0xa6,0x98,0xba,0x56,0xf8,0xa1,0x0a,0xb2,0xa4,0xb8,0x91,0xed,0x7c,0xea,0xf3,0x3f,0x50,0xc3,0x63,0xe7,0xfa,0xd3,0x5b};
unsigned char ss_my0[256]={0};
int b256=256;
int Length=0;
int i,j=0;
int n1=101;
        if((f=fopen(filename,"ab+"))==NULL)
        {printf("无法打开1");exit(0);}
        Length = filelength(_fileno(f));                //得到文件长度
            fclose(f);
        ss=new unsigned char[Length+256+1];                //文件容器
        ss1=new unsigned char[Length+256+1];        //密钥容器
        ss = (unsigned char*)calloc(Length+256, sizeof(char));   
        ss1 = (unsigned char*)calloc(Length+256, sizeof(char));   
        if((f=fopen(filename,"ab+"))==NULL)
        {printf("无法打开2");exit(0);}
        fseek(f,0,0);
        fread(ss,Length,1,f);        //文件内容读入 ss
        fclose(f);
        for(i=0;i<(int)mima[8];i++)        //关联用户密码
        {
                for(int j=0;j<b256;j++)        //单元串随机排序
                {
                n1=(n1+ss_my[j]+j+mima[j%strlen(mima)]+i)%b256;                //用户密码关联
                ch=ss_my[j];
                ss_my[j]=ss_my[n1];
                ss_my[n1]=ch;
                }
        }
        strcpy((char*)ss_my0,(char*)ss_my);
        for(i=0;i<Length;i=i+b256)                //密钥串建造
        {
                for(j=0;j<b256;j++)                //单元串随机排序
                {
//                n1=(ss_my1[j]+ss_my[n1]+ss_my[j]+j+i)%b256;
                n1=ss_my1[j];
                ch=ss_my[j];
                ss_my[j]=ss_my[n1];
                ss_my[n1]=ch;
                }
        strcpy((char*)ss_my1,(char*)ss_my);                //新组进入ss_my1
        strrev((char*)ss_my1);                        //ss_my1 倒序
                        if(ss_my[0]==ch1)                //边缘相等情况
                        {
                        ss_my[0]=ss_my[33+n1/3];        //换值
                        ss_my[33+n1/3]=ch1;
                        }
                        ch1=ss_my[255];                //记录边缘值
                        for(j=0;j<b256;j++)
                        {
                        ss1[i+j]=ss1[i+j]+ss_my[j];        //对结果串赋值
                        }
        }//for(i=0;i<Length;i=i+b256)
        for(i=0;i<Length;i++)
        {
        ss[i] = ss[i] + biaozhi*ss1[i];                //加密解密
        }
        ss1[Length]='\0';
        ss[Length]='\0';
        if((f=fopen(filename,"w"))==NULL)
        {printf("无法打开3");exit(0);}
        fclose(f);
        if((f=fopen(filename,"ab+"))==NULL)
        {printf("无法打开4");exit(0);}
        fseek(f,0,SEEK_SET);  
        fwrite(ss,Length,1,f);
        fclose(f);
        delete [] ss;
        delete [] ss1;
}

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//