Ruby RSA Algorithm works - mostly

Page 1 of 1

2 Replies - 2926 Views - Last Post: 13 February 2012 - 10:28 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=266464&amp;s=68c26c17e6cad4380a3cd7f494c08c06&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 muricula

Reputation: 3
• Posts: 35
• Joined: 24-November 11

Ruby RSA Algorithm works - mostly

Posted 13 February 2012 - 12:10 AM

I am implementing my own RSA algorithm in ruby to learn more about the algorithm. It is almost working, but when I decrypt it a few numbers don't decrypt but most do. Why would this be?

For the given plaintext: [ 42, 24, 424, 224, 421, 321]

The ciphertext is: [1239,1263,1495,1349,208,1878]

Which when decrypted is: [42,690,424,779,421,321]

This is the problem. Why is it happening?

These values are used to produce the keys (The method call is at the end of the program)

p = 51
q = 37
e = 223
n = 1887
phiN = 1800 (coprime with d)
d = 1687

```class RSA
#edited to be concise, such as omitting initialize()

def encrypt(plainText)
index = 0
puts 'encrypt in is ', plainText
plainText.each do |i|
plainText[index] = ((i**(@e)) % @n )
index+=1
end
puts  'ciphertext is '
puts plainText
return plainText
end

def decrypt(cipherText)
puts 'decrypt in is ', cipherText
index = 0
cipherText.each do |i|
cipherText[index] = ((i**(@d)) % @n )
index+=1
end
puts 'plaintext is '
puts cipherText
return cipherText
end

def calcD()
@d=1
begin
s = (@d*@e)% @phiN;
@d+=1
end while not s==1
@d -= 1
#puts 'd is ', @d
end

end # class

message = RSA.new(51,37,223,[ 42, 24, 424, 224, 421, 321])

```

In the past I have been pointed here as a better place to ask cryptography related questions than just the general language forums. If this is the wrong place, please feel free to move it.

Is This A Good Question/Topic? 1

Replies To: Ruby RSA Algorithm works - mostly

#2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12271
• Posts: 45,363
• Joined: 27-December 08

Re: Ruby RSA Algorithm works - mostly

Posted 13 February 2012 - 06:38 AM

Moved to Ruby. Software Development is a good forum for higher-level discussions. For specific code samples, better post in the language specific forum.

#3 Karel-Lodewijk

Reputation: 454
• Posts: 864
• Joined: 17-March 11

Re: Ruby RSA Algorithm works - mostly

Posted 13 February 2012 - 10:28 AM

Assuming you are following http://en.wikipedia....%28algorithm%29, you switched d and e

@p = 51
@q = 37
@n = 1887
@phiN = 800
@e = 1687
@d = 223

Spoiler

This post has been edited by Karel-Lodewijk: 13 February 2012 - 10:37 AM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }