Discussion on using RSA algorithm to prevent illegal registration machine production

Source: Internet
Author: User
Tags modulus
I. Introduction to RSA

The RSA public key cryptography system is proposed by R. Rivest, A. Shamir, L. Adleman. It can be used not only for data encryption, but also for digital signatures.AlgorithmAs follows:

  • 1. Take two similar big prime numbers p and q;
  • 2. calculate n = p * q, Z = (p-1) * (q-1 );
  • 3. Take an integer e that interacts with Z;
  • 4. Calculate the integer d that satisfies E * D = 1 mod z;
  • 5. Divide plaintext m into character block s for encryption, and each block s is less than N. The plaintext m is less than N, and encrypted to form ciphertext C. The encryption and decryption processes are as follows:
    Encryption: c = m ^ e mod n decryption: M = C ^ d mod n
  • 6. (n, E) and (n, d) are called "public keys" and "private keys" respectively ". According to the Euler's theorem, we can obtain:
    M = C ^ d mod n = (M ^ e mod n) ^ d mod n = m
  • The example shows the working process: Take two prime numbers P = 11, q = 13, n = p * q = 11*13 = 143, Z = (p-1) * (q-1) = (11-1) * (13-1) = 120, and then select the integer e with Z = 120, for example, E = 7, now we can calculate the integer d = 120 that satisfies 7 * D = 1 mod 103, that is, 7*103 = 1 mod 103/120, 7 *, and more than 1:

     
    P = 11 q = 13 N = 143 E = 7 d = 103 (n, e) = (143,7) (N, d) = (143,103)

    Take data encryption as an example:

    A sends confidential data M = 85 to B, and the Public Key (n, e) = (143,7) of B is known. Therefore, the following formula can be calculated:

    C = m ^ e mod n = 85 ^ 7 mod 143 = 123

    Party A sends C to Party B, and Party B uses the private key (n, d) = (143,103) to calculate C:

     
    M = C ^ d mod n = 123 ^ 103 mod 143 = 85

    Now, Party B has obtained the confidential data information that party a wants to send to it. Here, Party A sends a message to Party B. Party A only owns Party B's public key.

    Take the digital signature as an example:

    B wants to send a message to Party A and convince Party A that the message was sent by Party B himself. Therefore, Party B will be able to represent the encoding value of his identity (for example, 123 ), use the private key (n, d) = (143,103) for calculation, and send the result to:

     
    M = C ^ d mod n = 123 ^ 103 mod 143 = 85

    After receiving the digital signature of Party B, Party A uses the Public Key (n, e) = (143,7) of Party B to calculate the code representing the identity of Party B:

     
    C = m ^ e mod n = 85 ^ 7 mod 143 = 123

    The sender of the verified information is B. Because only Party B has the private key (n, d) to calculate the encoding 123 representing its own identity. Without knowing the private key (n, d) of Party B, No one will calculate the signature 85 to impersonate Party B. Here, Party B sends information to Party A and signs the signature. Party A only owns Party B's public key to verify Party B's signature.

    From the above two examples, we can better understand this conclusion: data encrypted by (n, e) can only be decrypted by (n, D), and vice versa.

    2. Apply RSA to serial number-based Shared Software

    Examples of digital signatures can better understand this application: in a shared software, a wants to register the software with the Registration Name of 123. What he owns now is only the sharing software.ProgramPublic Key (n, e) = (143,7 ). A hereby submits a registration application with the Registration Name of 123 to B. After B learns the application and passes the application, it calculates the Registration Name of 123 using the private key (n, D:

     
    M = C ^ d mod n = 123 ^ 103 mod 143 = 85

    The calculated result 85 (serial number) is provided to the Registration Program of the Shared Software for calculation:

     
    C = m ^ e mod n = 85 ^ 7 mod 143 = 123

    Then, the Registration Program checks whether the calculation result C is 123 (Registration Name) to determine whether the registration is successful.

    If a random input of a set of serial numbers using the public key (n, e) for calculation, the result will not be 123, and registration will fail. Note: here, the Registration Program for shared software compares the registration name rather than the serial number. If a traces the registration name generated by the serial number he randomly entered and provides it to the Registration Program, the registration program can also pass the registration, but he does not (n, d), so he cannot use his own registration name for software registration, thus preventing the production of illegal registration machines.

    The purpose of applying RSA to this scenario is to prevent the creation of illegal registration machines. In the above description, n = 143, including N (HEX) = 963251dc5a9c90d9f203a03c363ba411 in the following demo, the corresponding p and q can be quickly decomposed into corresponding p and q, and then combined with E exposed in the Shared Software registration program to calculate d, then the protection of this shared software is completely cracked. The solution is to avoid n being too short, and combine MD5 and other encryption algorithms ......

    Iii. Specific Practices

    /* RSA demo 1.0

    * Copyright (c) 2004 Zhao Chunsheng

    * 2004.04.25

    * Http://timw.yeah.net

    * Http://timw.126.com

    * This program calls the MIRACL ver 4.82 maxcompute database. For details, see the attached manual.

    * P, q, N, D, and E are generated using rsatool2.

    */

    1. Key to the registration serverCode:

    Void ckeygendlg: ongen () {// todo: add your control notification handler code here/* keygen 1.0 * copyright (c) 2004 Zhao Chunsheng * 2004.04.25 * http://timw.yeah.net * http://timw.126.com * This program calls MIRACL ver 4.82 large number of computing database, see its attached manual. */////////////////////////////////////// /// // P (HEX) = e34436f5f48a227b // Q (HEX) = a92fa24467c4e3e3 // n (HEX) = bytes // D (HEX) = bytes // E (HEX) = 10001 // keysize (BITs) = 128 updatedata (true); int namelen = m_name.getwindowtextlength (); // get the name length // be sure to meet the following requirements: Name Length <= (keysize/8), so that m
      
       
     Iobase = 16; // hexadecimal mode // define and initialize the variable big M = mirvar (0); // m in plaintext: Registration Code snbig c = mirvar (0 ); // C ciphertext: username namebig n = mirvar (0); // n modulus Big D = mirvar (0 ); // D Private Key tchar name [256] = {0}; tchar Sn [256] = {0}; m_name.getwindowtext (name, namelen + 1 ); // obtain namebytes_to_big (namelen, name, c); // convert it to hexadecimal cinstr (n, "inline"); // initialize the modulus n cinstr (D, "56157d29a89d77bf2f669a8f0b123cc9"); // initialize the private key dpowmod (c, d, n, m); // calculate M = C ^ d mod ncotstr (M, SN ); // write m into the snm_sn.setwindowtext (SN) in hexadecimal notation; // output the hexadecimal Sn // release the memory mirkill (m); mirkill (C ); mirkill (n); mirkill (d); mirexit ();} elsem_sn.setwindowtext ("error: Invalid registration name. "); updatedata (false );}

    2. Key code for registering a program in the software:

    Void crsaapplicationdlg: onreg () {// todo: add your control notification handler code here/* RSA application 1.0 * copyright (c) 2004 Zhao Chunsheng * 2004.04.25 * http://timw.yeah.net * http://timw.126.com * This program calls MIRACL ver 4.82 large number of computing database, see its attached manual. */////////////////////////////////////// /// // P (HEX) = e34436f5f48a227b // Q (HEX) = a92fa24467c4e3e3 // n (HEX) = bytes // D (HEX) = bytes // E (HEX) = 10001 // keysize (BITs) = 128 updatedata (true); MIRACL * MIP = mirsys (, 0); MIP-> iobase = 16; // hexadecimal mode // define and initialize the variable big M = mirvar (0); // M plaintext: Registration Code snbig c = mirvar (0 ); // C ciphertext: username namebig n = mirvar (0); // n Modulus big E = mirvar (0); // e public key tchar name [256] = {0}; tchar Sn [256] = {0 }; tchar temp [256] = {0}; int Len = 0; int I, j; // obtain namelen = m_name.getwindowtextlength (); m_name.getwindowtext (name, Len + 1 ); // obtain snlen = m_sn.getwindowtextlength (); m_sn.getwindowtext (Sn, Len + 1); // check whether the Sn is hexadecimal for (I = 0, j = 0; I <Len; I ++) {If (isxdigit (Sn [I]) = 0) {j = 1; break ;}} // If the input Sn is hexadecimal and the length is not 0if (J! = 1 & Len! = 0) {cinstr (M, SN); // initialize the plaintext mcinstr (n, "963251dc5a9c90d9f203a03c363ba411"); // initialize the modulus n cinstr (E, "10001 "); // initialize the public key E // when m <n, if (compare (m, n) =-1) {powmod (M, E, N, C ); // calculate c = m ^ e mod nbig_to_bytes (256, C, temp, false); // convert C into an array and write it into temp // release the memory mirkill (m ); mirkill (c); mirkill (n); mirkill (E); mirexit ();} elsej = 1;} // For name, temp, m, n, check the length of the SN if (lstrcmp (name, temp )! = 0 | j = 1 | Len = 0) MessageBox ("please check your name and Sn, then try again. "," RSA application "); elsemessagebox (" congratulate !!! "," Registration complete! "); Updatedata (false );}

    //////////////////////////////////////// //////////////////////////////////////// //

    Compilation prompt:

    1. Set Project-settings for (all configuration)-C/C ++

    Set precompiled headers to: automatic use of precompiled headers (1 ).

    Figure 1

    2. Add ms32.lib to the Project (2 ).

    Figure 2

    3. MIRACL is the C library.

     
    Extern "C" {# include "MIRACL. H" # include "mirdef. H"} # pragma comment (Lib, "ms32.lib ")

    4. The above Code passed the test in win2000p + SP4 + vc6 + SP5

    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.