Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Cipher

Reply
Thread Tools

Cipher

 
 
Max Zhou
Guest
Posts: n/a
 
      03-17-2008
[Note: parts of this message were removed to make it a legal post.]

I am trying to make a program that takes what you input, and encrypts it by turning a to p, b to q, z to b, etc. It moves the letter 2 spaces to the left (a to c) and moves it down (c to p). (See below.) Even though you can just say that a will be n, how do you seperate each letter and turn it into a string? Please put it in terms that a Ruby beginner would understand.
abcdefghijklm
nopqrstuvwxyz


---------------------------------
Never miss a thing. Make Yahoo your homepage.
 
Reply With Quote
 
 
 
 
Thomas Wieczorek
Guest
Posts: n/a
 
      03-17-2008
On Mon, Mar 17, 2008 at 1:50 AM, Max Zhou <> wrote:
> I am trying to make a program that takes what you input, and encrypts it by turning a to p, b to q, z to b, etc. It moves the letter 2 spaces to the left (a to c) and moves it down (c to p). (See below.) Even though you can just say that a will be n, how do you seperate each letter and turn it into a string? Please put it in terms that a Ruby beginner would understand.
> abcdefghijklm
> nopqrstuvwxyz
>


You can split a string with the String#split method:
irb
> "Foobar".split(//)

=> ["F", "o", "o", "b", "a", "r"]

You can get the ASCII value of a character either with ?character:
> ?F

=> 70

If you want to loop the splitted string, try eval(has anyone a better
solution?):
> "Foobar".split(//).each { |c| puts eval("?#{c}") }


You can use Fixnum#chr to turn an ASCII value in a character:
> 65.chr #=> "A"


You're trying to implement the ROT13 or ROTx algorithm. A naive
approach, still buggy might look like that:
plaintext = "Hello"
encrypted = ""
plaintext.split(//).each { |c| i=eval("?#{c}"); encrypted << (i+13).chr }
puts encrypted #=> Uryy|

As you see, the encrypted string contains a |(vertical line) and it
also chokes on spaces. I leave the rest to you. Ask again if you're
stuck.

Regards, Thomas

 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      03-17-2008
On Sun, Mar 16, 2008 at 7:50 PM, Max Zhou <> wrote:
> I am trying to make a program that takes what you input, and encrypts it by turning a to p, b to q, z to b, etc. It moves the letter 2 spaces to the left (a to c) and moves it down (c to p). (See below.) Even though you can just say that a will be n, how do you seperate each letter and turn it into a string? Please put it in terms that a Ruby beginner would understand.
> abcdefghijklm
> nopqrstuvwxyz
>
>
>
> ---------------------------------
> Never miss a thing. Make Yahoo your homepage.


There are many ways to do it, but if it was all lower case, I would probably...

a = "abcdefghijklmnopqrstuvwxyz"
ascii_offset = 15 - 97
str = "antidisestablishmentarianism"
str.each_byte {|b| new_str << a[(b + ascii_offset) % a.size]}
puts new_str

The 15 is to turn a into a p (?p - ?a == 15). The minus 97 part is to
make sure we work in the ascii byte code range. each_byte runs
through each byte of the string, where, in the block, the offset is
added and the mod by the size of a (26) to keep the index of a within
the correct bounds. a[] selects a byte to be appended (<<) to
new_str.

You could wrap this in a method or make it part of String class.
Ideally, I think a person would really want to include the full 256
character set to deal with punctuation and spaces.

Todd

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      03-17-2008
On Sun, Mar 16, 2008 at 9:00 PM, Todd Benson <> wrote:
>
> On Sun, Mar 16, 2008 at 7:50 PM, Max Zhou <> wrote:
> > I am trying to make a program that takes what you input, and encrypts it by turning a to p, b to q, z to b, etc. It moves the letter 2 spaces to the left (a to c) and moves it down (c to p). (See below.) Even though you can just say that a will be n, how do you seperate each letter and turn it into a string? Please put it in terms that a Ruby beginner would understand.


Also, I forgot to mention that you should realize that a String object
should be thought of as really just an ordering of bytes with some
special methods to make it human readable.

Todd

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      03-17-2008
On Sun, Mar 16, 2008 at 9:00 PM, Todd Benson <> wrote:
>
> On Sun, Mar 16, 2008 at 7:50 PM, Max Zhou <> wrote:
> > I am trying to make a program that takes what you input, and encrypts it by turning a to p, b to q, z to b, etc. It moves the letter 2 spaces to the left (a to c) and moves it down (c to p). (See below.) Even though you can just say that a will be n, how do you seperate each letter and turn it into a string? Please put it in terms that a Ruby beginner would understand.
> > abcdefghijklm
> > nopqrstuvwxyz
> >
> >
> >
> > ---------------------------------


Just for fun, I revisited this. To include the printable characters
on an english-based system (ASCII codes 32 through 126), not including
tab, return, line-feed, etc...


puts str = "Hello, world! Yabba dabba doo!"
puts

char_set = (32..126).map.pack('c*')
start, finish, offset = ?a, ?p, ?p - ?a
puts "char_set:\n" + char_set = (?\s..?~).map.pack('c*')
puts

size = char_set.size
new_str = ""
str.each_byte do |byte|
new_str << char_set[(byte + offset - char_set[0]) % size]
end
puts "enciphered: \n" + new_str


Todd

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      03-17-2008
On Mon, Mar 17, 2008 at 5:21 AM, Todd Benson <> wrote:
> On Sun, Mar 16, 2008 at 9:00 PM, Todd Benson <> wrote:
> >

>
> > On Sun, Mar 16, 2008 at 7:50 PM, Max Zhou <> wrote:
> > > I am trying to make a program that takes what you input, and encrypts it by turning a to p, b to q, z to b, etc. It moves the letter 2 spaces to the left (a to c) and moves it down (c to p). (See below.) Even though you can just say that a will be n, how do you seperate each letter and turn it into a string? Please put it in terms that a Ruby beginner would understand.
> > > abcdefghijklm
> > > nopqrstuvwxyz
> > >
> > >
> > >
> > > ---------------------------------

>
> Just for fun, I revisited this. To include the printable characters
> on an english-based system (ASCII codes 32 through 126), not including
> tab, return, line-feed, etc...
>
>
> puts str = "Hello, world! Yabba dabba doo!"
> puts
>
> char_set = (32..126).map.pack('c*')
> start, finish, offset = ?a, ?p, ?p - ?a
> puts "char_set:\n" + char_set = (?\s..?~).map.pack('c*')
> puts
>
> size = char_set.size
> new_str = ""
> str.each_byte do |byte|
> new_str << char_set[(byte + offset - char_set[0]) % size]
> end
> puts "enciphered: \n" + new_str


Dang it! Another thing that unit tests won't catch: code that is
redundant! Removing the first "char_set =" line will have the same
result...

puts str = "Hello, world! Yabba dabba doo!"
puts

start, finish, offset = ?a, ?p, ?p - ?a
puts "char_set:\n" + char_set = (?\s..?~).map.pack('c*')
puts

size = char_set.size
new_str = ""
str.each_byte do |byte|
new_str << char_set[(byte + offset - char_set[0]) % size]
end
puts "enciphered: \n" + new_str


Todd

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      03-17-2008
On Sun, Mar 16, 2008 at 9:00 PM, Todd Benson <> wrote:

> a = "abcdefghijklmnopqrstuvwxyz"
> ascii_offset = 15 - 97

new_str = ""
> str = "antidisestablishmentarianism"
> str.each_byte {|b| new_str << a[(b + ascii_offset) % a.size]}
> puts new_str


I think I'm going to drop google mail; that, or come up with a more
clear way to cut and paste :/

Todd

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to set cipher-suite in commons httpclient the_caspie@hotmail.com Java 2 08-29-2005 12:26 PM
128bit cipher strenght emcryption Zoli Computer Support 2 03-18-2005 07:38 AM
Cipher jpeg stream terry Java 1 08-01-2004 05:05 AM
Key generation algorithm and Cipher algorithm Ahmed Moustafa Java 0 11-15-2003 06:35 AM
Cipher.unwrap Gary Java 0 08-08-2003 03:24 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57