Asymmetric encryption and decryption-RSA Algorithm

Source: Internet
Author: User
Tags gmp
Generation of public and private keys

N = 17 * 53 = 901m = (17-1)*(53-1)=832let e=17 // by random d = 49 // because ( d * e ) % m = 1 , that is ( d * 17 ) % 832 = 1

Public Key (901,17) private key (901,49)

Encryption Process

Int text = 123 (note that the message must be smaller than N );

Because text ^ e % N = code, that is, 123 ^ 17% 901 = Code

Get code = 378

Decryption process

Int code = 378;

Because code ^ d % N = text, that is, 378 ^ 49% 901 = text

Get text = 123

Continue test text = 456, text = 789

pow=337587917446653715596592958817679803remain is encrypted msg, 378pow=1981975090781375567648692626283639181924626345824851461901652399140883944715714801363950285365649593405611421793924739086942208remain is decrypted msg, 123pow=1593684413895892775431288016392119629314523136remain is encrypted msg, 31pow=11932267966476130911517907186867185074148138371734008968629799047943267871remain is decrypted msg, 456pow=17795453274736834362412998969384875211516379055829remain is encrypted msg, 24pow=42692426938810341355395254247148178158685421582623594902764046516224remain is decrypted msg, 789

If the random private key is 23, obtained from (D * E) % 832 = 1, D = 1447

Key pair (901,23) (901,1447)

Encryption process 2

Int text = 123 (note that the message must be smaller than N );

Because text ^ e % N = code, that is, 123 ^ 23% 901 = Code

Get code = 149

./rsa 123 23 901
pow=1169008215014432917465348578887506800769541157267remain=149

Decryption process 2

Int code = 149;

Because code ^ d % N = text, that is, 149 ^ 1447% 901 = text

Get text = 123

./rsa 149 1447 901pow=3985936655027786133408829502155172486035904622712472852621608241746913310297391658184716646894437322554704092666282273452855275384085318288046586224429347497827880247547339473036917816909223107117175531587260639648846353409844711958706092093596730245565996912003513233565007065727967455584025260911486810746630678094905762197449698418393359848248040053204101912486274919097714199485366154899138632699959288422245852748033196204793416492537560971606021510788711927751466478702987029084746015114294265995598688131965702319407549561404014161346291768678253671588393974329706005805322594725598901652699451931023169921356303019131994689149252324743527490479491940886193543681983109194838286138082417166699477848202718347964687820796592019328071391913496121999081649165198851436538061394347514783202440077109779016851948752385464899137550781743050874740569187871261877899262047556241037569513727241777013958245104990221096084832705392187584307190592892229359969096765098092852369655657703699448285550244408917771734036376812464079731831055765945558390839789584335008109083209997253493029025530744552730080488782762304788784814726079959495953838029404233253982382231821825059796386932843561833899624385090559266388309444170895176942274476213381834239263776359183712755409616899578532221617869355630180167696863598518217920559059880617368830175766115161601439972079153451714153741788486717251510899260246046823014371599692024285779413561563126708199148100439542764252953767607725285544447473028149388111275729639654549626974834654173824045613468737321214142959704129737498708807684383933171830693895907145483744154610946805717856953750819840697171152907340638764324070540105234490652588160359246349466614681404427878668385500329188714136429101735281721801482418741695141767516243529016466547669220693256890471638180522587977329481861127690785609948263132745032373176370846812221619669962814451607786769451764283956667048224778080078119822225893910169294528225164973914039173067841802331919352006767688214788854741209476129408348246067157963999292702077540465878741199438072301716437512437206906827652022918225801876338146245435414504850134452701815858979786932044372642085983768077238865428181442964225497161921056275373467702146501770271202169524135998423744124666111748049368433360408756284224496271587223056244362621755475005116811157719670973646470109741830322978187377451183707774175105440143172725174869125803081026121461473916991813456601031329814195352576206287933278596520860904193983118052847549233481720087449879783102557667902374897959615060335464554488838194211827049636607371477869631641345731986506361643642064555937668953135519546086134667128658277195852003986966590034063953363008297418066438616213070509869485074973762746760573414761610051773364844423773838759134377078566230721304823672091660155346094353132676209154505265788271907294843328075680525047092758196227733579042369746326640103471517003588082621099265771434203691296983507676617026988511984284930947929603040015756972741093414371038843713030209328408570065664368532865353731976350820941739288708181549678167180213092260955630329359228466794631319443883593072931779043019549remain=123

Optimization of decryption (Chinese Remainder Theorem)

Dp = D Mod (p-1) = 1447 Mod (17-1) = 7dq = D Mod (Q-1) = 1447 Mod (53-1) = 43qinv * q mod p = 1 that is, qinv * 53 mod 17 = 1 get qinv = 9

m1=code^dp mod p = 149^7   mod17  = 4m2=code^dq mod q = 149^43 mod 53 = 17h= (qInv * (m1-m2))mod p = (9 * (-13)) mod 17 = 2r= m2 + h*q = 17 + 2*53= 17 + 106 = 123 

The result is the same as that obtained by the above optimization solution.

Demo program

It is written in C language. The big data computing library uses an Open Source GMP library, as shown below:

Http://gmplib.org/

The most common algorithm is the M modulo code of the N power of X.

#include <stdio.h>#include <stdlib.h>#include <gmp.h>int main(int argc, char * argv[]){mpz_t pow, mod;mpz_init(pow);mpz_init(mod);mpz_ui_pow_ui(pow, atoi(argv[1]), atoi(argv[2]));printf("pow=");mpz_out_str(stdout, 10, pow);printf("\n");mpz_mod_ui(mod, pow, atoi(argv[3]));printf("remain=");mpz_out_str(stdout, 10, mod);printf("\n");return 0;}
Project File

Http://hi.csdn.net/attachment/201201/20/0_1327043412h7wW.gif

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.