在Groovy中使用特殊字符创建强密码

de90aj5v  于 2022-12-22  发布在  其他
关注(0)|答案(3)|浏览(179)

我想创建一个强大的密码在groovy与字母都小和资本和数字和特殊字符。
以下是必需的特殊字符:
~`!@#$%^&*()-_=+[{]}|;:“”,<.>/?
我正在使用下面的代码,但我也希望至少有一个特殊字符在我的密码。

def pass_length = 15;

    def pool = ['a'..'z','A'..'Z',0..9,'_'].flatten();
    Random rand = new Random(System.currentTimeMillis());

    def passChars = (0..pass_length - 1).collect { pool[rand.nextInt(pool.size())] };
    def PASSWORD = passChars.join();

目前它只创建一个字母数字密码。任何快速的变化,我可以对代码?帮助我,因为我是新的使用groovy。

50few1ms

50few1ms1#

你可以选择一个随机的特殊字符,并把它放在生成的密码中的一个随机位置。这将确保密码中至少有一个特殊字符。
还有,为什么不把特殊字符也加到字典里呢?这样,特殊字符就有更多的可能出现在最终的字符串里。

def pass_length = 15;

def special = ['~' ,'`', ...] // you get the idea...
def pool = ['a'..'z','A'..'Z',0..9,'_'].flatten().plus(special);
Random rand = new Random(System.currentTimeMillis());

def passChars = (0..pass_length - 1).collect { pool[rand.nextInt(pool.size)] };
def specialChar = special[rand.nextInt(special.size)]
passChars[rand.nextInt(passChars.size)] = specialChar
def PASSWORD = passChars.join();
pw136qt2

pw136qt22#

以下代码使用指定的基本字母表和“特殊字符字母表”中指定数量的特殊字符生成指定长度的随机密码:

def alphabet    = ('a'..'z') + ('A'..'Z') + ('0'..'9')
def specials    = /~`!@#$%^&*()-_=+[{]}\|;:'",<.>\/?/

def random      = new Random()
def r           = { max -> random.nextInt(max) } // utility closure

// to test - generate 10 passwords and print them on stdout
10.times { 
  // generate a 15 character password with the given alphabet and specials 
  // set with one or two special characters randomly inserted
  def password = randomPassword(r, alphabet, specials, 15, (1..2))
  println password
}

def randomPassword(r, alphabet, specials, passwordLen, specialsRange) {
  // generate non-specials password
  def password  = randomChars(r, alphabet, passwordLen)

  // cointoss to determine number of specials
  def nSpecials = specialsRange.from + r(specialsRange.to-specialsRange.from+1)

  // insert that number of random specials picked from the specials set
  nSpecials.times { 
    password[r(password.size())] = specials[r(specials.size())]
  }

  password.join()
}

def randomChars(r, alphabet, length) {
  (1..length).collect { alphabet[r(alphabet.size())] }
}

和样品运行:

~> groovy solution.groovy 
06HT.Q5J4OOvN}S
U1h28bRcu{o)0l2
VxhMSD@6H7Qt(vH
9Bt!G2v.LT0u4ES
r1=RyWJO6R}x2Gl
sOBimChqOY3P1#x
5A]V4iiUQXkZdXv
R1iwlkZT-Ou;BrO
HbNAF>W4NRFSYRF
evuLb~ma%fzcSuA

~>
2hh7jdfx

2hh7jdfx3#

只是在这里分享这个记录。创建长度从6到100的密码(这些值可以在代码中更改)。

//Author: Rinaldi Michael
//Last Modified: 14th Dec 2022
//Password from length 6 to 100
 
import java.io.*
import java.lang.*
 
String passwords=""

            Random rnd = new Random()
            int randomNumber = rnd.nextInt(100)
            if(randomNumber==0 || randomNumber<6)
                randomNumber+=6
            String[] passwordCharacters=["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"
                      ,"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"
                      ,"!","@","%","^","&","*","(",")","-","+","=",".","/",">","<",";",":","|"]
     
            for(int p=0;p<randomNumber;)
            {
                try
                {
                passwords = passwords+passwordCharacters[rnd.nextInt(passwordCharacters.size())]
                p++
                }
                catch(Exception ex)
                {
                    p++
                }
            }
 
return passwords

相关问题