# A simple RSA

**URL:** <https://www.ruby-forum.com/t/a-simple-rsa/93993>\
**Category:** Ruby\
**Created:** [April 17, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/a-simple-rsa/93993 "2006-04-17T20:05:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shannon\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a698b9/32.png) [@Shannon\_F](https://www.ruby-forum.com/u/Shannon_F)\
**Post date:** [April 17, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/a-simple-rsa/93993/1 "2006-04-17T20:05:06Z")

</div>

Hi All,

I tried to implement a simple RSA crypto for my work. It seems all works  
fine, except that I am not satisfied with the code I wrote below:

def str\_to\_num(s)  
h = ‘0x’  
(0…s.length).each do |i|  
h += “%02x” % s[i]  
end  
eval(h)  
end

def num\_to\_str(n)  
s = ‘’  
while n \> 0 do  
s = “%c” % (n & 0xFF) + s  
n = n \>\> 8  
end  
s  
end

These 2 functions convert string to bignum and vise-versa. It works, but  
I  
feel there should be better (faster) way to do this.

Could anyone point the way out?

Thanks!  
Shannon

---

<div class="post-metadata">

**Author:** ![Shannon\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a698b9/32.png) [@Shannon\_F](https://www.ruby-forum.com/u/Shannon_F)\
**Post date:** [April 17, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/a-simple-rsa/93993/2 "2006-04-17T23:20:40Z")

</div>

Please see attached:

- samples with unpack/inject and each\_byte use in str\_to\_num;  
each\_byte is faster;
- num\_to\_str without use of formatting;
- unit test stuff and
- benchmark code

hope these help  
regards  
Sergey

----- Original Message -----

---

<div class="post-metadata">

**Author:** ![Shannon\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a698b9/32.png) [@Shannon\_F](https://www.ruby-forum.com/u/Shannon_F)\
**Post date:** [April 18, 2006, 5:21am UTC](https://www.ruby-forum.com/t/a-simple-rsa/93993/3 "2006-04-18T05:21:28Z")

</div>

Thanks a lot, this works fine.

BTW, another related question: while I ruby code, I can write a long  
string  
like: (without using heredoc)

str = “this is very long line 1” +  
“this is line 2”

but if I write a number:

num = 0x1234132410987094750245…

And the number is very long (an RSA key), how can I wrap the line? it  
seems  
that I cannot use a “” by the end of line…

Thanks!  
Shannon

> - unit test stuff and
> 
> > I tried to implement a simple RSA crypto for my work. It seems all works  
> > def num\_to\_str(n)
> > 
> > Could anyone point the way out?
> > 
> > Thanks!  
> > Shannon

---

<div class="post-metadata">

**Author:** ![Shannon\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a698b9/32.png) [@Shannon\_F](https://www.ruby-forum.com/u/Shannon_F)\
**Post date:** [April 18, 2006, 5:39am UTC](https://www.ruby-forum.com/t/a-simple-rsa/93993/4 "2006-04-18T05:39:43Z")

</div>

On Tue, 18 Apr 2006, Shannon F. wrote:

> num = 0x1234132410987094750245…
> 
> And the number is very long (an RSA key), how can I wrap the line? it seems  
> that I cannot use a “” by the end of line…
> 
> Thanks!  
> Shannon

harp:~ \> cat a.rb  
num = %w(  
1234132410987094750245  
1234132410987094750245  
1234132410987094750245  
).join.to\_i

p num

harp:~ \> ruby a.rb  
123413241098709475024512341324109870947502451234132410987094750245

-a
