VFP algorithm example RSA

xiaoxiao2021-03-30  203

***************** RSA algorithm example ************************

* Public key N: E: with (P-1) (Q-1) mutual number ** private key D: E of Mruto * ((MOD (P-1) (Q-1))) * Encrypted C = M E-square MOD N * Decryption M = C D-Party MOD N * Note: It is just a decimal operation, and the large number calculation will generate errors.

*********************************************************** ***** P = 47 q = 71 * n = 3337 (public key) * E = 79 (public key)

* D = 1019 (private key)

* m = 688 (raw data) * c = 1570 (number of encrypted) **************************************** ************************ P = 47 q = 71 * n = pq = 3337 ** Encryption key E and (P-1) ( Q-1) = 46 * 70 = 3220 No common cavity * randomly select E, such as 79, then * D = 79 negative primary MOD 3220 = 1019 * Set the number to encryption M = 688 * * encryption C = M The 79th MOD N * = 688 of the e-Party MOD 3337 * = 1570 * Decipained M = C D-Party MOD N * = 1570 1019 MOD 3337 * = 688 ******** ****************************************************** CLEAR *? ENCRYPT_DECRYPT1 (688, 79, 3337) AA = 3330? "Before:", AAXX = Encrypt_Decrypt2 (AA, 65537, 68714299)? "Encrypted Results:", XX? "Restore:", Encrypt_Decrypt2 (xx, 48843233,68714299)

ENCRYPT_DECRYPT1 (1570, 1019, 3337) ?? Encrypt_Decrypt2 (688, 79, 3337)? Encrypt_Decrypt2 (1570, 1019, 3337)

* Here, E = 17, which is interacting with N, which is common (3, 17, 65537 (ie, 2 16th 1)) several values ​​* The following needs to calculate D, as in the above example : * P = 47 q = 71 * n = 3337 (public key) * E = 79 (public key) * Calculation method is (D * e) MOD ((P-1) (q-1)) of the remainder is 1 DEKEY (47, 71, 79) *************************************************** **************** Both the following two functions can be calculated (m ^ e) mod n **************** ********************************************************************************************** ***************************************************************** Procedure Encrypt_Decrypt1lParameters M , E, NLOCAL A, B, CA = MB = EC = 1do while b <> 0 if b% 2 == 0 b = b / 2 && down-order A = (a * a)% N else b = b - 1 C = (a * c)% n endif enddo return c endproc ***************************************************** ********************************* Procedure Encrypt_Decrypt2lParameters M, E, NPRIVATE INTRESULT = 1 for intflag = 1 to e intResult = (intResult * m)% N Endfor return intresultendproc ********************************************************* ********** Poor drums Differential Number ********************************************* ******************************* Procedure Decryptlparameters NNUMLOCAL NSTR, NLEN, NSTART, NEND, NRETURNSTR = STR (NNUM, 1 6) Only 16-digit NLEN = LEN (AllTrim (nstr)) && VFP can be processed

* The following calculation is the maximum number of prime numbers in the number of prime numbers. NStart = int (NLEN / 2) IF Mod (NLEN, 2) <> 0 nStart = NStart 1ENDIF CSTR = Replicate ("9", nStart) NEND = VAL (Replicate, NStart-1)))

For nreturn = val (cstr) to Nend Step -1 if MOD (NNUM, NRETURN) = 0 EXIT Endif endfor

Return nreturnendproc ********************************************************* * The remainder of the private key D * (D * e) Mod ((P-1) (Q-1)) is 1 ******************************* ************************** Procedure DekeyLParameters P, Q, ELOCAL Z, XZ = (P-1) * (Q-1)

For x = 1 to z if (x * e)% z = 1 return x endif endfor

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

New Post(0)