折腾了2天的一个加密算法!使用了二进制和十六进制的转换!好玩很!bintohex hextobin bin2hex hex2bin...

 

哦花了2天时间折腾出来的!老不对!比较笨。如果对您有帮助请还回来顶下!谢谢!

ContractedBlock.gif ExpandedBlockStart.gif 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     ifin>=48 && in<=57)
 80         return (short int)(in-48);
 81     ifin>=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 

转载于:https://www.cnblogs.com/riky/archive/2008/08/29/1279240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个二进制转换十六进制的示例: ```c #include <stdio.h> #include <string.h> // 将二进制字符串转换十六进制字符串 char* binToHex(const char* bin) { static const char* hex = "0123456789ABCDEF"; static char result[256]; int len = strlen(bin); int i, j; for (i = 0, j = 0; i < len; i += 4) { int n = 0; n |= (bin[i + 0] - '0') << 3; n |= (bin[i + 1] - '0') << 2; n |= (bin[i + 2] - '0') << 1; n |= (bin[i + 3] - '0') << 0; result[j++] = hex[n]; } result[j] = '\0'; return result; } int main() { char* bin = "1101010111101101"; char* hex = binToHex(bin); printf("%s -> %s\n", bin, hex); return 0; } ``` 输出为:`1101010111101101 -> DAD`。 以下是一个十六进制转换二进制的示例: ```c #include <stdio.h> #include <string.h> // 将十六进制字符串转换二进制字符串 char* hexToBin(const char* hex) { static char result[256]; int len = strlen(hex); int i, j; for (i = 0, j = 0; i < len; i++) { char c = hex[i]; if (c >= '0' && c <= '9') { result[j++] = (c - '0') & 0x01 ? '1' : '0'; result[j++] = (c - '0') & 0x02 ? '1' : '0'; result[j++] = (c - '0') & 0x04 ? '1' : '0'; result[j++] = (c - '0') & 0x08 ? '1' : '0'; } else if (c >= 'A' && c <= 'F') { result[j++] = (c - 'A' + 10) & 0x01 ? '1' : '0'; result[j++] = (c - 'A' + 10) & 0x02 ? '1' : '0'; result[j++] = (c - 'A' + 10) & 0x04 ? '1' : '0'; result[j++] = (c - 'A' + 10) & 0x08 ? '1' : '0'; } } result[j] = '\0'; return result; } int main() { char* hex = "DAD"; char* bin = hexToBin(hex); printf("%s -> %s\n", hex, bin); return 0; } ``` 输出为:`DAD -> 1101010111101101`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值