SlideShare a Scribd company logo
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);

More Related Content

PPT
Chapter 06 rsa cryptosystem
PPT
14-applications-of-number-theory.ppt
PDF
Week5 ap3421 2019_part1
PDF
Public-Key Cryptography.pdfWrite the result of the following operation with t...
PPT
The rsa algorithm
PPT
The Introduction to RSA Algorithm with numerical example
PPT
The rsa algorithm
PPT
The rsa algorithm
Chapter 06 rsa cryptosystem
14-applications-of-number-theory.ppt
Week5 ap3421 2019_part1
Public-Key Cryptography.pdfWrite the result of the following operation with t...
The rsa algorithm
The Introduction to RSA Algorithm with numerical example
The rsa algorithm
The rsa algorithm

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

PDF
IHE-Columbia-Theory- diploma Seminar.pdf
PPTX
Information and network security 33 rsa algorithm
PPTX
01 - DAA - PPT.pptx
PDF
Implementation of RSA Algorithm with Chinese Remainder Theorem for Modulus N ...
PDF
RSA ALGORITHM
PDF
[Slides] A simple (leveled) fully homomorphic encryption scheme and thoughts ...
PPT
ch09_rsa_nemo.ppt
PPT
The rsa algorithm JooSeok Song
PPTX
module 4 ppt on crypography and network security
PPTX
RSA-W7(rsa) d1-d2
PPTX
Best C++ Programming Homework Help
PDF
Classical programming interview questions
PDF
How To Crack RSA Netrek Binary Verification System
PPTX
RSA & MD5 algorithm
PDF
Everything I always wanted to know about crypto, but never thought I'd unders...
PDF
Fundamentals of Communication Systems 1st Edition Proakis Solutions Manual
PPTX
Iterations FOR LOOP AND WHILE LOOP .pptx
PDF
C++ in 10 Hours.pdf.pdf
DOC
C lab-programs
PDF
Parallel R in snow (english after 2nd slide)
IHE-Columbia-Theory- diploma Seminar.pdf
Information and network security 33 rsa algorithm
01 - DAA - PPT.pptx
Implementation of RSA Algorithm with Chinese Remainder Theorem for Modulus N ...
RSA ALGORITHM
[Slides] A simple (leveled) fully homomorphic encryption scheme and thoughts ...
ch09_rsa_nemo.ppt
The rsa algorithm JooSeok Song
module 4 ppt on crypography and network security
RSA-W7(rsa) d1-d2
Best C++ Programming Homework Help
Classical programming interview questions
How To Crack RSA Netrek Binary Verification System
RSA & MD5 algorithm
Everything I always wanted to know about crypto, but never thought I'd unders...
Fundamentals of Communication Systems 1st Edition Proakis Solutions Manual
Iterations FOR LOOP AND WHILE LOOP .pptx
C++ in 10 Hours.pdf.pdf
C lab-programs
Parallel R in snow (english after 2nd slide)
Ad

More from wellesleyterresa (20)

DOCX
Hw059f6dbf-250a-4d74-8f5e-f28f14227edc.jpg__MACOSXHw._059.docx
DOCX
HW in teams of 3 studentsAn oil remanufacturing company uses c.docx
DOCX
HW 5.docxAssignment 5 – Currency riskYou may do this assig.docx
DOCX
HW#3 – Spring 20181. Giulia is traveling from Italy to China. .docx
DOCX
HW 2Due July 1 by 500 PM.docx
DOCX
HW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docx
DOCX
HW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docx
DOCX
HW 3 Project Control• Status meeting agenda – shows time, date .docx
DOCX
HW 1January 19 2017Due back Jan 26, in class.1. (T.docx
DOCX
Hussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docx
DOCX
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
DOCX
HUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docx
DOCX
HW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docx
DOCX
HW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docx
DOCX
Hunters Son Dialogue Activity1. Please write 1-2 sentences for e.docx
DOCX
HW 2 - SQL The database you will use for this assignme.docx
DOCX
Humanities Commons Learning Goals1. Write about primary and seco.docx
DOCX
HURRICANE KATRINA A NATION STILL UNPREPARED .docx
DOCX
Humanities 115Short Essay Grading CriteriaExcellentPassing.docx
DOCX
HUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docx
Hw059f6dbf-250a-4d74-8f5e-f28f14227edc.jpg__MACOSXHw._059.docx
HW in teams of 3 studentsAn oil remanufacturing company uses c.docx
HW 5.docxAssignment 5 – Currency riskYou may do this assig.docx
HW#3 – Spring 20181. Giulia is traveling from Italy to China. .docx
HW 2Due July 1 by 500 PM.docx
HW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docx
HW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docx
HW 3 Project Control• Status meeting agenda – shows time, date .docx
HW 1January 19 2017Due back Jan 26, in class.1. (T.docx
Hussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docx
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
HUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docx
HW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docx
HW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docx
Hunters Son Dialogue Activity1. Please write 1-2 sentences for e.docx
HW 2 - SQL The database you will use for this assignme.docx
Humanities Commons Learning Goals1. Write about primary and seco.docx
HURRICANE KATRINA A NATION STILL UNPREPARED .docx
Humanities 115Short Essay Grading CriteriaExcellentPassing.docx
HUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docx
Ad

Recently uploaded (20)

PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Sports Quiz easy sports quiz sports quiz
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Sports Quiz easy sports quiz sports quiz
O5-L3 Freight Transport Ops (International) V1.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
TR - Agricultural Crops Production NC III.pdf
Pre independence Education in Inndia.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial diseases, their pathogenesis and prophylaxis
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Supply Chain Operations Speaking Notes -ICLT Program
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Types and Its function , kingdom of life
Renaissance Architecture: A Journey from Faith to Humanism
Final Presentation General Medicine 03-08-2024.pptx
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india

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 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
  • 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 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
  • 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 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
  • 12. 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
  • 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 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
  • 16. 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
  • 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 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
  • 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.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,.
  • 21. . 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
  • 22. 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
  • 24. A Real World Example 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 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
  • 26. 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
  • 27. – 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
  • 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 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
  • 30. 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
  • 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);