Validity verification and mutual conversion algorithm for new and old ID cards

Source: Internet
Author: User
1. Common knowledge about Chinese resident ID cards:

  China's current use of citizen ID card numbers are subject to two national standards: [GB 11643-1989] and [GB 11643-1999 〗.[GB 11643-1989] indicates a 15-digit ID card number. The numbers are arranged from left to right in the order of six-digit address codes, six-digit birth date codes, and three-digit sequential codes, The birthdate Code does not contain the number of centuries. [GB 11643-1999] specifies the 18-digit ID card number. A citizenship number is a combination of features and consists of a 17-digit ontology code and a digital verification code. Sort order  The six-digit address code, eight-digit birth date code, three-digit sequence code, and one-digit verification code are displayed in sequence from left to right. Address Code: indicates the Administrative Code of the county (city, flag, district) where the resident account of the encoding object is located.
Date of birth: indicates the year, month, and day of the birth of the encoding object. The year is represented by four digits, and there is no separator between years, months, and days.
Sequence Code: indicates the sequence number of the person born on the same year, the same month, and the same day within the region specified by the same address code. The odd number of the sequence code is assigned to men, and the even number is assigned to women.
  Verification Code: it is the verification code calculated according to the first 17-digit code and ISO 7064: 1983.mod 11-2 verification code.
  The algorithm for verifying the last digit of the ID card number is as follows:
      SIGMA (A [I] * W [I]) mod 11 (I = 2, 3,..., 18) "*": represents the multiplication number.
    I: Indicates the serial number of each digit of the ID card number, from right to left, 18 at the left, and 1 at the right.
  A [I]: indicates the number on the I-digit of the ID card number.
  W [I]: indicates the weight on the I-th w [I] = 2 ^ (I-1) mod 11
  Set: r = Σ (A [I] * W [I]) mod 11 (I = 2, 3,..., 18)
  C = ID card number verification code
The relationship between R and C is as follows:
R: 0 1 2 3 4 5 6 7 8 9 10
C: 1 0x9 8 7 6 5 4 3 2
From this we can see that X is 10, and 10 in the Roman numerals is X, so the new standard ID card number may contain non-digit letters X.        2. algorithm:    Using system;

/// <Summary>
/// Program: hahaman
/// Program: QQ: 270901361
/// </Summary>
Public Class Identity
{
//  Bitwise value Array
   Private Static byte [] Wi = new byte [17];
//Number of characters before ID card
   Private Static readonly byte fpart = 6;
//ID Card Algorithm modulo key value
   Private Static readonly byte fmod = 11;
//Old ID card Length
   Private Static readonly byte oldidlen = 15;
// Length of new ID card
   Private Static readonly byte newidlen = 18;
// New ID card year sign
   Private Static readonly string yearflag = "19 ";
//  Verification code string
    Private Static readonly string checkcode = "10x98765432 ";
// Minimum administrative area code
   Private Static readonly int mincode = 150000;
//Maximum administrative area code
   Private Static readonly int maxcode = 700000;
    
    // Initialize the bit weight
   Private Static void setwibuffer (){
       For (INT I = 0; I <wi. length; I ++ ){  
            Int K = (INT) math. Pow (2, (WI. Length-I ));
           Wi [I] = (byte) (K % fmod );
       }
   }

   
    // Obtain the last digit of the new ID card: the checkpoint.
   Private Static string getcheckflag (string idcard ){
       Int64 sum = 0;
       // Perform weighted summation
        For (INT I = 0; I <17; I ++ ){      
            Sum + = int. parse (idcard [I]. tostring () * WI [I];
       }
       // Modulo operation to obtain the modulo Value
       Byte icode = (byte) (sum % fmod );
        Return checkcode [icode]. tostring ();
   }
   
    // Determine the validity of the string length
   Private Static bool checklength (string idcard, ref bool newidflag ){
       Bool right = (idcard. Length = oldidlen) | (idcard. Length = newidlen );
       Newidflag = false;
       If (right ){
           Newidflag = (idcard. Length = newidlen );
       }
       Return right;
   }
   
    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.