折腾了2天的一个加密算法!使用了二进制和十六进制的转换!好玩很!bintohex hextobin bin2hex hex2bin
哦花了2天时间折腾出来的!老不对!比较笨。如果对您有帮助请还回来顶下!谢谢!
Code
1 #include "stdafx.h"
2 #include "string.h"
3 typedef unsigned char BYTE;
4 typedef unsigned short int WORD;
5
6 /*****************************
7 Code Text To Password
8 Created by ricky.w(rikyok@126.com)
9 2008.8.29
10 *****************************/
11 char * Encode(char * Pwd_Dest,const char * Pwd_Src)
12 {
13 if ((Pwd_Dest==NULL)||(Pwd_Src==NULL))
14 {
15 printf("errro");
16 }
17 char * pDest=Pwd_Dest;
18 BYTE pkey[128];
19 memset(pkey,128,0);
20 char key[]="Lgp741023Hs720122Esunny";
21 memcpy(pkey,key,sizeof(key));
22 BYTE Epass[200];
23 memset(Epass,200,0);
24 memcpy(Epass,Pwd_Src,36);
25 Epass[36]='\0';
26 printf("EpassA: %s\n",Epass);
27 int len=36;
28 int i,j;
29 int key_len;
30 WORD ui1,ui2;
31 key_len=strlen(key);
32 for(i=0;i<len;i++)
33 {
34 ui1=0;
35 ui2=0;
36 for(j=0;j<key_len;j++)
37 {
38 ui1+=(BYTE)(pkey[j]);
39 ui2^=(BYTE)(pkey[j]);
40 }
41 pkey[key_len]=(BYTE)((ui1+ui2)%255);
42 Epass[i]=(BYTE)( (BYTE)Epass[i]^(BYTE)pkey[key_len] );
43 //printf("Epass[i]:%c\n",Epass[i]);
44 for(j=0;j<key_len;j++)
45 {
46 pkey[j]=pkey[j+1];
47 }
48 }
49 printf("EpassE: %s\n",Epass);
50 BYTE Epassf[73];
51 memset(Epassf,73,0);
52 char* Convert = "0123456789ABCDEF";
53 int z;
54 int t=0;
55 for(z=0;z<len;z++)
56 {
57 Epassf[t]=Convert[(BYTE)Epass[z] >> 4];
58 Epassf[t+1]=Convert[(BYTE)Epass[z] & 0xf];
59 t+=2;
60 }
61 Epassf[t]='\0';
62 int ips=0;
63 while ((*Pwd_Dest++=Epassf[ips])!='\0')
64 {
65 ips++;
66 };
67 return pDest;
68 }
69
70 /****************************
71 Decode Password
72 Code Text To Password
73 Created by ricky.w(rikyok@126.com)
74 2008.8.29
75 *****************************/
76
77 short int Convert(char in)
78 {
79 if( in>=48 && in<=57)
80 return (short int)(in-48);
81 if( in>=65 && in<=70)
82 return (short int)(in-55);
83 }
84
85 char * Decode(char * Pwd_Dest,const char * Pwd_Src)
86 {
87 if ((Pwd_Dest==NULL)||(Pwd_Src==NULL))
88 {
89 printf("errro");
90 }
91
92 char * pDest=Pwd_Dest;
93 BYTE pkey[128];
94 memset(pkey,128,0);
95 char key[]="Lgp741023Hs720122Esunny";
96 memcpy(pkey,key,sizeof(key));
97 BYTE Epass[200];
98 memset(Epass,200,0);
99 memcpy(Epass,Pwd_Src,73);
100 Epass[73]='\0';
101 // printf("Epass: %s\n",Epass);
102
103 BYTE Epassf[80];
104 memset(Epassf,80,0);
105
106
107 int len=36;
108 int K=len;
109 int x=0;
110 int m=0;//36
111 int n=0;//72
112
113 while(K>0)
114 {
115 bool ret0=false;
116 bool ret1=false;
117
118 Epassf[m] = (BYTE)( (short int)(Convert((BYTE)Epass[n]) << 4) + (BYTE)Convert( (BYTE)Epass[n+1]) );
119 n+=2;
120 m+=1;
121 K-=1;
122 }
123 Epassf[m]='\0';
124 // printf("Epassf: %s\n",Epassf);
125
126 BYTE EpassR[200];
127 memset(EpassR,200,0);
128 memcpy(EpassR,Epassf,36);
129 Epass[36]='\0';
130
131
132 int i,j;
133 int key_len;
134 WORD ui1,ui2;
135 key_len=strlen(key);
136 for(i=0;i<len;i++)
137 {
138 ui1=0;
139 ui2=0;
140 for(j=0;j<key_len;j++)
141 {
142 ui1+=(BYTE)(pkey[j]);
143 ui2^=(BYTE)(pkey[j]);
144 }
145 pkey[key_len]=(BYTE)((ui1+ui2)%255);
146 EpassR[i]=(BYTE)( (BYTE)EpassR[i]^(BYTE)pkey[key_len] );
147 //printf("Epass[i]:%c\n",Epass[i]);
148 for(j=0;j<key_len;j++)
149 {
150 pkey[j]=pkey[j+1];
151 }
152 }
153 EpassR[len]='\0';
154 // printf("EpassR: %s\n",EpassR);
155 int ips=0;
156 while ((*Pwd_Dest++=EpassR[ips])!='\0')
157 {
158 ips++;
159 };
160 return pDest;
161 }
162
163
164
165 int _tmain(int argc, _TCHAR* argv[])
166 {
167 char in[]="000000 TEST "; //15555173CAB07E0E1616A2E035FCFD16161E3ECB84A186C93B0BC94C589787272F73EB8F
168 char out[200];
169 Encode(out,in);
170 printf("word '%s'passd as:\n%s\n",in,out);
171
172 char en[]="15555173CAB07E0E1616A2E035FCFD16161E3ECB84A186C93B0BC94C589787272F73EB8F";
173 char de[200];
174 Decode(de,en);
175 printf("word '%s'depassd as:\n%s\n",en,de);
176
177
178 return 0;
179 }
180
1 #include "stdafx.h"
2 #include "string.h"
3 typedef unsigned char BYTE;
4 typedef unsigned short int WORD;
5
6 /*****************************
7 Code Text To Password
8 Created by ricky.w(rikyok@126.com)
9 2008.8.29
10 *****************************/
11 char * Encode(char * Pwd_Dest,const char * Pwd_Src)
12 {
13 if ((Pwd_Dest==NULL)||(Pwd_Src==NULL))
14 {
15 printf("errro");
16 }
17 char * pDest=Pwd_Dest;
18 BYTE pkey[128];
19 memset(pkey,128,0);
20 char key[]="Lgp741023Hs720122Esunny";
21 memcpy(pkey,key,sizeof(key));
22 BYTE Epass[200];
23 memset(Epass,200,0);
24 memcpy(Epass,Pwd_Src,36);
25 Epass[36]='\0';
26 printf("EpassA: %s\n",Epass);
27 int len=36;
28 int i,j;
29 int key_len;
30 WORD ui1,ui2;
31 key_len=strlen(key);
32 for(i=0;i<len;i++)
33 {
34 ui1=0;
35 ui2=0;
36 for(j=0;j<key_len;j++)
37 {
38 ui1+=(BYTE)(pkey[j]);
39 ui2^=(BYTE)(pkey[j]);
40 }
41 pkey[key_len]=(BYTE)((ui1+ui2)%255);
42 Epass[i]=(BYTE)( (BYTE)Epass[i]^(BYTE)pkey[key_len] );
43 //printf("Epass[i]:%c\n",Epass[i]);
44 for(j=0;j<key_len;j++)
45 {
46 pkey[j]=pkey[j+1];
47 }
48 }
49 printf("EpassE: %s\n",Epass);
50 BYTE Epassf[73];
51 memset(Epassf,73,0);
52 char* Convert = "0123456789ABCDEF";
53 int z;
54 int t=0;
55 for(z=0;z<len;z++)
56 {
57 Epassf[t]=Convert[(BYTE)Epass[z] >> 4];
58 Epassf[t+1]=Convert[(BYTE)Epass[z] & 0xf];
59 t+=2;
60 }
61 Epassf[t]='\0';
62 int ips=0;
63 while ((*Pwd_Dest++=Epassf[ips])!='\0')
64 {
65 ips++;
66 };
67 return pDest;
68 }
69
70 /****************************
71 Decode Password
72 Code Text To Password
73 Created by ricky.w(rikyok@126.com)
74 2008.8.29
75 *****************************/
76
77 short int Convert(char in)
78 {
79 if( in>=48 && in<=57)
80 return (short int)(in-48);
81 if( in>=65 && in<=70)
82 return (short int)(in-55);
83 }
84
85 char * Decode(char * Pwd_Dest,const char * Pwd_Src)
86 {
87 if ((Pwd_Dest==NULL)||(Pwd_Src==NULL))
88 {
89 printf("errro");
90 }
91
92 char * pDest=Pwd_Dest;
93 BYTE pkey[128];
94 memset(pkey,128,0);
95 char key[]="Lgp741023Hs720122Esunny";
96 memcpy(pkey,key,sizeof(key));
97 BYTE Epass[200];
98 memset(Epass,200,0);
99 memcpy(Epass,Pwd_Src,73);
100 Epass[73]='\0';
101 // printf("Epass: %s\n",Epass);
102
103 BYTE Epassf[80];
104 memset(Epassf,80,0);
105
106
107 int len=36;
108 int K=len;
109 int x=0;
110 int m=0;//36
111 int n=0;//72
112
113 while(K>0)
114 {
115 bool ret0=false;
116 bool ret1=false;
117
118 Epassf[m] = (BYTE)( (short int)(Convert((BYTE)Epass[n]) << 4) + (BYTE)Convert( (BYTE)Epass[n+1]) );
119 n+=2;
120 m+=1;
121 K-=1;
122 }
123 Epassf[m]='\0';
124 // printf("Epassf: %s\n",Epassf);
125
126 BYTE EpassR[200];
127 memset(EpassR,200,0);
128 memcpy(EpassR,Epassf,36);
129 Epass[36]='\0';
130
131
132 int i,j;
133 int key_len;
134 WORD ui1,ui2;
135 key_len=strlen(key);
136 for(i=0;i<len;i++)
137 {
138 ui1=0;
139 ui2=0;
140 for(j=0;j<key_len;j++)
141 {
142 ui1+=(BYTE)(pkey[j]);
143 ui2^=(BYTE)(pkey[j]);
144 }
145 pkey[key_len]=(BYTE)((ui1+ui2)%255);
146 EpassR[i]=(BYTE)( (BYTE)EpassR[i]^(BYTE)pkey[key_len] );
147 //printf("Epass[i]:%c\n",Epass[i]);
148 for(j=0;j<key_len;j++)
149 {
150 pkey[j]=pkey[j+1];
151 }
152 }
153 EpassR[len]='\0';
154 // printf("EpassR: %s\n",EpassR);
155 int ips=0;
156 while ((*Pwd_Dest++=EpassR[ips])!='\0')
157 {
158 ips++;
159 };
160 return pDest;
161 }
162
163
164
165 int _tmain(int argc, _TCHAR* argv[])
166 {
167 char in[]="000000 TEST "; //15555173CAB07E0E1616A2E035FCFD16161E3ECB84A186C93B0BC94C589787272F73EB8F
168 char out[200];
169 Encode(out,in);
170 printf("word '%s'passd as:\n%s\n",in,out);
171
172 char en[]="15555173CAB07E0E1616A2E035FCFD16161E3ECB84A186C93B0BC94C589787272F73EB8F";
173 char de[200];
174 Decode(de,en);
175 printf("word '%s'depassd as:\n%s\n",en,de);
176
177
178 return 0;
179 }
180