一个C写的加密方法,改写成javascript
C的加密方法如下:char * strencrypt0(const char * szsrc, const char * szkey, char * szresult)
{
char szkey1[PWD_MAX_LEN * 2 + 1];
char szsrc1[PWD_MAX_LEN * 2 + 1];
char bit1[PWD_MAX_LEN * 8 + 1];
char bit2[PWD_MAX_LEN * 8 + 1];
char bit3[PWD_MAX_LEN * 2+ 1];
int i, j, d;
int h1, h2;
memset(szkey1, 0, sizeof(szkey1));
memset(szsrc1, 0, sizeof(szsrc1));
memset(bit1, 0, sizeof(bit1));
memset(bit2, 0, sizeof(bit2));
memset(bit3, 0, sizeof(bit3));
strncpy( szkey1, szkey, 10 );
strncat( szkey1, "1234567890", 10-strlen(szkey1) );
strncpy(szsrc1, szsrc, 10);
strrev(szsrc1);
strcat(szsrc1, "\x5f\x3c\x5e\x40\x5d\x3f\x5c\x3e\x5b\x3d");
szsrc1[PWD_MAX_LEN] = 0;
strrev(szsrc1);
for(i = 0; i < PWD_MAX_LEN; i ++)
{
d = szsrc1[i] ^ szkey1[i];
if(i == 0)
{
h1 = 237;
h2 = 222;
}
else if(i == 1)
{
h1 = 88;
h2 = 125;
}
else
{
h1 = szsrc1[i - 1];
h2 = szsrc1[i - 2];
}
d = (d ^ h1) ^ h2;
for(j = 7; j >= 0; j --)
{
if(d & (1 < < j))
bit1[strlen(bit1)] = '1';
else
bit1[strlen(bit1)] = '0';
}
}
for(i = 0; i < 16; i ++)
{
for(j = 0; j < 5; j ++)
{
bit2[strlen(bit2)] = bit1[j * 16 + i];
}
}
for(i = 0; i < 20; i ++)
{
d = 'A';
for(j = 0; j < 4; j ++)
{
if(bit2[i * 4 + j] == '1')
d = d + (1 < < (3 - j));
}
bit3[i] = (char)d;
}
strcpy(szresult, bit3);
return szresult;
}