i cant think A blog belong to Mohammad Azwan bin Ali. Most of his writings are about his real life story, ideas, thoughts, moderate political views, computer, programming, tips & tricks, particularly in Linux…. and of course it includes some random unspecified crappy stuffs too! Don't forget to check Movies and Junkyard pages out!

straddling checkerboard with ruby

this week i sharpen my ruby saw with an excessive about straddling checkerboard encryption.

In cryptography, a straddling checkerboard is a device for converting an alphabetic  plaintext  into digits whilst simultaneously achieving fractionation (a simple form of information diffusion) and homophony (a simple method for suppressing peaks of the frequency distribution). It also is known as a monome-dinome cipher. -- wikipedia

after do some reading on how the operation was, i've workout the algorithm.
as i want to learn more about ruby but don't know what i can do with it, so i decided to write this in ruby
[melt][melt]

here's the solution

  1. class Straddling
  2. def initialize( key_text, n1, n2, n3 )
  3.    key_text = key_text+"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  4.    key_text = key_text.upcase.split(//).uniq
  5.    @key_text = key_text
  6.    @n1 = n1;@n2 = n2;@n3 = n3
  7. end
  8.  
  9. def create
  10.    @board = Array.new()
  11.    k = 0;prev = ""
  12.    for a in 0..40 do
  13.        if (a == @n1 || a == @n2 || a == @n3)
  14.          @board[a] = "_"
  15.        else
  16.          if ("A".."J") === prev
  17.  
  18.           @board[a] = prev == "J"? 0:((prev)[0]-16).chr
  19.          else
  20.           @board[a] = @key_text[k] == " " ? "*" : @key_text[k]
  21.           k+=1
  22.          end
  23.          prev = @board[a]
  24.        end
  25.     end
  26.    return @board.to_s
  27. end
  28.  
  29. def tp(key,text)
  30. @key = key.split(//)
  31. @text = text.gsub(' ','*').upcase.split(//)
  32. end
  33.  
  34. def keygen
  35. @keygen = Array.new
  36. @checker_board = @board.to_s.split(//)
  37. for a in 0..@text.length-1 do
  38.         found = @checker_board.index(@text[a])
  39.         if (0..9) === found
  40.         @keygen.push(found)
  41.         elsif (10..19) === found
  42.         @keygen.push(@n1,found%10)
  43.         elsif (20..29) === found
  44.         @keygen.push(@n2,found%10)
  45.         else
  46.         @keygen.push(@n3,found%10)
  47.         end
  48. end
  49. end
  50.  
  51. def e(key,text)
  52. self.tp(key,text)
  53. self.keygen
  54. print "#{@keygen.to_s}\n"
  55. t = Array.new
  56. @keygen.each do |value|
  57. t.push((@key[0].to_i+value.to_i)%10)
  58. @key << @key.shift
  59. end
  60. print "#{@t.to_s}\n"
  61. print "Encrypt=#{self.show(t)}\n"
  62. end
  63.  
  64. def d(key,text)
  65. self.tp(key,text)
  66. self.keygen
  67. t = Array.new
  68. @keygen.each do |value|
  69. t.push((value+10-(@key[0]).to_i)%10)
  70. @key << @key.shift
  71. end
  72. print "Decrypt=#{self.show(t).gsub('*',' ')}\n"
  73. end
  74.  
  75. def show(t)
  76. a = 0
  77. result = ""
  78. while a < t.length
  79. unless t[a] == @n1 || t[a] == @n2 || t[a] == @n3
  80. result = result + @checker_board[t[a]]
  81. a+=1
  82. else
  83. result = result + @checker_board[t[a+1]==nil ?
  84. t[a]:(t[a]==@n1 ?1:(t[a]==@n2) ?2:3)*10+t[a+1]]
  85. a+=2
  86. end
  87. end
  88. return result
  89. end
  90.  
  91. end
  92. board = Straddling.new("sharpen your saw",2,5,9)
  93. puts(board.create)
  94. board.e("2641","programming praxis")
  95. board.d("2641","S811R53S87A18RUAS8PSSH5")
  96. puts("\n")
  97. board.e("2641","encrypt this text")
  98. board.d("2641","A118R1H81A8RALR8H5ALRA180_")
  99. puts("\n\n")
  100. board = Straddling.new("my secret keys",2,5,9)
  101. puts(board.create)
  102. board.e("241","ruby programming rocks")
  103. board.d("241","SF5E5*5MIREESEACY45YIS5****MESE")

code preview
fix: bug where it didn't check for null value after last digit
note that this code use 3 space but of course you can modify it and chose different space if you like

[jamur]


2 Comments

I'm glad you enjoyed the exercise.

Perhaps you would like to post your solution in the comments at Programming Praxis so other programmers can learn from it.

Phil

Posted by Phil on 31 January 2010 @ 9pm

thanks phil
i've already submitted the solution there

[thumb]

Posted by azwan ali on 1 February 2010 @ 6pm

Leave a Comment

:D [angely] [argh] [blindfold] [color] [cry] [bye] [cutie] [dance] [flirt] [ghost] [thumb] >_< hmmm more »