RSA Encryption Algorithm in a Nut Shell

35 179 0
RSA Encryption Algorithm in a Nut Shell

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

RSA Encryption Algorithm in a Nut Shell

RSA Encryption Algorithm in a Nut Shell. 1 RSA Encryption Algorithm in a Nut Shell Abstract To analyze the RSA encryption algorithm and present a working implementation in python. We discuss the mathematical results and see why the math works. The proofs of various number theoretic results subsequently discussed are available in books mentioned in the bibliography and thus omitted. Detailed discussions on big oh notation, time complexity of basic bit operations, Euclidean and extended Euclidean algorithm, time complexity of Euclidean algorithm, time complexity of extended Euclidean algorithm, linear congruences, Euler totient function, Fermats little theorem, Euler’s theorem, the Miller-Rabin test are presented. With this mathematical background we then analyze the RSA algorithm followed by a simplifed example. Finally, the documented python code for the RSA algorithm is presented and is hoped to be of use for serious programmers who intend on implementating the algorithm on a workstation. RSA Encryption Algorithm in a Nut Shell. 2 Index Chapter One Ê Notation ……………………………………………………………………………… 04 Ê Definitions …………………………………………… …………………………… 04 Chapter Two Ê Mathematcial Background • Big Oh notation………………………………………………………………05 • Rules for binary addition…………………………………………………… 06 • Rules for binary multiplication……………………………………………….07 • Rules for binary subtraction………………………………………………….08 • Rules for binary division………………………………………… …………08 • Relations and equivakence classes………………………………………… 09 • Euclidean algorithm………………………………………………………….11 • Time complexity of Euclidean algorithm…………………………………….12 • Extended Euclidean algorithm……………………………………………….12 • Time complexity of Extended Euclidean algorithm………………………….13 • Linear Congruence………………………………………………………… 13 o Definition…………………………………………………………… 13 o Cancellation law of congruence…………………………………… 13 • Relatively Prime…………………………………………………………… 13 • Existence of multiplicative inverse……………………………………… …13 • Euler’s Totient function………………………………………………………15 • Algorithm for binary exponentioation modulo m………….…………………16 • Time complexity of binary exponentioation modulo m….………………….16 • Introduction to Finite Field theory……………………………………… ….17 o Multiplicative generators of finite field in F p * ……………………… 17 • Fermat’s little theorem………………………………………………… … 18 • Euler’s theorem………………………………………………………………19 • Corollary of Euler’s theorem…………………………… ………………….20 . RSA Encryption Algorithm in a Nut Shell. 3 Chapter Three Ê RSA Encryption Algorithm ………………………………………………………….21 • Example of RSA encryption algorithm………………………………………22 Ê Miller-Rabin test for primality ………………………………………………………23 • Algorithm for Miller-Rabin test…………………………………………… 24 Chapter Four Ê Python code …………………………………………………………………………….25 Bibliography RSA Encryption Algorithm in a Nut Shell. 4 Chapter One Notations Z: The set of integers. Z + : The set of positive integers. a|b: a divides b. gcd(a.b): Greatest Common divisor of a and b. O: Big oh notation. [x]: The greatest integer function. a==b: a is congruent to b a^b=a b . Definitions Divisibility: Given integers a and b, a divides b or b is divisible by a, if there is an integer d such that b=ad, and can be written as a | b. E.g. 3|6 because 6/3=2 or 6=2*3. Fundamental Theorem of Arithmetic: Any integer n, can be written uniquely (except for the order of the factors) as a product of prime numbers n= p 1 a1 * p 2 a2 *. . .* p n an , n has (a1+1)*(a2+1)*. . .*(an+1) different divisors. E.g. 18= 2 1 *3 2 . Total number of divisors for 18 are (1+1)(2+1)=6, namely 3,9,6,18,1,2. gcd(a,b): Given two non-zero integers a and b, their gcd is the largest integer d such that d|a and d|b. Note: d is also divisible by any integer that divides both a and b. E.g. gcd(30,15) = 15, 15|30 and 15|15, 15 is divisible by any integer that divides both (30,15). We see that 5|30 and 5|15, which means that 15 should be divisible by 5, which is true. RSA Encryption Algorithm in a Nut Shell. 5 Chapter Two Mathematical Background Big Oh notation A function f (n)=O(g(n) ) or f=O(g), if there exists constants c,n 0 such that f(n)<= C.g(n) for all n>= n 0 Figure 1, as below shows the growth of functions f(n) and g(n). For n>=n 0 , we see that f(n)<= C.g(n), i.e. f(n) is bounded by C.g(n) from above. We also observe that the graph is in the first quadrant and thus all values of n are positive. C .g(n) f(n) n 0 n Figure 1 We now look at a simple example to illustrate the concept. E.g. f(n)= (n+1) 2 = n 2 +2n+1 Æ(1) <= n 2 +2n 2 <=3 n 2 = C.g (n), where C=3 and n 0 =2 Thus the upper bound is O(n 2 ) Let us look at (1) again. RSA Encryption Algorithm in a Nut Shell. 6 n 2 +2n+1 <= n 3 +2n 3 <=3 n 3 = C.g (n), where C=3, n 0 =1 Here the upper bound is O (n 3 ) Which is the correct upper bound, O (n 2 ) or O (n 3 )? Both the bounds are correct. However O (n 2 ) is closer to the actual upper bound., thus we choose O (n 2 ) as the upper bound for the above example. Time complexity is the most important factor that decides the runtime of the algorithm. We now look at the time complexities for a few simple bit operations. Rules for Binary Addition Let a denote the first bit, b denote the second bit, c denote the carry, s denote the solution, then- If a=0, b=0, c=0 ; s=0, c=0. a=0, b=0, c=1 ; s=1, c=0. a=1, b=0, c=0 ; s=1, c=0. a=0, b=1, c=0 ; s=1, c=0. a=1, b=0, c=1 ; s=0, c=1. a=0, b=1, c=1 ; s=0, c=1. a=1, b=1, c=0 ; s=0, c=1. a=1, b=1, c=1 ; s=1, c=1. Doing this procedure once is called a bit operation. Adding two, k-bit numbers require k bit operations. Exclusive OR is same as bitwise addition modulo 2 or addition without carry. E.g. Add m=1010 with k=101 1010 + 101 1111 RSA Encryption Algorithm in a Nut Shell. 7 Every bit addition performs one the above-mentioned rules. Thus, to add a k bit number by another k bit we need k bit operations. To add a ‘m’ bit number with a ‘k’ bit number, m>k, takes k bit operations. We note that at the Most Significant Bit (msb) of 1010, there is no corresponding bit of 101 to add. Here we simply write down the msb bit of 1010 onto the solution without performing any binary operations. Rules for Binary Multiplication Rules of binary multiplication are the same as that of a logical AND gate. 0.0=0 0.1=0 1.0=0 1.1=1 We illustrate the multiplication through an example. Let m be a k bit integer and n be an l bit integer. E.g. Multiply m=11101 with n=1101 11101 * (k) 1101 (l) 11101 (row 1) 11101 (row 2) 11101 (row 3) 101111001 The second addition row does not calculate 0*1101 as it would not make any difference to the total sum. Thus we simply shift another position and carry out the next multiplication. We observe that there are utmost l addition rows. In order to perform additions, we add row 1with row 2. Then we add this partial sum along with the next row and so on. We observe that at each addition step there are utmost k bit operations, when (k>l). Thus, upper bound on time in multiplying k bit number by l bit number = k * l. RSA Encryption Algorithm in a Nut Shell. 8 If both are k bit numbers, then upper bound on time for multiplying k with k = k 2 bit operations, where k=[log 2 m]+1 [x] is the greatest integer function <=x where x belongs to the set of real numbers. E.g. [15/2]=[7.5]=7 [-7.5]= -8 Thus, k 2 = O ( ([log 2 m]+1) * ([log 2 m]+1) ) = O([log 2 m]+1} 2 Rules for Binary Subtraction 0-0=0 1-0=1 1-1=0 0-1=1 with carry from next significant bit. E.g. 10101-10011 10101 – 10011 00010 If we look at the subtraction, we see that binary subtraction takes the same upper bound on time as binary addition, which is O(k), where k is the number of bits in the output. Rules for Binary Division We illustrate the rules for binary division by an example E.g. divide m=(1010) 2 with n=(11111) 2 Let q denote the quotient and r the remainder. RSA Encryption Algorithm in a Nut Shell. 9 1010| 11111 | q=11 1010 01011 – 1010 0001 = r Let n be a k bit integer. Each step involves one multiplication and one subtraction. The multiplication at each step takes utmost k bit operations. (1010) 2 occupy 4 bits of space. Thus, each subtraction step takes 4 binary operations. There are utmost k subtraction steps and takes 4*k operations in all for subtraction. Thus, there are a total of (4*k)*k bit operations. = O ( ([log 2 n]+1) * ([log 2 n]+1) ) = O ([log 2 n]+1} 2 = O (k 2 ). is the upper bound on time for binary division. Relations and Equivalence Classes If A and B are non empty sets, a relation from A to B is a subset of A*B, the cartesian product. If R is a proper subset of A*B and the ordered pair (a, b) €R, we say a is related to b represented as aRb. The set A is said to be a proper subset of B if there is at least one element in set B that is not in set A. E.g. Consider the sets A= {0, 1, 2} and B= {3, 4, 5} Let R= {(1, 3), (2, 4), (2, 5)} i.e. 1R3 2R4 2R5 We see that the relation R ‘is less than’ holds since 1<3 RSA Encryption Algorithm in a Nut Shell. 10 2<4 2<5 Hence the order of appearance is important here. An equivalence relation is reflexive, symmetric and transitive by definition. A partition of a set is a decomposition of the set into subsets, such that every element of the given set becomes a member of some subset and the intersection of the subsets is the null set. It means that an element cannot reappear in more than one subset. The subsets in a partition are called cells or blocks. E.g. All the partitions of the set A={1,2} is {1,2} {1},{2} {2},{1} The equivalence class of an element a €A is the set of elements of A to which a is related to. It is denoted by [a]. This notation is not be confused with the notation for the greatest integer function. The meaning of the notation is clearly stated wherever it appears. E.g. Let R be an equivalence relation on the set A={6,7,8,9,10} defined by R={(6,6) (7,7) (8,8) (9,9) (10,10) (6,7) (7,6) (8,9) (9,8) (9,10) (10,9) (8,10) (10,8)}. The equivalence classes are [6]=[7]={6,7} [8]=[9]=[10]={8,9,10} The partitions are {(6,7) (8,9,10)}. The set of equivalence classes are called residue classes and denoted by Z/mZ. Any set of elements for the residue class is calculated modulo m. E.g. The equivalence class for Z/5Z is [0],[1],[2],[3],[4] such that [0]={. . .,-10,-5,0,5,10, . . . } [1]={. . .,-9,-4,-1,1,6,11, . . . } [2]={ . . .,-8,-3,2,7, . . . } [3]={ . . .,-7,-2,3,8, . . .} [4]={ . . .,-6,-1,4,9, . . .} It is clear that, any element of [0]modulo 5 = 0. Any element of [1] modulo 5 =1 and so on. [...]... _rsa_ dsp_i = 0 _rsa_ dsp_t = 0 def rsadsp(d): global rsa_ dsp rsa_ dsp = d 25 RSA Encryption Algorithm in a Nut Shell def _rsa_ dsp_init(): global _rsa_ dsp_t _rsa_ dsp_t = time() def _rsa_ dsp_end(): out.write(strftime(" # keys created in %H:%M:%S\n", gmtime(time() _rsa_ dsp_t))) def _rsa_ dsp_iter(b=False): if (b): out.write( _rsa_ dsp_sequence[1]) else: global _rsa_ dsp_i _rsa_ dsp_i += 1 _rsa_ dsp_i %= len( _rsa_ dsp_sequence[0])... sides with ak, we have ac.phi(n)+k== ak mod n Therefore, al==ak mod m We make use of this property in RSA algorithm during decryption i.e., if e and d be two arbitrary integers such that e*d==1 mod phi(n) and gcd( e, phi(n) )=1, then Me*d==M1 mod n, where M is another arbitrary integer 20 RSA Encryption Algorithm in a Nut Shell Chapter Three RSA Encryption Algorithm RSA is a public key encryption algorithm. .. of attacks on RSA and [2] is an excellent guide for writing practical algorithms Both are easily available for download over the internet An example of the RSA algorithm: We now look at an over simplified example for illustrating the algorithm Let p=3 and q=11, be two randomly selected primes n=3*11=33 phi(n)=(3-1)*(11-1)=20 22 RSA Encryption Algorithm in a Nut Shell We choose randomly, e such that gcd(e,20)=1... rsadsp(True) if name == " main ": e,d = keypair(1024) print "\nPublic Key:" print e print "\nPrivate Key:" print d raw_input() 34 RSA Encryption Algorithm in a Nut Shell BIBLIOGRAPHY 1 Boneh.D, Twenty years of attacks on the RSA Cryptosystem, Notices of the American Mathematical Society, February 1999 2 IEEE P1363/D13(Draft Version 13) Standard Specifications for Public Key Cryptography, Annex A( Informative),... return sval # # # # hexpack reads a string an interprets it as a long integer number stored byte by byte in little endian format and returns that integer 26 RSA Encryption Algorithm in a Nut Shell def hexpack(s,l=0): hret = 0L if not l: l = long(len(s)) for i in range(l): val = long(ord(s[i])) val = val . RSA Encryption Algorithm in a Nut Shell. 1 RSA Encryption Algorithm in a Nut Shell Abstract To analyze the RSA encryption algorithm and present a. theorem…………………………… ………………….20 . RSA Encryption Algorithm in a Nut Shell. 3 Chapter Three Ê RSA Encryption Algorithm ………………………………………………………….21 • Example of RSA encryption algorithm………………………………………22 . this mathematical background we then analyze the RSA algorithm followed by a simplifed example. Finally, the documented python code for the RSA algorithm is presented and is hoped to be of

Ngày đăng: 08/06/2014, 22:50

Từ khóa liên quan

Mục lục

  • Abstract

  • Index

  • Chapter One

  • Chapter Two

  • Chaptert Three

  • Chapter Four

  • Bibliography

Tài liệu cùng người dùng

Tài liệu liên quan