(原创)随机串的产生,可以用于盐值

之前要加密密码的时候,要加入盐值以加强强度,找了部分,又在网上的基础上修改了下(参考网页:http://blog.csdn.net/wangchangshuai0010/article/details/17188417)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//产生长度为length的随机字符串

 char pSymbol[] = {'~','`','!','@','#','$','%','^','&',
    '*','(',')','_','+','-','=','{','}','|','[',']',
    ':',';','<','>','?',
    ',','.','\"','\\','\'','/'};
char* getRandomString(int length)
{
int nRand, i;
char* string;
srand((unsigned) time(NULL ));
if ((string = (char*) malloc(length + 1)) == NULL )
return NULL ;

for (i = 0; i < length - 1; i++)
{
nRand = rand() % (sizeof(pSymbol) + 26 * 2 + 10);
if (nRand >= 0)
{
if (nRand < 26)
string[i] = 'A' + nRand;
else if (nRand < 52)
string[i] = 'a' + nRand - 26;
else if (nRand < 62)
string[i] = '1' + nRand - 52;
else if(nRand - 62 < sizeof(pSymbol))
string[i] = pSymbol[nRand - 62];
else
string[i] = 'x';
}
else
string[i] = 'x';
}
string[length] = '\0';
return string;
}

多谢打赏
-------------本文结束感谢您的阅读-------------
0%