Convert the RSA encryption code of netizens to C #

zhaozj2021-02-16  87

I didn't do anything, I think it is shameless, but some friends may be more lazy, there will be some use. I apologize to Duduwolf first.

The original text of the doodle is as follows: http: //dev.9cbs.net/develop/Article/30/30182.shtm

My shameless results are as follows (some local adjustments):

#Region Using Directives

Using system.collections.gener; using system.diagnost;

#ndregion

Namespace Rsatest {

/ * ??? RSA algorithm ????? In 1978, this algorithm has the first algorithm that can be used for data encryption and digital signatures. ??? It is easy to understand and operate, and it is also very popular. The name of the algorithm is named by inventors: ron rivest, ??? Adishamir and Leonard Adleman. But RSA's security has not been able to obtain theoretical proof.

????? The security of RSA depends on the fact that the large number is difficult to decompose. The public key and private key are two functions of two large numbers (more than 100 ??? decoction). It is speculated that the difficulty of clearing the plain text from a key and ciphertext is equivalent to decomposing the number of two large-scale prime numbers.

????? The result of the key pair. Select two large numbers, p and q. Calculate: n = p * q then randomly select the encryption key E, ??? Require E and (P - 1) * (Q - 1) mutual. Finally, the decryption key D is calculated using the Euclid algorithm, satisfying ??? e * d = 1 (MOD (P - 1) * (Q - 1)) where n and d are also mutual. The number E and N are the public key, and D is a private key. ??? Two prime p and q no longer need, should be discarded, don't let anyone know. When encrypting information m (binary representation), the first is divided into equal length data blocks M1, M2, ..., Mi, block length S, wherein 2 ^ s <= n, s is as large as possible. Ciphertext of Corresponding ??? is: Ci = Mi ^ e (MOD N) ................. (a) Decryption as follows: Mi = Ci ^ D MOD N) ................. (b)

????? RSA can be used for digital signatures, the scheme is verified by (A) signature, (b). Considering the safety of safety ??? and M information is large when considering the factors, which is generally ash as a HASH operation. RSA security. The security of RSA depends on the decomposition of large numbers, but whether it is equivalent to the theoretical proof, because there is no proven to crack the RSA, it is necessary to make a large number of decomposition. Assume that there is an algorithm that does not have to decompose, it must be modified to become a large number of decomposition algorithms. ??? Currently, some variable algorithms of RSA have been proven to be equivalent to large decomposition. Anyway, decomposition N is the most obvious attack method. ??? Now, people have decomposed more than 140 decimal places. Therefore, the modulus n must be selected, depending on the specific applicability. ????? Since the most calculations are made, the fastest cases of RSA are more slow than the DES, whether it is software or hardware implementation. ??? Speed ​​has always been the deficiencies of RSA. Generally, only a small amount of data encryption. * / ??? public struct RSA_PARAM ??? {??????? public uint64 p, q; ?? // two prime numbers, do not participate in encryption decryption operation ??????? public uint64 f;? ???? // f = (p-1) * (Q-1), do not participate in encryption decryption operation ??????? public uint64 n, e; ?? // 公, n = p * q , GCD (E, F) = 1 ??????? public uint64 d; ????? // Private, E * D = 1 (MOD F), GCD (N, D) = 1 ?? ????? public uint64 s; ????? // block long, satisfy the maximum S of 2 ^ s <= n, that is, log2 (n) ???}; ??? class program ??? { ??????? // small pill table ??????? #REGION PRIME TABLE ?????? oo ??????? {????? ?????? 3, ??????????? 5, ????????????????????????? 11,??? ????????? 13, ???????????? 17, ???????????? 19, ??????????? 23 ,? ????????????????????????????????????????????????????? 41 ???????????? 43, ????????????????????????? 53, ?????????? ? 59, ?????????????????????????????????????????????? ??? 73, ????????????????????????? 83, ?????????? 89, ?????? ????? 97 ???????}; ???????? #Ndreamion

??????? = g_primetable.length; ??????? consth; ??????? consth; ???????????????????.?????????

??????? // random number ?????? public class randnumber ??????? {??????????? * * / ????? ?????? private uint64 randseed; / * * / ??????????? public randNumber (): this (0) {}

??????????? public randnumber (uint64 s) ??????????????????? f (0 == s) ) // (! s) ??????????????? {??????????????????? randSeed = (UInt64) new Random ( ) .Next (); // Time (null); ?????????????????????? ELSE ????? ?????????? {??????????????????? randseed = S; ???????????????} ???????????} ??????????????????????????? PUBLIC UINT64 RANDOM (uint64 n) ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????? Return randseed% N; ???????????} ????????} ??????? static randNumber g_rnd = new randNumber ();

??????? / * model operation, return value x = a * b mod n * / ??????? uint64 mulmod (uint64 a, uint64 b, uint64 n) ??????? {??????????? return a * b% N; ???????}

??????? / * Model power calculation, return value x = base ^ POW mode N * / ??????? uint64 POWMOD (uint64 BAS, uint64 Pow, uint64 n) ??????? {??????????? uint64 a = BAS, B = Pow, c = 1; ???????????? while (b! = 0)? // (b)? ?????????? {?????????????????????????????????????????????????? (b & 1) ???? ??????????? {??????????????????? b >> = 1; ??????????? A = a * a% N; ??? // The function appears to handle the integer of 64 bits, but since A * a has caused overflow when A> = 2 ^ 32, there is no 64 bits of actual processing range? ??????????????????????????? a = MULMOD (A, a, n); ???????????????} B-;? ?????? // c = a * c% N; ??????? // here will also overflow, if the 64-bit integer is removed to two 32-bit integers, I don't know if this problem can be solved. ??????????????? c = mulmod (a, c, n); ???????????} return c; ???????}

??????? / * ??????? Rabin-miller number test, return to 1 by test, otherwise returns 0. ??????? n is the number of to be measured. ??????? Note: The test is not necessarily the number of prime numbers. The probability of non-quantity is 1/4 ???????? * / ??????? long Rabinmillerknl (uint64 n)? ?????? {??????????? uint64 b, m, j, v, i; ??????????? M = N-1; ???? ??????? j = 0; ??? // 0, first calculate M, J, so that N-1 = m * 2 ^ j, where M is odd, J is a non-negative integer ????? ?????? while (1! = (m & 1)) ??? // (! (m & 1)) ????????????? ????? j; ???????????????? >> = 1; ???????????} ??? // 1, random Take a B, 2 <= B ??????????? b = 2 g_rnd.random (n - 3); ??? // 2, calculate v = b ^ m mod n

??????????? v = POWMOD (b ,? m ,? n); ??? // 3, if v == 1, through test

??????????? IF (v == 1)

??????????? {

??????????????? RETURN 1;

???????????} ??? // 4, order i = 1

??????????? i = 1; ??? // 5, if v = N-1, through test

??????????? while (v! = n - 1)

??????????? {

???????????????? // 6, if i == L, non-prime number, end

??????????????? IF (i == j)

??????????????? {

??????????????????? RETURN 0;

????????????????} ???????} ??????? // 7, v = V ^ 2 mod n, i = i 1

??????????????? v = POWMOD (V, 2,? n);

??????????????? i; ??????? // 8, loop to 5

???????????} RETURN 1;

???????}

??????? / * ??????? Rabin-miller number test, cyclic calling core loop times ??????? all by returning 1, otherwise return 0 ??????? * / ??????? long rabinmiller (uint64 n, long loop) ??????? {??????????? // first use small pixels once, improve efficiency??? ???????? for (long i = 0; i

??????????? // cycle calls the Rabin-Miller test loop, making the non-quantity pass the probability of test to (1/4) ^ LOOP ??????????? for (long i = 0; i

??????? / * Europa, the number of laws, the number of conventions * / ??????? uint64 Euclidgcd (uint64 p, uint64 q) ??????? {??????? ???? uint64 a = p> q? P: q; ??????????? uint64 b = P q? P: q; ??????????? uint64 b = P > = 1; ???????????????????? b >> = 1; ?????????????? ?} ??????????????? i (0 == (A & 1)) // (! (A & 1)) ?????????????? ? {??????????????????? T = a; ??????????? // If A is an even number, swap A, B??? ????????????????? a = B; ???????????????????? b = t; ??????? ????????} Do ?????????????????????????????????????????????????????????????????????????? (b & 1)) / / (! (b & 1)) ????????????????????????? ?????????? b >> = 1; ????? // b is an even number, A is an odd number, GCD (B, A) =

GCD (B / 2, A) ?????????????????????} if (b > 1; // b, A is odd, GCD (b, a) = GCD ((ba) / 2, a) ???????????????} while (b! = 0); // (b); ?????????? ????? return r * a; ???????????} ???????} ???????????????? is known A, b, ask X, meet A * x = 1 (MOD B) ??????? is equivalent to solving the minimum integer solution of A * XB * Y = 1 ??????? * / ????? ?? uint64 euclid (uint64 a, uint64 b) ??????????? uint64 m????? uint64 m, E, I, J, X, Y; ??????? ???? long xx, yy; ??????????? m = b; ??????????? e = a; ??????????? x = 0; ??????????? y = 1; ???????????? xx = 1; ??????????? yy = 1;? ?????????? while (0! = e) // (e) ???????????????????i = M / E; ??????????????? j = m% e; ??????????????????????????? ?????????? e = j; ??????????????? j = Y; ??????????????? y * = i; ???????????????? f (xx == yy) ??????????????? {????????? ?????????? f (x> y) ???????????????????????????????????????????? ????????? y = x - y; ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???? ELS e ???????????????????? {???????????????????????? y - = x ;? ??????????????????????? yy = 0; ???????????????????}???? ???????????} ???????????????????????? {?????? ?????????????? y = x; ???????????????????? xx = 1-xx; ??????? ???????????? yy = 1 - yy; ????????????????} x = j; ???????????} ??????????? IF (xx == 0) ??????????? {???????????????? x = B - X ; ???????????} return x; ???????}

??????? / * randomly generate a RSA encryption parameter * / ???????? RSA_PARAM RSAGETPARAM () ??????????? RSA_PARAM RSA = New RSA_Param (); ??????????? uint64 t; ??????????? rsa.p = randomprime ((char) 16); ???????? ? // randomly generate two prime numbers ??????????? rsa.q = randomprime ((char) 16); ??????????? rsa.n = rsa.p * RSA.Q; ??????????? rsa.f = (RSA.P - 1) * (RSA.Q - 1); ??????????? DO??? ????????? {??????????????? rsa.e = g_rnd.random (65536) ;? // less than 2 ^ 16, 65536 = 2 ^ 16 ?? ????????????? rsa.e | = 1; ?????????????????? // guarantee the lowest position 1, that is, the guarantee is an odd number Because f must be an even number, to be mutual, can only be odd ???????????} while (STEINGCD (RSA.E, RSA.f)! = 1); RSA.D = Euclid RSA.E, RSA.f); ??????????? rsa.s = 0; ??????????? T = RSA.N >> 1; ???? ??????? while (0! = t) // (t) ??????????? {???????????? rsa.s ; ??????????????????????????????????????????? t >> = 1;?? ??????????} return RSA; ???????} ??????? / * Labin-Miller Test * / ??????? Void TestrM ()? ?????? {??????????? uint32 k = 0; ??????????? console.write ("- rabin-miller prime check./n/n "); ??????????? for (uint64 I = 4197900001; i <4198000000; i = 2) ??????????? {???????? ????? ?? IF (0! = Rabinmiller (i, 30)) ????????????????????????????? K ; ??????????????????? console.writeline (i); ???????????????} ??????? ????} ??????????? console.writeline ("total:" k); ???????}

??????? / * RSA encryption decryption * / ??????? void testRSA () ????????? rsa_param r;??? ???????? String psrc = "abcdefghijklmnopqrstuvwxyz"; ??????????? uint32 n = (uint) psrc.length; ????????????// unsigned Char ?????? * q, PDEC [N]; ??????????? byte [] PDEC = New Byte [n]; ??????????? uint64 [ ] penc = new uint64 [n]; ??????????? r = rsAgetParam (); ??????????? console.writeline ("p =" rp);? ?????????? console.writeline ("q =" rq); ??????????? console.writeLine ("f = (p-1) * (q-1 ) = " rf); ??????????? console.writeLine (" n = p * q = " rn); ??????????? console.writeLine (" e = " re); ??????????? console.writeline (" d = " rd); ??????????? console.writeLine (" s = " RS); ??????????? console.writeline ("Source:" psrc); ???????????? q = (unsigned char *) PSRC; ?? ????????? console.write ("eNCode:"); ??????????? for (int i = 0; i <(int) n; i ) ???? ??????? {?????????????????penc [i] = potMod (q [i], re, rn); ???????? ??????? penc [i] = potmod ((ulong) PSRC [I], RE, RN); ??????????????? console.write (Penc [i] .Tostring () ""); ???????????} con Sole.writeline (""); ??????????? console.write ("decode:"); ??????????? for (int i = 0; i < int) n; i ) ??????????? {??????????????? PDEC [i] = (byte) POWMOD ((Ulong) PENC [i] , rd, rn); ??????????????? console.write ((uint32) PDEC [i] ""); ???????????} console .Writeline (""); ??????????? console.writeline (pdec)); ???????} / * * / ?????? ? static void main (string [] args) ??????????? NEW Program (). TestRSA ();

???????} ???}}

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

New Post(0)