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

 之前要加密密码的时候,要加入盐值以加强强度,找了部分,又在网上的基础上修改了下(参考网页: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 - 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;  
}  

 

posted @ 2017-08-03 14:11  lydit  阅读(317)  评论(1编辑  收藏  举报