前言:网上没有太好的直接可用的方法,于是自己写了一个,下面的方法可以根据字母,数字和符号组合生成指定长度密码,策略和长度可控,详细代码如下:
/// <summary> /// 生成密码 /// </summary> /// <param name="zmLength">小写字母长度</param> /// <param name="upzmLength">大写字母长度</param> /// <param name="szLength">数字长度</param> /// <param name="zfLength">字符长度</param> /// <returns></returns> public static string BuildNewPwd(int zmLength, int upzmLength, int szLength, int zfLength) { char[] zmArray = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; char[] upzmArray = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; char[] szArray = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char[] zfArray = new char[] { '%', '_', '.' }; Random random = new Random(); string pwd = ""; Action<int, char[]> updatePwdAction = (insertCount, dataArray) => { for (int i = 0; i < insertCount; i++) { int dataIndex = random.Next(0, dataArray.Length); char data = dataArray[dataIndex]; int insertIndex = random.Next(0, (pwd.Length == 0 ? 1 : pwd.Length)); pwd = pwd.Insert(insertIndex, data.ToString()); } }; updatePwdAction(zmLength, zmArray); updatePwdAction(upzmLength, upzmArray); updatePwdAction(szLength, szArray); updatePwdAction(zfLength, zfArray); return pwd; }
标签:符号,int,字母,长度,char,密码,pwd,updatePwdAction,new From: https://www.cnblogs.com/lxhbky/p/17470045.html