# Modular arithmetic for encryption

12 ビュー (過去 30 日間)
Fatma Alnabrisi 2019 年 12 月 1 日
コメント済み: Fatma Alnabrisi 2019 年 12 月 2 日
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 件のコメント

Fatma Alnabrisi 2019 年 12 月 2 日
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 ??

サインイン to comment.

### 採用された回答

Bruno Luong 2019 年 12 月 1 日

"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 件のコメント

Fatma Alnabrisi 2019 年 12 月 1 日
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 2019 年 12 月 1 日
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 2019 年 12 月 1 日
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

サインイン to comment.

### その他の回答 (0 件)

サインイン してこの質問に回答します。

Translated by