如何使用字符串排列生成密码

时间:2013-05-31 02:34:52

标签: java passwords generator

我想用Java构建一个非常基本的密码生成器。我需要在String中设置所有需要的字符。我想生成字符串中所有可能的字符排列,密码长度为1-256个字符。

编辑,人们想要一些代码:

String ascii = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+.-/:;<=>?@[]^_{|}~";
    for (int i = 0; i < 255; i++) {
        for (int j = 0; j < ascii.length(); j++) {
            for (int k= 0; k < ascii.length(); k++) {
                System.out.print(ascii.charAt(k));
            }
        }
        System.out.println("");
    }

这是我到目前为止所知道的,我知道它不起作用。底部的System.out.println是为其生成的每个密码创建一个新行。

编辑2,我觉得这被误读为生成随机密码。我正在尝试生成一个我可以人为生成的每个密码的列表。

2 个答案:

答案 0 :(得分:0)

我觉得这对你有用。如果你需要任何修改,请告诉我。享受,这将产生你所拥有的所有排列。不确定你是否想破解密码,哈哈 我添加了排列和组合方法。请根据您的需要使用。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stackoverflow;

import java.util.Random;

/**
 *
 * @author Ashish Tyagi
 */
public class Stackoverflow {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //String ascii = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+.-/:;<=>?@[]^_{|}~";
        String ascii="abc";
        for(int i=1;i<=ascii.length();i++){
        combin(ascii.substring(0, i), new StringBuffer(), i);
        combin(ascii.substring(i), new StringBuffer(), i);
    }
  }


 public static void combin(String input,StringBuffer output,int depth){
      if (depth == 0) {
            System.out.println(output);
        } else {
            for (int i = 0; i < input.length(); i++) {
                output.append(input.charAt(i));
                combin(input, output,depth - 1);
                output.deleteCharAt(output.length() - 1);
            }
        }    
 }
}

答案 1 :(得分:0)

这是一个递归问题。因此,让我们使用递归和麻袋的力量。如果我们看一下ascii table,我们可以看到pw中的角色来自!至〜这是32-126。知道了这一点,我们可以通过在每个角色上添加一个角色,直到我们到达最后一个角色并继续前进,直到达到所需的长度。你可以根据自己的喜好限制“允许的字符”和长度。但是可能性的增长非常快,因此当你增加可能性时,你很可能会快速耗尽堆栈空间。

public static final int LENGTH=2; // the length of passwords we want to generate

public static void passWordGen(String currentPass,char c){      
    if(c<='~'){
        System.out.println(currentPass+c);
        passWordGen(currentPass,(char)(c+1)); //go through every character at this postion
        if(currentPass.length()<LENGTH-1){
            passWordGen(currentPass+c,'!'); //start over by adding the current charterer to the current postion
        }
    }       
}
public static void main(String[] args) {        
        passWordGen("",'!'); // kick off the recursion with an empty string and the first character         
}