2 Replies - 2276 Views - Last Post: 13 February 2012 - 10:28 AM Rate Topic: -----

#1 muricula  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • 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  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1