MATLAB Answers

Modular arithmetic for encryption

10 views (last 30 days)
I'm trying to implement an encryption technique using Matlab for a college project.
However I'm facing some difficulties in modular calculation.
For example I have matrix A = [ 1 , 0 ; 1/2 , 1 ] , and I need to calculate A mod 29.
what I'm getting in matlab is [ 1 , 0 ; 1/2 ,1 ],
while what I'm supposed to get is [ 1 , 0 ; 15 , 1 ] as in https://planetcalc.com/8326/
Actually I don't understand the math behined the fact that (1/2 mod 29) = 15 .
So, if any one can help me in getting this result in Matlab, I would be grateful.

  1 Comment

Fatma Alnabrisi
Fatma Alnabrisi on 2 Dec 2019
Can someone please help me in finding modular of fractions.
My input is real number matrix A and I need to get (A mod 29)
The output should be integer matrix.
How to code this function in Matlab ??

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 1 Dec 2019
Edited: Bruno Luong on 1 Dec 2019
"1/2" is integer (let us call it "a") so that
a*2 = 1 mod 29
This can be obtained by GCD function
>> [~,a]=gcd(2,29)
a =
-14
>> a = mod(a,29)
a =
15

  3 Comments

Fatma Alnabrisi
Fatma Alnabrisi on 1 Dec 2019
Thank you very much for your answer.
But actually I didn't get it 100% ..
what if I need to calculate 3/2 mod 29 .. I'm getting wrong answer !
>> b = 3/2
b =
1.5000
>> [~,b]=gcd(2,29)
b =
-14
>> b = mod (b,29)
b =
15
Bruno Luong
Bruno Luong on 1 Dec 2019
You should wonder where 3 is used in your code (no where).
Here is the result
b = 3/2 = 3*(1/2) = 3*a = 3*15 = 45 = 16 mod 29
Fatma Alnabrisi
Fatma Alnabrisi on 1 Dec 2019
well, based on what you've mentioned can you please help me in writing a code which takes any matrix A as an input, and finds A mod 29 (considering that the elements of A are real numbers and the outputs have to be integers.)
Thank you

Sign in to comment.

More Answers (0)

Sign in to answer this question.