summaryrefslogtreecommitdiff
path: root/3544/CH4/EX4.5/Ex4_5.sce
blob: c588dc7047806728dd18c43eaeb653b9ff270b78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// RSA Encryption scheme

p = 7                      //Large prime numbers
q = 17                     //Small values taken here for convenience of calculation
                           //and explanation

n = p*q
z = (p-1)*(q-1)

e = 5                              // e<n and e & z are coprime 

i=1
d = i                       
while(1==1)                        //Calcualtion of 'd' from e and z
    if modulo(i*e,z)==1 then       // (e*d)mod z = 1
        d=i
        break
    end
    i=i+1
end

printf("\nPublic Key: (%d,%d)\nPrivate Key: (%d,%d)\n\n",n,e,n,d)

PT = 10                            //Example plaintext

printf("Plaintext: %d\n",PT)

C = modulo(PT^e,n)
printf("\nEncrypted Text Code: %d\n\n",C)

PT=1
for i = 1:d
    PT = modulo(PT*C,n)
end

printf("Decrypted Text: %d\n",PT)       //Conversion to plain text in ASCII