首页 > 代码库 > (原创)随机串的产生,可以用于盐值

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

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

//产生长度为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; 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] = a + 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;  
}  

 

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