Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:49:24+00:00 2026-06-01T23:49:24+00:00

I wrote up the ruby script below to help my students understand public key

  • 0

I wrote up the ruby script below to help my students understand public key encryption. I followed the “pencil and paper” method shown here: http://sergematovic.tripod.com/rsa1.html

This works fine as long as 29 isn’t chosen as either p or q. If 29 is picked, it hangs on calculating the secret key. Can anyone tell me why that is?

#!/usr/bin/env ruby -wKU

#initialize
primes, p, q, n, z, k, j, m,e,d = nil

def prime
  primes = [2,3,5,7,11,13,17,19,23,29,31]
  primes.sample
end

#pick p
p= prime
puts "p: " + p.to_s

#pick q
q=p
while p==q
  q = prime
end
puts "q: " + q.to_s

#find n
n=p*q
puts "n: " + n.to_s

#find z
z=(p-1)*(q-1)
puts "z: " + z.to_s

#pick a relative prime of the totient
k=7

puts "k: " + k.to_s

#calculate secret key
j=0
while j*k % z != 1
  j+=1
end

puts "j: " + j.to_s
#message
m=16
puts "Message: " + m.to_s

#encrypt
e = m**k % n
puts "Encrypted: " + e.to_s

#decrypt
d = e**j % n
puts "Decrypted: " + d.to_s
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:49:25+00:00Added an answer on June 1, 2026 at 11:49 pm

    When 29 is chosen as p or q, z has 28 as a factor, and thus k = 7 is not a relative prime of the totient as your comment claims!

    (This means j*k % z is always a multiple of 7, so your loop never terminates.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a small ruby script that generates an array like the one below:
A couple of weeks ago, I wrote a simple Ruby script to test a
I wrote a script using Ruby to click on the screen automatically on Windows's
I wrote a short Ruby script to profile MongoDB, just to see how its
I wrote this little test script in Ruby (on MacOS): #!/usr/bin/ruby require 'digest/sha2' def
I wrote a ruby script to process a large amount of documents and use
I wrote a Ruby script that I want to run in the background repeatedly
I wrote a script in Ruby. I'd like to run it every day at
I need to write some ruby script which will help me with big image
I wrote a little ruby script, which connects itself to a mysql database and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.