why do I get NaN result

8 visualizzazioni (ultimi 30 giorni)
Nazli Demir
Nazli Demir il 7 Giu 2020
Commentato: Nazli Demir il 8 Giu 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 Commenti
Andreas Bernatzky
Andreas Bernatzky il 7 Giu 2020
Because you get an overflow with plannumbers^e which is "inf".
I do not know the rsa algorithm well enough but a simple solution could be choose a value from e in a much smaller range something between 10 and 100 for example. But as I say I am not an expert for crypthography.
Nazli Demir
Nazli Demir il 7 Giu 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.

Accedi per commentare.

Risposte (1)

Andreas Bernatzky
Andreas Bernatzky il 8 Giu 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

Categorie

Scopri di più su Multicore Processor Targets in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by