#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
static const char sel[] = {
'B','C','D','F','G',
'H','J','K','M','P',
'Q','R','T','V','W',
'X','Y','2','3','4',
'6','7','8','9', '\0'};
typedef unsigned int size_t;
int cd_code(unsigned char *in , int inlen , int number, int psw, char ser_sign, char chk)
{
if(in==NULL)
return -1;
unsigned char *all = in;
number = number << 11;
psw = psw << 11;
all[0] = (number >> 24);
all[1] = (number >> 16)&0x00FF;
all[2] = ((number >> 8)&0x0000F8) | (psw >> 29);
all[3] = (psw >> 21) & 0xFF;
all[4] = ((psw >> 13) & 0xFF) ;
all[5] = ((psw >> 5) & 0xc0) | (ser_sign & 0x3F) ;
//chk 3bit
all[6] = (chk & 0x07) << 5 ;
if(all == NULL)
return -2;
if(inlen != 7)
return -3;
return 0;
}
void restore_code(unsigned char *buf, int *number, int *psw, char * ser_sign, char *chk)
{
unsigned char *p = buf;
*number = ((p[0] << 24) | (p[1] << 16) | ((p[2] & 0xF8) << 8)) >> 11;
*psw = (((p[2] & 0x07) << 29) | (p[3] << 21) | (p[4] << 13) | ((p[5] >>6) << 11)) >> 11;
*ser_sign = p[5] & 0x3f;
*chk = (p[6]>>5) & 0x07;
}
/*
char *b24e(char *buf, unsigned char *byst, size_t sizeOfBytes)
{
int i = 0;
unsigned char *p = byst;
while ((size_t)(i = (p-byst)) < sizeOfBytes) {
buf[4*i] = sel[((*p) >> 6)];
buf[(4*i)+1] = sel[23 - (((*p) >> 4) & 003)];
buf[(4*i)+2] = sel[15 - (((*p) >> 2) & 0003)];
buf[(4*i)+3] = sel[7 - ((*p) & 00003)] ;
p++;
}
memset(&buf[(4*sizeOfBytes)],'\0',1);
return buf;
}
*/
/*
unsigned char *b24d(unsigned char *buf, char *str, size_t countOfChars)
{
size_t i;
char *p = str;
char *loc[4];
unsigned char n[4];
if (countOfChars % 4)
return NULL;
for (i = 0; i < (countOfChars>>2); i++) {
loc[0] = strchr( sel, str[4*i] );
loc[1] = strchr( sel, str[ ( 4*i ) + 1 ] );
loc[2] = strchr( sel, str[ (4*i) + 2]);
loc[3] = strchr( sel, str[ (4*i) + 3]);
if (loc[0] == NULL || loc[1] == NULL || loc[2] == NULL || loc[3] == NULL)
return NULL;
printf("--%c%c%c%c--\n",*loc[0], *loc[1], *loc[2], *loc[3]);
n[0] = (unsigned char)( loc[0] - sel );
//printf("%d,%c\n",n[0],*loc[0]);
n[1] = 23 - (unsigned char)( loc[1] - sel );
n[2] = 15 - (unsigned char)( loc[2] - sel );
n[3] = 7 - (unsigned char)( loc[3] - sel );
//printf("**%d-%d-%d-%d**\n",n[0],n[1],n[2],n[3]);
buf[i] = (unsigned char)((n[0] << 6) | (n[1] << 4) | (n[2] << 2) | n[3]);
}
return buf;
}
*/
char * base24_en(char *buf, unsigned char * byst, size_t sizeOfBytes)
{
int i = 0;
unsigned char *p = byst;
while ((size_t)(i = (p-byst)) < sizeOfBytes) {
buf[2*i] = sel[((*p) >> 4)];
buf[(2*i)+1] = sel[23 - ((*p) & 0x0f)];
p++;
}
buf[(2*i)+1] = '\0';
memset(&buf[2*i], '\0', 1);
return buf;
}
unsigned char *base24_de(unsigned char *buf, char *str, size_t countOfChars)
{
size_t i;
char *p = str;
char *loc[2];
int j;
for(j = 0; j< 2 ; j++)
{
loc[j] = NULL;
}
unsigned char n[2];
if (countOfChars % 2)
return NULL;
for (i = 0; i < (countOfChars>>1); i++) {
loc[0] = strchr( sel, str[2*i] );
loc[1] = strchr( sel, str[ ( 2*i ) + 1 ] );
if (loc[0] == NULL || loc[1] == NULL)
return NULL;
n[0] = (unsigned char)( loc[0] - sel );
n[1] = 23 - (unsigned char)( loc[1] - sel );
buf[i] = (unsigned char)((n[0] << 4) | n[1]);
}
return buf;
}
void main() {
/*
unsigned char *chkchars;
unsigned char *temp;
char output[512];
chkchars = (unsigned char *)malloc(7);
temp = (unsigned char *)malloc(7);
sprintf(chkchars,"%s","2541987");
base24_en(output, (unsigned char *)chkchars, 7);
printf("==%s==\n",output);
temp = base24_de(temp, output, 14);
printf("%s\n",temp);
*/
int a,b ;
char c, d;
unsigned char *chkchars;
unsigned char *temp;
char *output;
output = (char*)malloc(14);
temp = (unsigned char *)malloc(7);
chkchars = (unsigned char *)malloc(7);
int f;
int ret = cd_code(chkchars, 7, 2518,255255,4,7);
if(ret == 0)
base24_en(output, chkchars, 7);
printf("==%s==\n",output);
int xx;
xx = 7;
//YF_Base24Decrypt(output,14,temp, &xx);
temp = base24_de(temp, output, 14);
//printf("%s\n",temp);
restore_code(temp,&a, &b, &c ,&d);
printf("%d=%d=%d=%d=\n",a,b,c,d);
free(output);
free(temp);
free(chkchars);
output = NULL;
temp = NULL;
chkchars = NULL;
}
- 1
- 2
- 3
前往页