0

So I have decided to learn Sage programming since it can handle very large numbers which is useful for RSA encrypting/decrypting.

(1) I was following an example but I am not quite sure how they got 100 inside the ZZ() function.

(2) Also another question is there a way to go from integer to plain text using a sage function?

sage: m = "HELLOWORLD"
sage: m = map(ord, m); m
[72, 69, 76, 76, 79, 87, 79, 82, 76, 68]
sage: m = ZZ(list(reversed(m)), 100) ; m           <------ this line
72697676798779827668


sage: m = 72697676798779827668
sage: c = 630913632577520058415521090
sage: d = 4460824882019967172592779313
sage: n = 4951760154835678088235319297
sage: power_mod(c, d, n)
72697676798779827668                <--- how do i convert this number back to plain text
sage: power_mod(c, d, n) == m
True
jaymeister
  • 53
  • 7

1 Answers1

2

The 100 tells you how much to multiply each element of the list by, in powers. Think of it as "base 100".

sage: ZZ([1,2,3],100)
30201
sage: ZZ([1,2,3],2)
17
sage: ZZ([1,2,3],10) # 1*10^0+2*10^1+3*10^2
321

This question has a zillion ways to go backwards from ord. Then we use chr.

sage: a = 72697676798779827668
sage: ''.join([chr(int(str(a)[i:i+2])) for i in range(0, len(str(a)), 2)])
'HELLOWORLD'

I agree that this is not ideal in readability. Actually, Sage has some other built-in ways to do crypto on a pedagogical basis in its crypto module. That has some built-in stuff for alphabets as well. (I presume that this is not an industrial-strength version of RSA you are currently creating.)

Community
  • 1
  • 1
kcrisman
  • 4,374
  • 20
  • 41
  • how do i determine what base i should multiply by? for my problem my m is very large, my m is 350 characters long which turns into 2x350 = 700 integers long, I have tried to use base 100 but it does not spit out the correct numbers – jaymeister Oct 04 '12 at 04:50
  • I think they just did 100 so as to easily have enough room for the 26 letters of the alphabet. Remember that this is a toy example. That said, 100 should work fine, just because one can decompose the string neatly; in general you'd have to do something more complicated to get the letters back. – kcrisman Oct 04 '12 at 16:44