|
原理:生成小于10的随机数,把输入字符按条件加上对应的随机数和位置号,最后把生成字符+随机数生成密码输出。
代码如下(示例仅使用2个随机数):
CString input = "123456";
int num = strlen(input);
char str[100];
Rand1 = rand() % 8 + 1;
Rand2 = rand() % 9;
for (int i = 0; i < num + 1; i++)
{
if (i < num)
{
if (i % 2 == 0)
{
ascii = (int)input[i] + Rand1 + i;
}
else if (i % 2 == 1)
{
ascii = (int)input[i] + Rand2 + i;
}
if (ascii > 126)
{
ascii = ascii - 126 + 32;
}
str[i] = (char)ascii;
}
else
{
str[i] = (char)(Rand1 + (rand() % 7 + 4) * 10);
str[i + 1] = (char)(Rand2 + (rand() % 7 + 4) * 10);
}
}
input.Format("%s", str);
CString output = input.Left(num + 2);
二、解码
原理:解析密码中的随机数,按加密规则逆向解密。
代码如下:
char str[100];
int num = strlen(inputRaw); //inputRaw: CString,加密字符
CString input = inputRaw.Right(2);
Rand1 = input[0] % 10;
Rand2 = input[1] % 10;
input = inputRaw.Left(num - 2);
for (int i = 0; i < num - 2; i++)
{
if (i % 2 == 0)
{
ascii = (int)input[i] - Rand1 - i;
}
else
{
ascii = (int)input[i] - Rand2 - i;
}
if (ascii < 33)
{
ascii = ascii + 126 - 32;
}
str[i] = (char)ascii;
}
inputRaw.Format("%s", str);
CString output = inputRaw.Left(num - 2);
|
|