身分証明書の検証
もっと読む
身分証明書の検証
身分証明書の検証
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
//
// :http://blog.csdn.net/tanksyg/article/details/49927739
/**
*
* --15 : 7、8 ( ), 9、10 , 11、12 , 15 , , 。
* --18 : 7、8、9、10 ( ), 11、 12 , 13、14 , 17 , , 。
* --
*/
public class IdcardValidator {
/**
* 、 :
* 11 : 12 : 13 : 14 : 15 :
* 21 : 22 : 23 : 31 : 32 :
* 33 : 34 : 35 : 36 : 37 :
* 41 : 42 : 43 : 44 : 45 : 46 :
* 50 : 51 : 52 : 53 : 54 :
* 61 : 62 : 63 : 64 : 65 :
* 71 :
* 81 : 82 :
* 91 :
*/
private static String cityCode[] = { "11", "12", "13", "14", "15", "21",
"22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42",
"43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62",
"63", "64", "65", "71", "81", "82", "91" };
/**
*
*/
private static int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
/**
*
* @param idcard
* @return true, false
*/
public static boolean isValidatedAllIdcard(String idcard) {
if (idcard == null || "".equals(idcard)) {
return false;
}
if (idcard.length() == 15) {
return validate15IDCard(idcard);
}
return validate18Idcard(idcard);
}
/**
*
* 18
*
* 〖 GB11643-1999〗 , , 。
* : , , 。
*
* : , 、 、 , , 。
*
* 1. 1、2 : ;
* 2. 3、4 : ;
* 3. 5、6 : ;
* 4. 7~14 : 、 、 ;
* 5. 15、16 : ;
* 6. 17 : , ;
* 7. 18 : , , 。 0~9 , x 。
*
* ( ) : 1. 17 。 :7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
*
* 2. 17 。
*
* 3. 11,
*
* 4. 0 1 2 3 4 5 6 7 8 9 10 11 。 1 0 X 9 8 7 6 5 4 3
* 2。
*
* 5. 2, 18 Ⅹ。 10, 2。
*
*
* @param idcard
* @return
*/
public static boolean validate18Idcard(String idcard) {
if (idcard == null) {
return false;
}
// 18
if (idcard.length() != 18) {
return false;
}
// 17
String idcard17 = idcard.substring(0, 17);
// 17
if (!isDigital(idcard17)) {
return false;
}
String provinceid = idcard.substring(0, 2);
//
if (!checkProvinceid(provinceid)) {
return false;
}
//
String birthday = idcard.substring(6, 14);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try {
Date birthDate = sdf.parse(birthday);
String tmpDate = sdf.format(birthDate);
if (!tmpDate.equals(birthday)) {//
return false;
}
} catch (ParseException e1) {
return false;
}
// 18
String idcard18Code = idcard.substring(17, 18);
char c[] = idcard17.toCharArray();
int bit[] = converCharToInt(c);
int sum17 = 0;
sum17 = getPowerSum(bit);
// 11
String checkCode = getCheckCodeBySum(sum17);
if (null == checkCode) {
return false;
}
// 18 ,
if (!idcard18Code.equalsIgnoreCase(checkCode)) {
return false;
}
return true;
}
/**
* 15
*
*
*
*
*
*@param idカード
*@return
*/
public static bootlean validate 15 IDCard(String idcard){
if(idcard==null){
return false;
)
// 15 は です
if(idcard.length()==15){
return false;
)
//15は です.
if(!isDigital(idcard){
return false;
)
String province id=idcard.substring(0,2)
// を
if(!checkProvince id){
return false;
)
String birthday=idcard.substring(6,12)
SimpleDateFormat sdf=new SimpleDateFormat("yMMdd")
try{
Date birthDate=sdf.parse(birthday);
String tmpDate=sdf.format(bithDate)
if(!tmpDate.equals){/ の が っています.
return false;
)
}catch(PaseException e 1){
return false;
)
return true;
)
/**
*15 の を18 の に します.
*
*@param idカード
*@return
*/
public static String convertIdcarBy 15 bit(String id card){
if(idcard==null){
return null
)
// 15 の
if(idcard.length()==15){
return null
)
//15は です.
if(!isDigital(idcard){
return null
)
String province id=idcard.substring(0,2)
// を
if(!checkProvince id){
return null
)
String birthday=idcard.substring(6,12)
SimpleDateFormat sdf=new SimpleDateFormat("yMMdd")
Date bithdate=null;
try{
bithdate=sdf.parse(birthday);
String tmpDate=sdf.format(bithdate)
if(!tmpDate.equals){/ の が っています.
return null
)
}catch(PaseException e 1){
return null
)
Calendar cday=Calendar.get Instance()
cday.setTime;
String year=String.valueOf(cday.get);
String idcard 17=idcard.substring(0,6)+year+idcard.substring(8)
char c[]=idcard 17.1 toChararararray()
String checkCode=";
// を に する
int bit[]=converCharToInt(c);
int sum 17=0
sum 17=get PowerSum(bit)
// と と11 の を するコード
checkCodeBySum(sum 17)
// ビットを できませんでした.
if(null==checkCode){
return null
)
// 17 と18 の コードをつなぎ わせる
idcard 17+=checkCode;
return id card 17
)
/**
* を
*
*@param province id
*@return にTRUEに ります.そうでないとFALSEに ります.
*/
prvate static book check Province id(String province id){
for(String id:cityCode){
if(id.equals){
return true;
)
)
return false;
)
/**
*デジタル
*
*@param str
*@return
*/
prvate static book isDigital(String str){
return str.matches('^[0-9]*);
)
/**
* の と の み け を け わせた 、 を る
*
*@param bit
*@return
*/
prvate static int getPowerSum(int[]bit){
int sum=0;
if(パワー.length!=bit.length){
return sum;
)
for(int i=0;i<bit.length;i+){
for(int j=0;j<パワー・レングス;j++){
if(i==j){
sum=sum+bit[i]*パワー[j]
)
)
)
return sum;
)
/**
* の を11と り、 りの を で する
*
*@param checkCode
*@param sum 17
*@return ビット
*/
privte static String get CheckCodeBySum(int sum 17){
String checkCode=null;
switch(sum 17%11){
case 10:
checkCode="2"
break;
case 9:
checkCode="3"
break;
case 8:
checkCode="4"
break;
case 7:
checkCode="5"
break;
case 6:
checkCode="6"
break;
case 5:
checkCode="7"
break;
case 4:
checkCode="8"
break;
case 3:
checkCode="9"
break;
case 2:
checkCode="x"
break;
case 1:
checkCode="0"
break;
case 0:
checkCode="1"
break;
)
return check Code;
)
/**
* を に する
*
*@param c
*@return
*@throws Number FormatException
*/
prvate static int[]converChartoInt(char[]c)throws Number FormatException{
int[]a=new int[c.length]
int k=0
for(char temp:c){
a[k+]=Integer.parseInt(String.valueOf(temp)
)
return a;
)
public static void main(String[]args)throws Exception{
String idcard 15=「13032186 0311123」
String idcard 18=「13022919830501231」
//15 の
System.out.printel(isValidated AllIdcard);
//18
System.out.printel(isValidated AllIdcard);
//15 の を18 の に えます.
System.out.println(convertIdcarBy 15 bit);
)
)
を する
の の で、
のものを いて、 びがあって、 もあって、あなたが の が きなことを んで、 に し することができます.もちろん、お を ってお を っています.(アリペイとWeChatとボタングループを うのを します.) の を つお がありません.ありがとうございます.
のホームページ:http://knight-black-bob.iteye.com/
ご ありがとうございます.もっとよくやります.