Use libtommath to implement the RSA Algorithm

Source: Internet
Author: User

RSAAlgorithmDescription:

1) select two large prime numbers p and q and calculate n = p * q;

2) generate E, D to make:

E * D = 1mod (p-1) (q-1)

E and P-1 (q-1)

[Public Key] e, n

[Private Key] d, n

3) encryption:

C = m ^ d mod n

4) Decryption:

M = C ^ e mod n

Bytes --------------------------------------------------------------------------------------

Libtommath is a large number algorithm library. The followingCodeIt is implemented using the functions in this library, which is quite simple.

# Include <tommath. h> typedef struct {int bits;/* bits in key */mp_int N;/* modulus */mp_int E;/* Public exponent */mp_int D; /* private exponent */} rsa_key; int rsa_rng (unsigned char * DST, int Len, void * dat) {int X; For (x = 0; x <Len; X ++) DST [x] = rand () & 0xff; return Len;} int rsa_preme_random (mp_int * a, int bits) {int err = mp_prime_random_ex (A, 8, BITs, ltm_prime_2msb_on | ltm_prime_safe, rsa_rng, Null); If (Err! = Mp_okay) {return-1;} return 0;} int rsa_gen_key (rsa_key * Key, int bits) {mp_int p, q; mp_int sp, SQ; mp_int n, m; mp_int E, D; mp_int t; // init mp_intsmp_init (& P); mp_init (& Q); mp_init (& SP); mp_init (& sq ); mp_init (& N); mp_init (& M); mp_init (& E); mp_init (& D); mp_init (& T); // genarate P & qrsa_preme_random (& P, bits/2); rsa_preme_random (& Q, bits/2); // make N & MMP _sub_d (& P, 1, & SP); mp_sub_d (& Q, 1, & sq); mp_mul (& P, & Q, & N); mp_mul (& sp, & SQ, & M); // make E & D mp_set (& E, 127); retry_e: mp_gcd (& E, & M, & T); If (mp_cmp_d (& T, 1)> 0) {mp_add_d (& E, 2, & E); goto retry_e;} mp_invmod (& E, & M, & D); // copy n d e to key structmp_init (& Key-> N ); mp_init (& Key-> D); mp_init (& Key-> E); key-> bits = bits; mp_copy (& N, & Key-> N ); mp_copy (& D, & Key-> D); mp_copy (& E, & Key-> E); mp_clear (& P); mp_clear (& Q ); mp_clear (& SP); mp_clear (& sq); mp_clear (& N); mp_clear (& M); mp_clear (& E); mp_clear (& D ); mp_clear (& T); Return 0;}/* Set RSA key by string */INT rsa_set_key (rsa_key * Key, char * sn, char * se, char * SD, int bits, int Radix) {key-> bits = bits; mp_init (& Key-> N); mp_init (& Key-> D ); mp_init (& Key-> E); If (SN) mp_read_radix (& Key-> N, Sn, Radix); If (SE) mp_read_radix (& Key-> E, Se, radix); If (SD) mp_read_radix (& Key-> D, SD, Radix); Return 0 ;}/ * encrypt by private key */INT rsa_encrypt (mp_int * C, mp_int * m, rsa_key * Key) {mp_exptmod (C, & Key-> D, & Key-> n, m); Return 0 ;} /* decrypt by Public Key */INT rsa_decrypt (mp_int * m, mp_int * C, rsa_key * Key) {mp_exptmod (M, & Key-> E, & Key-> N, c); Return 0;} int rsa_test () {mp_intc, M; rsa_key key; char Sn [] = "inherit"; char se [] = "010001 "; char Sm [] = "inherit"; char SC [1024]; mp_init (& C); mp_init (& M); rsa_set_key (& Key, Sn, Se, null, 128, 16); mp_read_radix (& M, Sm, 16); rsa_decrypt (& M, & C, & Key); mp_toradix (& C, SC, 16 ); printf ("% s \ n", SC); return 0 ;}

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.