创建随机字符串并充当密码生成器的程序

时间:2015-02-24 20:12:02

标签: java string random

我正在编写此程序,要求用户输入密码长度,然后创建一个随机字符串,其长度使用字符。我坚持的唯一部分是它创建了多个字符串,我只需要一个。任何意见都将不胜感激。

import java.util.*;
public class pw
{
    public static void main(String[] arg) {
        Scanner in = new Scanner(System.in);
        Random gen = new Random();
        System.out.println("Enter password length:  ");
        int length = in.nextInt();

        for (int i = 1; i < length; i++) {
            int slength = length + gen.nextInt(length - 1);
            StringBuilder s = new StringBuilder(slength);

            while (s.length() <= length) {
                s.append((char) (33 + gen.nextInt(126)));
            }
            System.out.println(s.toString());
        }
    }
}

3 个答案:

答案 0 :(得分:1)

你不需要外部for循环,你正在循环,而#34; s&#34;小于或等于你需要的长度,因此不需要外环。

public static void main(String[] arg) {
    Scanner in = new Scanner(System.in);
    Random gen = new Random();
    System.out.println("Enter password length:  ");
    int length = in.nextInt();

    int slength = length + gen.nextInt(length - 1);
    StringBuilder s = new StringBuilder(slength);

    while (s.length() <= length) {
        s.append((char) (33 + gen.nextInt(126)));
    }

    System.out.println(s.toString());
}

答案 1 :(得分:1)

import java.security.SecureRandom;
import java.util.Scanner;

public class PasswordGenerator {

    public static void main(final String[] args) {
        final Scanner scanner = new Scanner(System.in);
        System.out.print("Desired password length: ");
        final int length = scanner.nextInt();

        final PasswordGenerator generator = new PasswordGenerator();
        final String password = generator.generatePassword(length);
        System.out.println("Password: " + password);
    }

    public String generatePassword(final int length) {
        final StringBuilder builder = new StringBuilder();
        final SecureRandom rng = new SecureRandom();
        for (int i = 0; i < length; i++) {
            final char value = (char) (rng.nextInt(94) + 33);
            builder.append(value);
        }
        return builder.toString();
    }
}

我将126更改为94,因为如果您为随机滚动125,则+33将为158. 158 % 127为31,这低于您想要的字符范围。

您也可以稍微重构一下,以便选择特定类型的密码要求。

public String generatePassword(final int length, final char[] characters) {
    final StringBuilder builder = new StringBuilder();
    final SecureRandom rng = new SecureRandom();
    for (int i = 0; i < length; i++) {
        final char value = characters[rng.nextInt(characters.length)];
        builder.append(value);
    }
    return builder.toString();
}

答案 2 :(得分:0)

这应该只生成一个字符串:

import java.util.*;

public class pw {
    public static void main(String[]arg) {
        Scanner in = new Scanner(System.in);
        Random gen = new Random(); 
        System.out.println("Enter password length:  ");
        int length = in.nextInt();

        String s = "";
        while(s.length() <= length) {
            s += (char)(33 + gen.nextInt(126));    
        }
        System.out.println(s);
    }
}