您的位置:首页 > 编程语言 > Java开发

java生成随机编码的方法。

2016-06-03 17:36 211 查看
java生成随机编码的方法。

代码如下:

package com.hzcominfo.voucher.utils;

import java.util.Calendar;

/**

* @author : suyuyuan

* @date :2016年6月3日 下午4:50:42

* @version 1.0

*/

public class CodeUtil {

public static String passWord(String id) {

String result = "";

int[] b = new int[id.length()];

for (int i = 0; i < id.length(); i++) {

char q = id.charAt(i);

b[i] = (int) (q - '0');// 字符数字-字符0就是实际的数字值,赋值给数字数组

}

int[] w = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

int[] c = { 1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2 };

int sum = 0;

for (int j = 0; j < id.length(); j++) {

sum = sum + b[j] * w[j];

}

int r = sum % 11;

int res = c[r];

// if (res == 120) {

// result = "x";

// } else {

// result = String.valueOf(res);

// }

if (res == 120) {

result = "9";

} else {

result = String.valueOf(res);

}

return result;

}

public static String createRandom(int ws) {

String cs = "0";

for (int i = 0; i < ws-1; i++ ) {

cs = cs + "0";

}

cs = "1" + cs;

int stochastic = new Double(Math.random() * Integer.valueOf(cs)).intValue();

String temp = "%0" + ws + "d";

String stocha = String.format(temp, stochastic);

return stocha;

}

public static String createCode(){

StringBuffer sBuffer = new StringBuffer();

//时分秒

Calendar cal = Calendar.getInstance();

int hour = cal.get(Calendar.HOUR_OF_DAY);

int minute = cal.get(Calendar.MINUTE);

int second = cal.get(Calendar.SECOND);

int mm = cal.get(Calendar.MILLISECOND);

sBuffer.append(hour).append(minute).append(second).append(mm);

// 随机数7位

String random = createRandom(7);

// System.out.println(random);

sBuffer.append(random);

// 校验码

String check = CodeUtil.passWord(random);

// System.out.println(check);

sBuffer.append(check);

String str = sBuffer.toString();

return str;

}

public static void main(String[] args) {

for(int i=0;i<100;i++){

String str=CodeUtil.createCode();

System.out.println(str);

System.out.println(str.length());

}

}

}

===================

借鉴的代码如下:

package com.hzcominfo.grsinfo.selfprint.utils;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ibm.icu.util.Calendar;

import cn.edu.zju.grs.alufer.albert.print.facade.dto.StudentInfo;

public class Util {

public static String passWord(String id) {

String result = "";

int[] b = new int[id.length()];

for (int i = 0; i < id.length(); i++) {

char q = id.charAt(i);

b[i] = (int) (q - '0');// 字符数字-字符0就是实际的数字值,赋值给数字数组

}

int[] w = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

int[] c = { 1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2 };

int sum = 0;

for (int j = 0; j < id.length(); j++) {

sum = sum + b[j] * w[j];

}

int r = sum % 11;

//System.out.println(r);

int res = c[r];

//System.out.println(res);

if (res == 120) {

result = "x";

} else {

result = String.valueOf(res);

}

return result;

}

/**

*

*

* @param xsxx

* @return

*/

public static String createSecurityCode(StudentInfo xsxx, String num) {

StringBuffer sBuffer = new StringBuffer();

//版本号

sBuffer.append("1");

//硕博标记,1博士,2硕士

if (xsxx.getPylxlxDm() != null) {

sBuffer.append(Integer.valueOf(xsxx.getPylxlxDm()));

} else {

sBuffer.append("0");

}

// 年份尾数

Calendar cal = Calendar.getInstance();

int year = cal.get(Calendar.YEAR);

sBuffer.append(String.valueOf(year % 100));

// 院系代码

String yx = xsxx.getYxDm();

if (StringUtils.isNotEmpty(yx)) {

if (yx.length() > 2) {

yx = yx.substring(yx.length() - 2, yx.length());

}

} else {

yx="00";

}

// 流水号

//String number = PrimaryGenerater.getInstance().generaterNextNumber(num);

String number = "";

if (num.length()==8) {

number = num;

} else {

int numLength = num.length();

number = createRandom(8-numLength) + num;

}

// 随机数6位

//int stochastic = new Double(Math.random() * 100000).intValue();

//String stocha = String.format("%06d", stochastic);

String stocha = createRandom(6);

sBuffer.append(yx);

sBuffer.append(number);

sBuffer.append(stocha);

String temp = sBuffer.toString();

temp = temp.substring(temp.length()-17, temp.length());

// 校验码

String captcha = Util.passWord(temp);

// 防伪码

sBuffer.append(captcha);

return sBuffer.toString();

}

public static String createRandom(int ws) {

String cs = "0";

for (int i = 0; i < ws-1; i++ ) {

cs = cs + "0";

}

cs = "1" + cs;

int stochastic = new Double(Math.random() * Integer.valueOf(cs)).intValue();

String temp = "%0" + ws + "d";

String stocha = String.format(temp, stochastic);

return stocha;

}

public static String randomSplit(String barName){

List<String> list = new ArrayList<String>();

if (barName.length()%4==0) {

for (int i = 0; i < barName.length()/4; i++) {

list.add(barName.substring(4*i,4+4*i));

}

}else {

for (int i = 0; i < barName.length()/4; i++) {

list.add(barName.substring(4*i,4+4*i));

}

list.add(barName.substring(barName.length()-barName.length()%4,barName.length()));

}

StringBuffer sBuffer = new StringBuffer();

for (int i = 0; i < list.size(); i++) {

sBuffer.append(list.get(i)+" ");

}

String fg = sBuffer.toString();

return fg;

}

public static void main(String[] args) {

for (int i=1; i <= 18; i++) {

System.out.println("随机数10:"+createRandom(8));

//System.out.println("随机数16:"+Integer.toHexString(createRandom(8)));

//System.out.println("随机数还原:"+Integer.parseInt(Integer.toHexString(createRandom1(8))));

}

//Util.passWord("12150100000069027");//12150100000069027653

// Util.passWord("50100000069027653");

// // 年份尾数

// Calendar cal = Calendar.getInstance();

// int year = cal.get(Calendar.YEAR);

// int y = year % 100;

// int yy = y % 10;

// String nfy = String.valueOf(yy);

// String nf = String.valueOf(y);

// System.out.println("年份尾数:" + y);

// // 院系代码

// String yx = "123456";

// if (yx.length() > 2) {

// yx = yx.substring(yx.length() - 2, yx.length());

// }

// System.out.println("院系代码:" + yx);

// // 流水号

// String number = PrimaryGenerater.getInstance().generaterNextNumber(

// "00000000");

// System.out.println("流水号:" + number);

// // 随机数

// int stochastic = new Double(Math.random() * 1000000).intValue();

// while (stochastic < 100000) {

// stochastic = new Double(Math.random() * 1000000).intValue();

// }

//

// System.out.println("随机数:" + stochastic);

//

// System.out.println(nfy + yx + number + stochastic);

// // 校验码

// int captcha = Util.passWord(nfy + yx + number + stochastic);

//

// System.out.println("校验码:" + captcha);

//

// String jym = String.valueOf(captcha);

//

// System.out.println("校验码:" + jym);

// // 防伪码

// String textName = "1" + "1" + nf + yx + number + stochastic + jym;

//

// System.out.println("防伪码:" + textName);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: