java產生隨機密碼(隨機長度,指定長度)方法總結

來源:互聯網
上載者:User

Java產生隨機長度密碼。

 代碼如下 複製代碼

/**
* 產生隨機密碼
*
* @param pwd_len
* 產生的密碼的總長度
* @return 密碼的字串
*/
public static String genRandomNum(int pwd_len) {
// 35是因為數組是從0開始的,26個字母+10個數字
final int maxNum = 36;
int i; // 產生的隨機數
int count = 0; // 產生的密碼的長度
char[] str = { ‘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’, ’0′, ’1′, ’2′, ’3′, ’4′, ’5′, ’6′, ’7′, ’8′, ’9′ };

StringBuffer pwd = new StringBuffer(“”);
Random r = new Random();
while (count < pwd_len) {
// 產生隨機數,取絕對值,防止產生負數,

i = Math.abs(r.nextInt(maxNum)); // 產生的數最大為36-1

if (i >= 0 && i < str.length) {
pwd.append(str[i]);
count++;
}
}

return pwd.toString();
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(genRandomNum(14));//產生14位隨機密碼
}


產生指定長度的隨機密碼

 代碼如下 複製代碼


import java.util.Random;

public class test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println(genRandomNum(10));
 }
 /**
  * 產生隨即密碼
  * @param pwd_len 產生的密碼的總長度
  * @return  密碼的字串
  */
 public static String genRandomNum(int pwd_len){
  //35是因為數組是從0開始的,26個字母+10個數字
  final int  maxNum = 36;
  int i;  //產生的隨機數
  int count = 0; //產生的密碼的長度
  char[] str = { '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
 
  StringBuffer pwd = new StringBuffer("");
  Random r = new Random();
  while(count < pwd_len){
   //產生隨機數,取絕對值,防止產生負數,
  
   i = Math.abs(r.nextInt(maxNum));  //產生的數最大為36-1
  
   if (i >= 0 && i < str.length) {
    pwd.append(str[i]);
    count ++;
   }
  }
 
  return pwd.toString();
 }
}

java 產生15位的隨機碼

 代碼如下 複製代碼

import java.util.ArrayList;
import java.util.List;

public class GeneratePswd {

 @SuppressWarnings({ "unchecked", "unchecked" })
 public static void main(String[] args) {

  StringBuffer buf = new StringBuffer("a,b,c,d,e,f,g,h,i,g,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z");
  buf.append(",A,B,C,D,E,F,G,H,I,G,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
  buf.append(",~,@,#,$,%,^,&,*,(,),_,+,|,`,.");
  buf.append(",1,2,3,4,5,6,7,8,9,0");

  String[] arr = buf.toString().split(",");
  List pswdList = new ArrayList();
  
  for(int i=0;i<50;i++){
   pswdList.add(getPswd(arr));
  }
  
  for(int i=0;i<pswdList.size();i++){
   System.out.println(pswdList.get(i));
  }
 }
 
 public static String getPswd(String[] arr){
  StringBuffer b = new StringBuffer();
  java.util.Random r;
  int k ;
  for(int i=0;i<15;i++){
    r = new java.util.Random();
    k = r.nextInt();
    b.append(String.valueOf(arr[Math.abs(k % 76)]));
  }
  
  return b.toString();
 }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.