why do I get NaN result

8 views (last 30 days)
Nazli Demir
Nazli Demir on 7 Jun 2020
Commented: Nazli Demir on 8 Jun 2020
I am trying to code a RSA encryption and I either get a Nan result where I should get a number or Matlab doesn't create that variable at all. Can somebody help me? The code is below:
fid = fopen('input.txt', 'r'); % We have to first open the file 'Input.txt'
plain = fscanf(fid, '%s', [5,1]); % We write the 5 letter word in the file as a 5x1 matrix
fclose(fid); % We close the file
plainascii = double(plain); % Now we have the numeric values of the letters depending on ASCII code
% We reduce the numbers in such a way that A=0, B=1, ... Z=25.
for i = 1:5
plainmod(i) = plainascii(i) - 65;
end
% We add the numbers in Z_26 and make it ready for encyption
plainnumbers = 26^4*plainmod(1) + 26^3*plainmod(2) + 26^2*plainmod(3) + 26^1*plainmod(2) + 26^0*plainmod(1);
% Now, we move on to the key. We select two prime numbers between 100.000
% and 1.000.000.
p = 459817;
q = 459817;
n = p*q;
% We need to have a key set (n, e). To find e, we ask the user for a number.
% We test that gcd(e, phi(n)) = 1. We define phi(n) as phin.
phin = n - p - q + 1;
e = input('Please provide a number between 100.000 to 1.000.000 to use as key:\n');
if gcd(e, phin) == 1
continue
else
e = input('This number is not valid. Please pick another:\n');
end
ciphernumbers = mod(plainnumbers^e,n);
display(ciphernumbers);
  2 Comments
Nazli Demir
Nazli Demir on 7 Jun 2020
I was wondering if that was it, but they told us to choose p and q large. But, now that you mention it, they didn't say anything about e. I will try with a different value for e. Thank you.

Sign in to comment.

Answers (1)

Andreas Bernatzky
Andreas Bernatzky on 8 Jun 2020
Hey Nazil,
I can just remember the RSA really roughly but I did some researches yesterday evening and I read at several points to choose p and q large. But I did just some quick researches on it.
But fact u get an INF for plannumbers^e and that is the problem.
Also you could use this for allowing extra high numbers:
Greets Andreas
  1 Comment
Nazli Demir
Nazli Demir on 8 Jun 2020
Thank you for this Andreas!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by