* @note 华融湘江银行提供的转换方法
*/
private static String getVerify(String s17) throws Exception {
String sVerify = "";
String code = "";
int num = 0;
int tmp = 0;
for (int i = 18; i >= 2; i--) {
num += (Math.pow(2, i - 1) % 11)
* Integer.parseInt(s17.substring(18 - i, 18 - i + 1));
}
num %= 11;
switch (num) {
case 0:
code = "1";
break;
case 1:
code = "0";
break;
case 2:
code = "X";
break;
default:
tmp = 12 - num;
code = Integer.toString(tmp);
break;
}
sVerify = code;
return sVerify;
}
/**
* 验证身份证号码
* @param idNo 身份证号码
* @return 如果验证正常返回IdNo 如果是15位的身份证则转换为18位身份证,否则抛出异常
*/
public static String verifyIDCard(String idNo){
if(Util.isNullOrEmpty(idNo)){
throw new ValidationRuntimeException(CHECKMSG.VALIDATE_IDNO_EMPTYSIZE);
}
/* if(idNo.length()!=18){
throw new ValidationRuntimeException(CHECKMSG.VALIDATE_IDNO_ERRORSIZE);
}*/
if(idNo.length()==15){
idNo = convert15to18(idNo);
}
try {
String lastStr = getVerify(idNo.substring(0,idNo.length()-1));
if(!idNo.substring(idNo.length()-1).equals(lastStr)){
throw new ValidationRuntimeException(CHECKMSG.VALIDATE_IDNO_ILEGAL);
}
} catch (Exception e) {
throw new ValidationRuntimeException(CHECKMSG.VALIDATE_IDNO_ILEGAL);
}
return idNo;
}