Asked by Mohammed alfath Ahmed
on 9 Sep 2019

The error appears in this line(fprintf('\t%d',c1(o)))

Answer by Adam Danz
on 9 Sep 2019

Edited by Adam Danz
on 10 Sep 2019

Accepted Answer

The code that contains the problem is copied below from your pdf attachment. The error happens when 'o' becomes larger than the number of elements in 'c1'.

while(o<=over)

fprintf('\t%d',c1(o));

o=o+1;

end

The while-loop that comes before this only has 1 iteration because of the break at the end! Without the break, the while loop will never end because 'o' will always be 1 since you are not increasing that value within the loop.

Solution

This is how your last 2 while-loops should appear. The arrows show where I corrected your code.

while(o<=over);

m=m1(o);

diff=0;

if(m>n);

diff=m-n+1;

end

m=m-diff;

qm=dec2bin(e);

len=length(qm);

c=1;

xz=1;

while(xz<=len)

if(qm(xz)=='1')

c=mod(mod((c^2),n)*m,n);

elseif(qm(xz)=='0')

c=(mod(c^2,n));

end

xz=xz+1;

end

c1(o)=c;

o = o+1; % <----------------- here (remove the break)

end

o=1;

fprintf('\nThe encrypted message is \n');

while(o<=over)

fprintf('\t%d',c1(o));

o=o+1;

end

fprintf('\n') % <-------------------- here (add this)

Adam Danz
on 10 Sep 2019

Glad I could help.

" I need to perform decryption"

do you mean you need to decrypt the encrypted message (ie, perform the reverse of the encryption)?

Mohammed alfath Ahmed
on 10 Sep 2019

Yes

Adam Danz
on 10 Sep 2019

randint() was part of the Communication System Toolbox and is now replaced with randi(). You'll see the use of randi() in places of these two lines in KALYAN 's answer.

n1=randint(1,1,n);

e=randint(1,1,[2 n1]);

These function produce randome integers. Random number generation cannot be reverse-engineered. You'll need to save the random numbers somewhere.

I haven't looked too deeply into your algrithm so I can't say whether the rest is deterministic or not. However, before you get into dycription, I'd check that your encryption is doing what it's supposed to do.

My answer gets rid of your errors but I doubt the the algorithm is correct. For example, when I enter the inputs 7, 3, and "secret message" (without quotes), the encryption is just all 20s (I confirmed with c1==20). You can be certain that this encyption cannot be decrypted and carries no meaning.

The encrypted message is

20 20 20 20 20 20 20 20 20 20 20 20 20 20

KALYAN 's answer produces a different result but again, I doubt that's the correct algorithm since the 'break' only permits 1 iteration of the while(o<=over)-loop.

Sign in to comment.

Answer by KALYAN ACHARJYA
on 9 Sep 2019

Edited by KALYAN ACHARJYA
on 9 Sep 2019

In your example PDF C1 is

>> whos c1

Name Size Bytes Class Attributes

c1 1x1 8 double

you can do c1(1) only

>> c1(1)

ans =

107

>> c1(2)

Index exceeds matrix dimensions.

You are trying to access the vector elements more than its length, hence Index exceeds matrix dimensions.while loop continue o=1,2,3... In c1(o) but you have c1(1) only, not c1(2),c1(3)...hence code shows the Index exceeds matrix dimensions

Is It:

p=input('Enter the prime no. for p: ');

q=input('Enter the prime no. for q: ');

n=p*q;

fprintf('\nn=%d',n);

phi=(p-1)*(q-1);

fprintf('\nphi(%d) is %d',n,phi);

val=0;

cd=0;

while(cd~=1||val==0)

n1=randi(n);

e=randi([2,n],1,1)

val=isprime(e);

cd=gcd(e,phi);

end

val1=0;

d=0;

while(val1~=1);

d=d+1;

val1=mod(d*e,phi);

end

fprintf('\nd=%d',d);

fprintf('\nPublic key is (%d,%d)',e,n);

fprintf('\nPrivate key is (%d,%d)',d,n);

m=input('\nEnter the message: ','s');

m1=m-0;

disp('ASCII equivalent of message ');

disp(m1);

over=length(m1);

o=1;

while(o<=over);

m=m1(o);

diff=0;

if(m>n);

diff=m-n+1;

end

m=m-diff;

qm=dec2bin(e);

len=length(qm);

c=1;

xz=1;

while(xz<=len)

if(qm(xz)=='1')

c=mod(mod((c^2),n)*m,n);

elseif(qm(xz)=='0')

c=(mod(c^2,n));

end

xz=xz+1;

end

c1(o)=c;

break

end

fprintf('\nThe encrypted message is \n');

fprintf('\t%d',c1);

Sample Results: % I have removed the while loop, as c1 having length 1 only, hence no role to access the c1(2), c(3)..invalid in the case.

Enter the prime no. for p: 9

Enter the prime no. for q: 13

n=117

phi(117) is 96

e =

12

e =

89

d=41

Public key is (89,117)

Private key is (41,117)

Enter the message: hello

ASCII equivalent of message

104 101 108 108 111

The encrypted message is

65>>

Mohammed alfath Ahmed
on 9 Sep 2019

But I want to print the whole encrypted message

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.