📜  在java代码示例中生成随机密码

📅  最后修改于: 2022-03-11 14:52:38.692000             🧑  作者: Mango

代码示例1
import java.util.Random;

public class PasswordGenerator {
    public static String generateRandomPassword(int len) {
        String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi"
          +"jklmnopqrstuvwxyz!@#$%&";
        Random rnd = new Random();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++)
            sb.append(chars.charAt(rnd.nextInt(chars.length())));
        return sb.toString();
    }
}