使用Android创建强大的密码生成器

时间:2017-01-26 06:12:04

标签: java android

因此,我正在构建一个根据用户的规范生成强密码的应用。用户界面的规范部分看起来像this

enter image description here

以下是MainActivity.java类中的onCreate。我尝试创建一些逻辑,例如检查某个单选按钮时的if语句,并在选中复选框时将允许的字符添加到String变量。 View对象都是全局btw但我无法弄清楚如何在用户设置的字符限制内使用每个允许字符中的至少一个来创建随机字符串。

以下是代码:

private static int MAX_LENGTH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        capitalLetter = (CheckBox) findViewById(R.id.capital_letter);
        lowercaseLetter = (CheckBox) findViewById(R.id.lowercase_letter);
        numbers = (CheckBox) findViewById(R.id.numbers);
        symbols = (CheckBox) findViewById(R.id.symbols);

        passGroup = (RadioGroup) findViewById(R.id.passRadioGroup);

        sizeFour = (RadioButton) findViewById(R.id.size_four);
        sizeEight = (RadioButton) findViewById(R.id.size_eight);
        sizeTwelve = (RadioButton) findViewById(R.id.size_twelve);
        sizeSixteen = (RadioButton) findViewById(R.id.size_sixteen);

        passHint = (EditText) findViewById(R.id.passwordHint);

        passShow = (TextView) findViewById(R.id.passwordDisplay);

        passGenerate = (Button) findViewById(R.id.passwordGenerate);
        passClear = (Button) findViewById(R.id.passwordClear);

        String allowedCharacters = "";

        // Determines the types of characters permitted when a check box is checked.
        if (capitalLetter.isChecked()) {allowedCharacters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";}
        if (lowercaseLetter.isChecked()) {allowedCharacters += "abcdefghijklmnopqrstuvwxyz";}
        if (numbers.isChecked()) {allowedCharacters += "0123456789";}
        if (symbols.isChecked()) {allowedCharacters += "!@#$%^&*()_-+=<>?/{}~|";}

        //Determines the length of the string based on which radio button the user has selected.
        int checkedRadioButtonId = passGroup.getCheckedRadioButtonId();

        if (checkedRadioButtonId == 1) {MAX_LENGTH = 4;}
        if (checkedRadioButtonId == 2) {MAX_LENGTH = 8;}
        if (checkedRadioButtonId == 3) {MAX_LENGTH = 12;}
        if (checkedRadioButtonId == 4) {MAX_LENGTH = 16;}
    }

3 个答案:

答案 0 :(得分:2)

关于生成随机字符串的基本思路:

private static final String ALLOWED_CHARACTERS ="0123456789qwertyuiopasdfghjklzxcvbnm";

private static String getRandomString(final int sizeOfPasswordString){
  final Random random=new Random();
  final StringBuilder sb=new StringBuilder(sizeOfPasswordString);

  for(int i=0;i<sizeOfRandomString;++i){
      sb.append(ALLOWED_CHARACTERS.charAt(random.nextInt(ALLOWED_CHARACTERS.length())));
    return sb.toString();
  }
}

答案 1 :(得分:0)

这是您正在寻找的。首先,您必须根据选中的复选框添加所需的字符,然后您必须使用整个允许字符串中的随机字符填充lefover长度。

() => String

用法:在为您生成密码的按钮的onclick实现中添加以下内容:

private static String generateRandomPassword(int max_length, boolean upperCase, boolean lowerCase, boolean numbers, boolean specialCharacters)
{
    String upperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String lowerCaseChars = "abcdefghijklmnopqrstuvwxyz";
    String numberChars = "0123456789";
    String specialChars = "!@#$%^&*()_-+=<>?/{}~|";
    String allowedChars = "";

    Random rn = new Random();
    StringBuilder sb = new StringBuilder(max_length);

    //this will fulfill the requirements of atleast one character of a type.
    if(upperCase) {
        allowedChars += upperCaseChars;
        sb.append(upperCaseChars.charAt(rn.nextInt(upperCaseChars.length()-1)));
    }

    if(lowerCase) {
        allowedChars += lowerCaseChars;
        sb.append(lowerCaseChars.charAt(rn.nextInt(lowerCaseChars.length()-1)));
    }

    if(numbers) {
        allowedChars += numberChars;
        sb.append(numberChars.charAt(rn.nextInt(numberChars.length()-1)));
    }

    if(specialCharacters) {
        allowedChars += specialChars;
        sb.append(specialChars.charAt(rn.nextInt(specialChars.length()-1)));
    }


    //fill the allowed length from different chars now.
    for(int i=sb.length();i < max_length;++i){
        sb.append(allowedChars.charAt(rn.nextInt(allowedChars.length())));
    }

    return  sb.toString();
}

答案 2 :(得分:0)

检查此答案:

private static final String NUMBERS = "0123456789";
private static final String UPPER_ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String LOWER_ALPHABETS = "abcdefghijklmnopqrstuvwxyz";
private static final String SPECIALCHARACTERS = "@#$%&*";
CheckBox chkCapital, chkSmall, chkNumber, chkSpec;
Button btnSUbmit;
int MAX_CHAR = 0;
private RadioGroup radioGroup;
private RadioButton radiobtnm;

private static String getRandomPasswordCharacters(int pos) {
    Random randomNum = new Random();
    StringBuilder randomChar = new StringBuilder();
    switch (pos) {
        case 0:
            randomChar.append(NUMBERS.charAt(randomNum.nextInt(NUMBERS.length() - 1)));
            break;
        case 1:
            randomChar.append(UPPER_ALPHABETS.charAt(randomNum.nextInt(UPPER_ALPHABETS.length() - 1)));
            break;
        case 2:
            randomChar.append(SPECIALCHARACTERS.charAt(randomNum.nextInt(SPECIALCHARACTERS.length() - 1)));
            break;
        case 3:
            randomChar.append(LOWER_ALPHABETS.charAt(randomNum.nextInt(LOWER_ALPHABETS.length() - 1)));
            break;
    }
    return randomChar.toString();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    chkCapital = (CheckBox) findViewById(R.id.checkBox1);
    chkSmall = (CheckBox) findViewById(R.id.checkBox2);
    chkNumber = (CheckBox) findViewById(R.id.checkBox3);
    chkSpec = (CheckBox) findViewById(R.id.checkBox4);


    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
    btnSUbmit = (Button) findViewById(R.id.btnSUbmit);

    btnSUbmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            StringBuilder password = new StringBuilder();
            ArrayList<Integer> passSel = new ArrayList<Integer>();
            int selectedId = radioGroup.getCheckedRadioButtonId();

            radiobtnm = (RadioButton) findViewById(selectedId);

            MAX_CHAR = Integer.parseInt(radiobtnm.getText().toString());

            // when  UPPER CASE selected
            if (chkCapital.isChecked())
                passSel.add(1);

            // when  LOWER CASE selected
            if (chkSmall.isChecked())
                passSel.add(3);

            // when  Number  selected
            if (chkNumber.isChecked())
                passSel.add(0);

            // when  Special selected
            if (chkSpec.isChecked())
                passSel.add(2);

            for (int i = 1; i <= MAX_CHAR; ) {

                if (passSel.contains(0) && i <= MAX_CHAR) {
                    password.append(getRandomPasswordCharacters(0));
                    i++;
                }
                if (passSel.contains(1) && i <= MAX_CHAR) {
                    password.append(getRandomPasswordCharacters(1));
                    i++;
                }
                if (passSel.contains(2) && i <= MAX_CHAR) {
                    password.append(getRandomPasswordCharacters(2));
                    i++;
                }
                if (passSel.contains(3) && i <= MAX_CHAR) {
                    password.append(getRandomPasswordCharacters(3));
                    i++;
                }
            }
            Log.e("!_@@:your password is :>--> ", password + "");
        }
    });
}