HW 5-RSA/ascii2str.m
function str = ascii2str(ascii)
% Convert to string
str = char(ascii);
HW 5-RSA/bigmod.m
function remainder = bigmod (number, power, modulo)
% modulo function for large numbers, -> number^power(mod
modulo)
% by bennyboss / 2005-06-24 / Matlab 7
% I used algorithm from this webpage:
% http://www.disappearing-inc.com/ciphers/rsa.html
% binary decomposition
binary(1,1) = 1;
col = 2;
while ( binary(1, col-1) <= power-binary(1, col-1) )
binary(1, col) = 2*binary(1, col-1);
col = col + 1;
end
% flip matrix
binary = fliplr(binary);
% extract binary decomposition from number
result = power;
cols = length(binary);
extracted_binary = zeros(1, cols);
index = zeros(1, cols);
for ( col=1 : cols )
if( result-binary(1, col) > 0 )
result = result - binary(1, col);
extracted_binary(1, col) = binary(1, col);
index(1, col) = col;
elseif ( result-binary(1, col) == 0 )
extracted_binary(1, col) = binary(1, col);
index(1, col) = col;
break;
end
end
% flip matrix
binary = fliplr(binary);
% doubling the powers by squaring the numbers
cols2 = length(extracted_binary);
rem_sqr = zeros(1, cols);
rem_sqr(1, 1) = mod(number^1, modulo);
if ( cols2 > 1 )
for ( col=2 : cols)
rem_sqr(1, col) = mod(rem_sqr(1, col-1)^2, modulo);
end
end
% flip matrix
rem_sqr = fliplr(rem_sqr);
% compute reminder
index = find(index);
remainder = rem_sqr(1, index(1, 1));
cols = length(index);
for (col=2 : cols)
remainder = mod(remainder*rem_sqr(1, index(1, col)),
modulo);
end
HW 5-RSA/EGCP447-Lecture No 10.pdf
RSA Encryption
IT), 1978
– Number theory – determining prime factors of a given
(large) number
-) 5, 6 -) 2 *3
– Arithmetic modulo n
– So far remains secure (after all these years...)
– Will somebody propose a quick algorithm to factor
large numbers?
– Will quantum computing break it? -) TBD
RSA Encryption
– P = E (D(P)) = D(E(P)) (order of D/E does not matter)
– More precisely: P = E(kE, D(kD, P)) = D(kD, E(kE, P))
– n is the key length
– Note, P is turned into an integer using a padding
scheme
– Given C, it is very difficult to find P without knowing
KD
s algorithm in detail next time
RSA Algorithm
1. Key Generation
– A key generation algorithm
2. RSA Function Evaluation
– A function F, that takes as an input a point x and a
key k and produces either an encrypted result or
plaintext, depending on the input and the key
Key Generation
complex part of RSA
generate both the public and the private RSA
keys
– Very important, weak key generation makes RSA
very vulnerable to attack
Key Generation
1. Large Prime Number Generation
– Two large prime numbers p and q need to be generated
– These numbers are very large: at least 512 digits, but 1024
digits is considered
safe
2. Compute n = pq
– n is used as the modulus for both the public and private keys
– Its length, usually expressed in bits, is the key length
3. Totient
– The totient of n,�(n) is calculated
4. Public Key
– A prime number is calculated from the range [3,�(n)) that has
a GCD of 1 with
�( n )
5. Private Key
– Because the prime in step 4 has a GCD of 1 with �(n), we are
able to
determine it's inverse with respect to mod �(n)
Large Prime Number Generation
hat two very large
prime numbers be generated that are quite far
apart
prime numbers that are close together makes
RSA totally insecure
Large Prime Number Generation
prime numbers?
very large random number) and test for
primeness
and start over again until we have a number that
passes a prime test
blem is now
– How do we test a number in order to determine if it is
prime?
Large Prime Number Generation
– An incredibly fast prime number tester called the
Rabin-Miller primality tester
at
implements this tester: “isprime”
numbers: p and q
Large Prime Number Generation
Compute n = p• q
compute n very easily:
n= p• q
n security foundation relies upon the
fact that given two large prime numbers, a
composite number (in this case n) can very easily
be deduced by multiplying the two primes
together
efficiently determining n’s prime factors
– In fact, it is considered a hard math problem!
Totient
very quickly calculated:
q 5 � = ( � �
q 5� = q 5�• � = q 5� • q 5�
= (� �
is if one can determine the prime factors of �,
then one can easily determine the totient
Public Key
number chosen
in the range [3, � � )
– You may think 3 is too small – I agree! – this could lead to
security flaws
– No, as long as the private key cannot be deduced from the
public key
and n and is present as follows
(�, �)
Private Key
multiplicative
inverse of the public key with respect to 0 n can be efficiently
and
quickly determined using the Extended Euclidean Algorithm
he following equation:
e• dE l mod 0 n
–
congruent to
– We call e • d the residue of a modulo 0 n and write e • d = (1
mod
0 n )
the
exponent d and modulus n:
(d, n)
Extended Euclidean
Algorithm
– (e • d ) -1 = i • c / � o r ( e • d ) = i • c / � + 1
Where i is a positive integer
while the following Euclidean remainder is
greater than 0 (r > 0)
� = � � � � �
– Where k= i•c/� + 1
4 9
=
7
= 7
�
– �=
�
Extended Euclidean Algorithm
Example
�= 3, �= 5, a n d
�= 7
�� = � �
=
2 * 4 = 8
– Do this loop while �> 0
– See table
culate �
� � �
1 1 9
2 2 17
3 3 25
4 4 33
5 5 41
6 6 49
7 0
RSA Function Evaluation
message into ciphertext (or vice-versa)
evaluated as follows:
Fm, k = m'modn
1. Encrypting with the public key, and then decrypting
with the private key
2. Encrypting with the private key, and then decrypting
with the public key
RSA Function Evaluation
from the first
� = m� mo� n = c,
where m is the message, e is the public key
and c is the cipher
� = c� mo� n =m
RSA Example
1. Calculation of n and Totient
– Lets choose two primes: p=11 and q=13
– Hence, n=p×q=143
– T h e t o t i e n t o f n �� = � �
RSA Example
2. Key Generation
– For the public key, a random prime number that has a GCD
o f 1 w i t h � � a n d i s l e s s t h a n � � i s c h o s e n
– Let’s choose 7
� �
– So e = 7, and to determine d, the secret key, we need to
find the inverse of 7 with � �
– This can be done very easily and quickly with the Extended
Euclidean Algorithm (see example above), and hence d =
103
– � �
-1 = 720 = 6*120
RSA Example
3. Encryption/Decryption
– Lets choose our plaintext message, m to be 9:
– Encryption: me mod n = 97 mod 143 = 48 = c
– Decryption: cd mod n = 48103 mod 143 = 9 = m
the built in “mod” Matlab function
– “mod” will not work for large numbers
A Real World Example
bit array and then one large single number
– For the HW, we will just use ASCII
1976620216402300889624482718775150
ASCII
-encoding scheme
converts a string to ASCII
CAIIEORNIA STATE UNIVOLSITY
FULLERTON"
ASCII CHART
Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char
0 00 Null 32 20 Space 64 40 @ 96 60 '
1. 01. Start of heading 65 41 A 97 61 a
34 22 " 66 42 B 98 62 b
3 03 End of text
if 67 43 C 99 63 c
rensmrt 36 24 $ 68 44 D 100 64 ci
05 Enquiry 37 25 % 69 45 E 101 65 e 5
6 05 Acknowledge 38 26 & 70 46 F 102 66 1
7 0'7 Audible bell 39 2'7 ' 71 67 G 103 67 9
8 08 Backspace 40 28 i 72 48 H 104 66 h
9 09 Horizontal tab 41. 29 1 73 49 I 105 69 i
10 OA Line feed 42 2A ,, 74 4A J 106 6A j
11 OH Vertical tab 43 26 + 75 4B X 107 6B k
12 02 Form reed 44 2C f 76 4C L 108 62 I.
13 OD Carriage return 45 2D - 77 4D N 109 6D m
14 OE Shift out 46 2E . 78 4E N 110 6E n
15 OF Shift In 47 2F / 79 4F 0 111 6F o
16 10 Data link escape 48 30 0 80 50 P 112 '70 9
1.7 11 Device control 1 49 3 1 1 81 51 0 113 '71 q
1.8 12 Device control 2 50 32 2 82 52 R 114 '72 r
1.9 13 Device control 3 51. 33 3 83 53 6 115 '73 a
14 Device control 4 52 34 4 84 54 T 116 74 t
21 eg acknowledge 53 35 5 85 55 U 11'7 75 u
16 22 Sy sidle 54 36 6 86 56 V 118 '76 v
23 17 Endtrans.blo 55 3'7 '7 8'7 57 11 119 '77 w
24 18 Cancel
8 8 88 58 X 120 '78 x
25 19 End of medium 57 32
89 59 Y 121 79 y
26 1A Substitution 58 3A : 90 5A Z 122 7A z
27 1B Escape 59 312 ; 91 5B [ 123 713 1
28 10 File separator 60 3C < 92 50  124 70 I
29 10 Group separator 61 3D - 93 50 ] 125 7D )
30 12 Record separator 62 3E > 94 52 ' 126 7E -'
31 1F Unit separator 63 3F 2 95 5F 127 7F 0
9S S U RS ES
Code P
el: MARAB PM Fa - academic use
= :2/ Preferences Communal,
Lsy,„,t v_t Set Pat He„
>ksue, +Add,.
. 4 6 M 1 - 1 0 ® e f F C u r r e n t
F o l d e r I W o r k s p a c e ®
alibi t Oro taboo (Cal Fullerton) I CS LIF Related f Courses I
EGCP-
dlarne Value
a n s
C o m m a n d F l i s t r a y
r e a d ( 1 . 1 2 0 )
c i c
r e a d ( 7 . 1 0 0
1 1 x t ( 9 0 . 1 0 1
d o u b l e ( P )
d a u b . ° ( I V O
d o u b l e ( f a »
d o u b l e ( f t ,
d o u b l e ( . 6 . ) ,
c i c
c . v . °
c i c
d o u b l e ( f a »
c l o a » l e »
» d o a bl e • S • /
6 5
d o u b l e C B .
6 6
d o u b l e Z . )
9 0
A >
m - A
feefert Save
Worlapace Clew Workspace 6 1:4Clerveenerancia 6 Libra,
Eta Pun ana Toe
t 6 : . 1
A Real World Example
Key Generation
other
-Miller primality
tests:
–
12131072439211271897323671531612440428472427633701410
925
63454931230196437304208561932419736532241686654101705
736
1365214171711713797974299334871062829803541
–
12027524255478748885956220793734512128733387803682075
433
65389998395517985098879789986914690080913161115334681
705
0832096022160146366346391812470987105415233
A Real World Example
Key Generation
� �
–
14590676800758332323018693934907063529240187237535716
439
95818710198734387990053589383695714026701498021218180
862
92467422828157022922076746906543401224889672472407926
969
98710058129010319931785875366371086235765651050788371
429
71156373427889114635351027120327651665184117268598379
886
72111837205085526346618740053
� �
– 14590676800758332323018693934907063529240187
237535716439
95818710198734387990053589383695714026701498021218180
862
92467422828157022922076746906543401224889648313811232
279
96631730139777785236530154784827347887129722205858745
715
28916064592697181192689711635550708026439995295496441
168
11947516513938184296683521280
A Real World Example
Key Generation
- the public key
– 65537 has a GCD of 1 with /. �, so lets use it as the public
key
– To calculate the private key, use extended Euclidean
algorithm
to find the multiplicative inverse with respect to /. �
- the private key
– 894894250092744443682285459217730939196695860658842
574454978544564876748396298183909349419732628796167
979706089172836798754993315741611138540888132754881
105882471930775825272784379065040156806234235500672
400424666656542323835029222154936232894721388664458
187891279461234078077257026266440910365023725451397
13
A Real World Example
Encryption/Decryption
– 1976620216402300889624482718775150e mod n
– 3505211133867302669021242393705332851188
0760811579981620642802346685810623109850
2359430490809733862411137840407947041939
7821537849976541308364643878474095230693
2534945195080183861574225226218879827232
4539128205968864403775360824656817500744
1745915148540744586251102347223556082305
3497791518928820272257787786
A Real World Example
Encryption/Decryption
– 35052111338673026690212423937053328511880760811
57998162064280234668581062310985023594304908097
33862411137840407947041939782153784997654130836
46438784740952306932534945195080183861574225226
21887982723245391282059688644037753608246568175
00744174591514854074458625110234722355608230534
97791518928820272257787786d mod n
our plaintext “attack at dawn”)
are that are used in the real world
HW 5-RSA/genPrivKey.m
function [n, d] = genPrivKey(p,q,e)
% function [n, d] = genPrivKey(p,q,e)
%
% Generates the private key exponent and the modulus
%
% Inputs:
% p: One of the prime numbers
% q: The other prime numbers
% e: Exponent of public key
%
% Outputs:
% n: Modulus (length of keys)
% d: Exponent of private key
%
HW 5-RSA/HW_5.pdf
1/3
HW 5 – RSA Encryption
Objectives
The objective of this homework is for you to learn about RSA
encryption. You will code and
test the RSA encryption algorithm. Additionally, a sub-
objective is to gain further experience with
the Matlab simulation environment.
Files
All the files (i.e., cover page, Matlab files, etc.) are contained
in a folder called “HW 3” which
is available for download on TITANium. You must download
these files prior to completing the
following tasks.
Background
RSA is one of the first practicable public-key cryptosystems and
is widely used for secure data
transmission. In such a cryptosystem, the encryption key is
public and differs from the decryption
key which is kept secret. In RSA, this asymmetry is based on
the practical difficulty of factoring the
product of two large prime numbers, the factoring problem.
RSA stands for Ron Rivest, Adi Shamir
and Leonard Adleman, who first publicly described the
algorithm in 1977. Clifford Cocks, an
English mathematician, had developed an equivalent system in
1973, but it wasn't declassified
until 1997.
A user of RSA creates and then publishes a public key based on
the two large prime numbers,
along with an auxiliary value. The prime numbers must be kept
secret. Anyone can use the public
key to encrypt a message, but with currently published methods,
if the public key is large enough,
only someone with knowledge of the prime factors can feasibly
decode the message. Breaking
RSA encryption is known as the RSA problem. It is an open
question whether it is as hard as the
factoring problem.
Description
Code the private key generation function (“genPrivKey.m”).
The genPrivKey function should
display the value of n, the public key (e), the value of the
totient (�), and the private key (d) in the
command window as shown in Figure 1. You will not need the
genPrivKey function to
decrypt/encrypt the messages. However, you still need to code
this function. Next, you will code
the RSA algorithm (“rsa.m”). Then provide a script
(“rsa_script.m”) that will complete the work
tasks below. I provided code that will convert from string to
ASCII and vice versa (i.e., “ascii2str.m”
and “str2ascii.m”. I also provided code to perform the modulus
(see “bigmod.m”). See the “RSA
Overview” lecture for a description of the algorithm.
2/3
Figure 1: genPrivKey example using p=11, q=13, and e=7.
Figure 2: Example decryption of the cipher for “Hello World”
using p=11, q=13, and d=103.
3/3
Figure 3: Example encryption of the plain text “Hello World”
using p=19, q=17, and e =7.
Work Task
Part 1 - Decrypt
Given the ciphers, decrypt the messages using p=11, q=13, and
d=103:
1. 89,59,33,98,67,40,39,98,69,62,59,29,98,77,108,110
2. 124,45,45,100,98,35,40,66,98,20,24
Part 2 - Encrypt
Encrypt the following messages using p=19, q=17, and e=7
(case sensitive):
1. the rain in spain falls mainly on the plain
2. Supercalifragilisticexpialidocious
Part 3 - Private Key Generation
Generate the private key given p=19, q=17, and e=7.
What to Turn In (Please read this carefully)
For this HW, you only need to provide the Matlab code and the
answers for the “Work Task”
section. You must put the complete Matlab functions and the
answers into a single PDF. Your code
must be in text format. Code provided as an image will not be
accepted. You must label everything
appropriately (i.e., label the code and work task sections). If I
can’t understand your answers or
code, I will assume it is incorrect. Also, please include the
cover page in your PDF document. This
HW will be a digital submission and it will be submitted online
using TITANium. No paper
submissions will be accepted.
HW 5-RSA/rsa.m
function mc = rsa(M,N,key)
% function mc = rsa(M,N,key)
%
% RSA Encryption/Decryption
%
% Inputs:
% M: Input ASCII plaintext/ciphertext
% N: Modulus (length of keys)
% key: Public or private key exponent (e or d)
%
% Output:
% mc: Output ASCII plaintext/ciphertext
%
HW 5-RSA/rsa_script.m
HW 5-RSA/str2ascii.m
function ascii = str2ascii(s)
% Convert to ASCII
ascii = double(s);

HW 5-RSAascii2str.mfunction str = ascii2str(ascii) .docx

  • 1.
    HW 5-RSA/ascii2str.m function str= ascii2str(ascii) % Convert to string str = char(ascii); HW 5-RSA/bigmod.m function remainder = bigmod (number, power, modulo) % modulo function for large numbers, -> number^power(mod modulo) % by bennyboss / 2005-06-24 / Matlab 7 % I used algorithm from this webpage: % http://www.disappearing-inc.com/ciphers/rsa.html % binary decomposition binary(1,1) = 1; col = 2; while ( binary(1, col-1) <= power-binary(1, col-1) ) binary(1, col) = 2*binary(1, col-1);
  • 2.
    col = col+ 1; end % flip matrix binary = fliplr(binary); % extract binary decomposition from number result = power; cols = length(binary); extracted_binary = zeros(1, cols); index = zeros(1, cols); for ( col=1 : cols ) if( result-binary(1, col) > 0 ) result = result - binary(1, col); extracted_binary(1, col) = binary(1, col); index(1, col) = col; elseif ( result-binary(1, col) == 0 )
  • 3.
    extracted_binary(1, col) =binary(1, col); index(1, col) = col; break; end end % flip matrix binary = fliplr(binary); % doubling the powers by squaring the numbers cols2 = length(extracted_binary); rem_sqr = zeros(1, cols); rem_sqr(1, 1) = mod(number^1, modulo); if ( cols2 > 1 ) for ( col=2 : cols)
  • 4.
    rem_sqr(1, col) =mod(rem_sqr(1, col-1)^2, modulo); end end % flip matrix rem_sqr = fliplr(rem_sqr); % compute reminder index = find(index); remainder = rem_sqr(1, index(1, 1)); cols = length(index); for (col=2 : cols) remainder = mod(remainder*rem_sqr(1, index(1, col)), modulo); end HW 5-RSA/EGCP447-Lecture No 10.pdf
  • 5.
    RSA Encryption IT), 1978 –Number theory – determining prime factors of a given (large) number -) 5, 6 -) 2 *3 – Arithmetic modulo n – So far remains secure (after all these years...) – Will somebody propose a quick algorithm to factor large numbers? – Will quantum computing break it? -) TBD RSA Encryption – P = E (D(P)) = D(E(P)) (order of D/E does not matter)
  • 6.
    – More precisely:P = E(kE, D(kD, P)) = D(kD, E(kE, P)) – n is the key length – Note, P is turned into an integer using a padding scheme – Given C, it is very difficult to find P without knowing KD s algorithm in detail next time RSA Algorithm 1. Key Generation – A key generation algorithm 2. RSA Function Evaluation – A function F, that takes as an input a point x and a key k and produces either an encrypted result or plaintext, depending on the input and the key Key Generation
  • 7.
    complex part ofRSA generate both the public and the private RSA keys – Very important, weak key generation makes RSA very vulnerable to attack Key Generation 1. Large Prime Number Generation – Two large prime numbers p and q need to be generated – These numbers are very large: at least 512 digits, but 1024 digits is considered safe 2. Compute n = pq – n is used as the modulus for both the public and private keys – Its length, usually expressed in bits, is the key length 3. Totient – The totient of n,�(n) is calculated 4. Public Key – A prime number is calculated from the range [3,�(n)) that has a GCD of 1 with
  • 8.
    �( n ) 5.Private Key – Because the prime in step 4 has a GCD of 1 with �(n), we are able to determine it's inverse with respect to mod �(n) Large Prime Number Generation hat two very large prime numbers be generated that are quite far apart prime numbers that are close together makes RSA totally insecure Large Prime Number Generation prime numbers? very large random number) and test for primeness
  • 9.
    and start overagain until we have a number that passes a prime test blem is now – How do we test a number in order to determine if it is prime? Large Prime Number Generation – An incredibly fast prime number tester called the Rabin-Miller primality tester at implements this tester: “isprime” numbers: p and q Large Prime Number Generation
  • 10.
    Compute n =p• q compute n very easily: n= p• q n security foundation relies upon the fact that given two large prime numbers, a composite number (in this case n) can very easily be deduced by multiplying the two primes together efficiently determining n’s prime factors – In fact, it is considered a hard math problem! Totient very quickly calculated: q 5 � = ( � � q 5� = q 5�• � = q 5� • q 5� = (� �
  • 11.
    is if onecan determine the prime factors of �, then one can easily determine the totient Public Key number chosen in the range [3, � � ) – You may think 3 is too small – I agree! – this could lead to security flaws – No, as long as the private key cannot be deduced from the public key and n and is present as follows (�, �) Private Key
  • 12.
    multiplicative inverse of thepublic key with respect to 0 n can be efficiently and quickly determined using the Extended Euclidean Algorithm he following equation: e• dE l mod 0 n – congruent to – We call e • d the residue of a modulo 0 n and write e • d = (1 mod 0 n ) the exponent d and modulus n: (d, n) Extended Euclidean Algorithm
  • 13.
    – (e •d ) -1 = i • c / � o r ( e • d ) = i • c / � + 1 Where i is a positive integer while the following Euclidean remainder is greater than 0 (r > 0) � = � � � � � – Where k= i•c/� + 1 4 9 = 7 = 7 � – �= � Extended Euclidean Algorithm
  • 14.
    Example �= 3, �=5, a n d �= 7 �� = � � = 2 * 4 = 8 – Do this loop while �> 0 – See table culate � � � � 1 1 9 2 2 17 3 3 25 4 4 33 5 5 41 6 6 49 7 0
  • 15.
    RSA Function Evaluation messageinto ciphertext (or vice-versa) evaluated as follows: Fm, k = m'modn 1. Encrypting with the public key, and then decrypting with the private key 2. Encrypting with the private key, and then decrypting with the public key RSA Function Evaluation from the first � = m� mo� n = c, where m is the message, e is the public key and c is the cipher � = c� mo� n =m
  • 16.
    RSA Example 1. Calculationof n and Totient – Lets choose two primes: p=11 and q=13 – Hence, n=p×q=143 – T h e t o t i e n t o f n �� = � � RSA Example 2. Key Generation – For the public key, a random prime number that has a GCD o f 1 w i t h � � a n d i s l e s s t h a n � � i s c h o s e n – Let’s choose 7 � � – So e = 7, and to determine d, the secret key, we need to find the inverse of 7 with � � – This can be done very easily and quickly with the Extended Euclidean Algorithm (see example above), and hence d = 103
  • 17.
    – � � -1= 720 = 6*120 RSA Example 3. Encryption/Decryption – Lets choose our plaintext message, m to be 9: – Encryption: me mod n = 97 mod 143 = 48 = c – Decryption: cd mod n = 48103 mod 143 = 9 = m the built in “mod” Matlab function – “mod” will not work for large numbers A Real World Example bit array and then one large single number
  • 18.
    – For theHW, we will just use ASCII 1976620216402300889624482718775150 ASCII -encoding scheme converts a string to ASCII CAIIEORNIA STATE UNIVOLSITY FULLERTON" ASCII CHART Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char 0 00 Null 32 20 Space 64 40 @ 96 60 ' 1. 01. Start of heading 65 41 A 97 61 a 34 22 " 66 42 B 98 62 b 3 03 End of text if 67 43 C 99 63 c
  • 19.
    rensmrt 36 24$ 68 44 D 100 64 ci 05 Enquiry 37 25 % 69 45 E 101 65 e 5 6 05 Acknowledge 38 26 & 70 46 F 102 66 1 7 0'7 Audible bell 39 2'7 ' 71 67 G 103 67 9 8 08 Backspace 40 28 i 72 48 H 104 66 h 9 09 Horizontal tab 41. 29 1 73 49 I 105 69 i 10 OA Line feed 42 2A ,, 74 4A J 106 6A j 11 OH Vertical tab 43 26 + 75 4B X 107 6B k 12 02 Form reed 44 2C f 76 4C L 108 62 I. 13 OD Carriage return 45 2D - 77 4D N 109 6D m 14 OE Shift out 46 2E . 78 4E N 110 6E n 15 OF Shift In 47 2F / 79 4F 0 111 6F o 16 10 Data link escape 48 30 0 80 50 P 112 '70 9 1.7 11 Device control 1 49 3 1 1 81 51 0 113 '71 q 1.8 12 Device control 2 50 32 2 82 52 R 114 '72 r 1.9 13 Device control 3 51. 33 3 83 53 6 115 '73 a 14 Device control 4 52 34 4 84 54 T 116 74 t 21 eg acknowledge 53 35 5 85 55 U 11'7 75 u 16 22 Sy sidle 54 36 6 86 56 V 118 '76 v
  • 20.
    23 17 Endtrans.blo55 3'7 '7 8'7 57 11 119 '77 w 24 18 Cancel 8 8 88 58 X 120 '78 x 25 19 End of medium 57 32 89 59 Y 121 79 y 26 1A Substitution 58 3A : 90 5A Z 122 7A z 27 1B Escape 59 312 ; 91 5B [ 123 713 1 28 10 File separator 60 3C < 92 50 124 70 I 29 10 Group separator 61 3D - 93 50 ] 125 7D ) 30 12 Record separator 62 3E > 94 52 ' 126 7E -' 31 1F Unit separator 63 3F 2 95 5F 127 7F 0 9S S U RS ES Code P el: MARAB PM Fa - academic use = :2/ Preferences Communal, Lsy,„,t v_t Set Pat He„ >ksue, +Add,.
  • 21.
    . 4 6M 1 - 1 0 ® e f F C u r r e n t F o l d e r I W o r k s p a c e ® alibi t Oro taboo (Cal Fullerton) I CS LIF Related f Courses I EGCP- dlarne Value a n s C o m m a n d F l i s t r a y r e a d ( 1 . 1 2 0 ) c i c r e a d ( 7 . 1 0 0 1 1 x t ( 9 0 . 1 0 1 d o u b l e ( P ) d a u b . ° ( I V O d o u b l e ( f a » d o u b l e ( f t , d o u b l e ( . 6 . ) , c i c c . v . ° c i c
  • 22.
    d o ub l e ( f a » c l o a » l e » » d o a bl e • S • / 6 5 d o u b l e C B . 6 6 d o u b l e Z . ) 9 0 A > m - A feefert Save Worlapace Clew Workspace 6 1:4Clerveenerancia 6 Libra, Eta Pun ana Toe t 6 : . 1 A Real World Example Key Generation
  • 23.
  • 24.
    A Real WorldExample Key Generation � � – 14590676800758332323018693934907063529240187237535716 439 95818710198734387990053589383695714026701498021218180 862 92467422828157022922076746906543401224889672472407926 969 98710058129010319931785875366371086235765651050788371 429 71156373427889114635351027120327651665184117268598379 886 72111837205085526346618740053
  • 25.
    � � – 14590676800758332323018693934907063529240187 237535716439 95818710198734387990053589383695714026701498021218180 862 92467422828157022922076746906543401224889648313811232 279 96631730139777785236530154784827347887129722205858745 715 28916064592697181192689711635550708026439995295496441 168 11947516513938184296683521280 AReal World Example Key Generation - the public key – 65537 has a GCD of 1 with /. �, so lets use it as the public key – To calculate the private key, use extended Euclidean algorithm to find the multiplicative inverse with respect to /. � - the private key – 894894250092744443682285459217730939196695860658842 574454978544564876748396298183909349419732628796167 979706089172836798754993315741611138540888132754881
  • 26.
    105882471930775825272784379065040156806234235500672 400424666656542323835029222154936232894721388664458 187891279461234078077257026266440910365023725451397 13 A Real WorldExample Encryption/Decryption – 1976620216402300889624482718775150e mod n – 3505211133867302669021242393705332851188 0760811579981620642802346685810623109850 2359430490809733862411137840407947041939 7821537849976541308364643878474095230693 2534945195080183861574225226218879827232 4539128205968864403775360824656817500744 1745915148540744586251102347223556082305 3497791518928820272257787786 A Real World Example Encryption/Decryption
  • 27.
    – 35052111338673026690212423937053328511880760811 57998162064280234668581062310985023594304908097 33862411137840407947041939782153784997654130836 46438784740952306932534945195080183861574225226 21887982723245391282059688644037753608246568175 00744174591514854074458625110234722355608230534 97791518928820272257787786d modn our plaintext “attack at dawn”) are that are used in the real world HW 5-RSA/genPrivKey.m function [n, d] = genPrivKey(p,q,e) % function [n, d] = genPrivKey(p,q,e) % % Generates the private key exponent and the modulus % % Inputs: % p: One of the prime numbers % q: The other prime numbers % e: Exponent of public key
  • 28.
    % % Outputs: % n:Modulus (length of keys) % d: Exponent of private key % HW 5-RSA/HW_5.pdf 1/3 HW 5 – RSA Encryption Objectives The objective of this homework is for you to learn about RSA encryption. You will code and test the RSA encryption algorithm. Additionally, a sub- objective is to gain further experience with the Matlab simulation environment. Files All the files (i.e., cover page, Matlab files, etc.) are contained in a folder called “HW 3” which is available for download on TITANium. You must download these files prior to completing the
  • 29.
    following tasks. Background RSA isone of the first practicable public-key cryptosystems and is widely used for secure data transmission. In such a cryptosystem, the encryption key is public and differs from the decryption key which is kept secret. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described the algorithm in 1977. Clifford Cocks, an English mathematician, had developed an equivalent system in 1973, but it wasn't declassified until 1997. A user of RSA creates and then publishes a public key based on the two large prime numbers, along with an auxiliary value. The prime numbers must be kept secret. Anyone can use the public key to encrypt a message, but with currently published methods, if the public key is large enough, only someone with knowledge of the prime factors can feasibly decode the message. Breaking RSA encryption is known as the RSA problem. It is an open question whether it is as hard as the factoring problem. Description Code the private key generation function (“genPrivKey.m”). The genPrivKey function should display the value of n, the public key (e), the value of the totient (�), and the private key (d) in the
  • 30.
    command window asshown in Figure 1. You will not need the genPrivKey function to decrypt/encrypt the messages. However, you still need to code this function. Next, you will code the RSA algorithm (“rsa.m”). Then provide a script (“rsa_script.m”) that will complete the work tasks below. I provided code that will convert from string to ASCII and vice versa (i.e., “ascii2str.m” and “str2ascii.m”. I also provided code to perform the modulus (see “bigmod.m”). See the “RSA Overview” lecture for a description of the algorithm. 2/3 Figure 1: genPrivKey example using p=11, q=13, and e=7. Figure 2: Example decryption of the cipher for “Hello World” using p=11, q=13, and d=103. 3/3 Figure 3: Example encryption of the plain text “Hello World” using p=19, q=17, and e =7. Work Task
  • 31.
    Part 1 -Decrypt Given the ciphers, decrypt the messages using p=11, q=13, and d=103: 1. 89,59,33,98,67,40,39,98,69,62,59,29,98,77,108,110 2. 124,45,45,100,98,35,40,66,98,20,24 Part 2 - Encrypt Encrypt the following messages using p=19, q=17, and e=7 (case sensitive): 1. the rain in spain falls mainly on the plain 2. Supercalifragilisticexpialidocious Part 3 - Private Key Generation Generate the private key given p=19, q=17, and e=7. What to Turn In (Please read this carefully) For this HW, you only need to provide the Matlab code and the answers for the “Work Task” section. You must put the complete Matlab functions and the answers into a single PDF. Your code must be in text format. Code provided as an image will not be accepted. You must label everything appropriately (i.e., label the code and work task sections). If I can’t understand your answers or code, I will assume it is incorrect. Also, please include the cover page in your PDF document. This HW will be a digital submission and it will be submitted online using TITANium. No paper submissions will be accepted.
  • 32.
    HW 5-RSA/rsa.m function mc= rsa(M,N,key) % function mc = rsa(M,N,key) % % RSA Encryption/Decryption % % Inputs: % M: Input ASCII plaintext/ciphertext % N: Modulus (length of keys) % key: Public or private key exponent (e or d) % % Output: % mc: Output ASCII plaintext/ciphertext % HW 5-RSA/rsa_script.m
  • 33.
    HW 5-RSA/str2ascii.m function ascii= str2ascii(s) % Convert to ASCII ascii = double(s);