The 18-bit ID format verification algorithm implemented by Java

xiaoxiao2021-03-06  43

The citizen identity number is a feature group code, consisting of a seventeen digital body code and a digital check code. Arrangement order from left to right as: six digit address code, eight digits birth date, three digital order Code and a digital check code. 1. The address code represents the administrative division code of the county (city, flag, district) of the coded object, and is implemented in accordance with the provisions of GB / T 2260.

2. The birth date indicates the year, month, day, and executed in accordance with GB / T 7408. No separator between the year, month, and the date code. Example: The date of birth is October 26, 1966, and its birth date is 19661026.

3, the sequence code indicates that within the area identified by the same address code, the sequence number of the person born in the same year, the same month, the same day, the odd number of sequence code is assigned to male, even thousands allocated to women.

4. The check code check code is used by ISO 7064: 1983, MOD 11-2 check code system.

(1) Seventeen digital body code weighted summation and formula S = SUM (AI * Wi), I = 0, ..., 16, first on the top 17 digits and AI: Represents the first position ID card number Number Wi: Represents Weighted Factors at the 1st Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

(2) Calculation model Y = mod (s, 11)

(3) Get the corresponding check code y: 0 1 2 3 4 5 6 7 8 9 10 Check code: 1 0 x 9 8 7 6 5 4 3 2

Below is the code implemented by Java

/ * IDCard.java created on 2004-11-5 17:03:37 * * / package org.yz21.study.idcard;

/ ** * @Author violin 2004-11-5 17:03:37 * Copyright www.yz21.org 2003-2004 * / public class IDCARD {// wi = 2 (n-1) (MOD 11) Final INT [ ] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};

// Verify Digit Final Int [] Vi = {1,0, 'x', 9, 8, 7, 6, 5, 4, 3, 2};

Public IDCard () {}

// verify public boolean verify (idcard.length () == 15) {IDCARD = uptoeighteen (idcard);}}}}}}} {Return False;} string verify = IDCard.substring (17, 18); if (Verify.Equals)) {return true;} return false;

// get verify public string getverify (string ele htcardid) {int Remaining = 0;

IF (ele htcardid.length () == 18) {elehtcardid = ele htcardid.substring (0, 17);

IF (Eightcardid.Length () == 17) {Int Sum = 0;

For (int i = 0; i <17; i ) {string k = ele htcardid.substring (i, i 1); intloy = integer.parseint (k); sum = sum wi [i] * ai;} Remaining = sum% 11;} return remaining == 2? "x": string.valueof (vi [remaining]);}

// 15 update to 18 public String uptoeighteen (String fifteencardid) {String eightcardid = fifteencardid.substring (0,6); eightcardid = eightcardid "19"; eightcardid = eightcardid fifteencardid.substring (6,15); eightcardid = eightcardid GETVERIFY (EIGHTCardID); Return EightcardId;

}

Test code: The unit test tool used is JUnit

/ * Idcardtest.java created on 2004-11-5 17:32:12 * * / pack org.yz21.study.idcard;

Import junit.framework.test; import junit.framework.testcase; import junit.framework.testsuite;

/ ** * @Author violin 2004-11-5 17:32:12 * Copyright www.yz21.org 2003-2004 * / public class idcardtest extends testcase {

Private string idcard1 = "11010519491231002X"; private string IDCARD2 = "440524188001010014";

Public void testverified () {idcard idcard = new idcard (); this.asserttrue (idcard.verify (idcard1)); this.asserttrue (idcard.verify (idcard2));}

Public static test suite () {return new testsuite (IDCARDTEST.CLASS);}

Public static void main (string [] args) {junit.textui.teStrunner.run (suite ());}}}} online authentication: http://www.yz21.org/common/idcard.jsp

转载请注明原文地址:https://www.9cbs.com/read-59486.html

New Post(0)