Advertisement
Narzew

Bites operations algorithms implementation

Dec 5th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.59 KB | None | 0 0
  1. #**Algorithms by Peter O.
  2. #**Converted to Ruby by Narzew
  3. #**Implemented by Narzew
  4.  
  5. #**Functions
  6.  
  7. module NRGSS
  8.     def self.swap32(x)
  9.         return (((x>>24)&0x000000FF)|((x>>8)&0x0000FF00)|((x<<8)&0x00FF0000)|((x<<24)&0xFF000000))
  10.     end
  11.     def self.swap8(x)
  12.         return (((x>>8)&0x00FF)|((x<<8)&0xFF00))
  13.     end
  14.     def self.rlc(x)
  15.         return ((((x)&0x7F)<<1)|(((x)&0x80)>>7))
  16.     end
  17.     def self.rrc(x)
  18.         return ((((x)&0xFE)>>1)|(((x)&0x01)<<7))
  19.     end
  20.     def self.swap(x)
  21.         return ((((x)&0xF0)>>4)|(((x)&0x0f)<<4))
  22.     end
  23.     def self.sla(x)
  24.         return (((x)<<1)&0xFE)
  25.     end
  26.     def self.sla16(x)
  27.         return (((x)<<1)&0xFFFE)
  28.     end
  29. end
  30.  
  31. #**Implementation - for example ;)
  32.  
  33. $val = 70
  34. 10.times{|x|
  35. puts "================="
  36. puts "===ROUND #{x+1} ==="
  37. puts "VALUE IS #{$val}"
  38. puts "================="
  39. $a = NRGSS.swap32($val)
  40. $b = NRGSS.swap8($val)
  41. $c = NRGSS.swap32($a)
  42. $d = NRGSS.swap8($b)
  43. $e = NRGSS.rlc($val)
  44. $f = NRGSS.rrc($e)
  45. $g = NRGSS.swap($val)
  46. $h = NRGSS.swap($g)
  47. $i = NRGSS.sla($val)
  48. $j = NRGSS.sla($i)
  49. $k = NRGSS.sla16($val)
  50. $l = NRGSS.sla16($k)
  51. puts ("Swap 32 encrypt result #{$a}")
  52. puts ("Swap 8 encrypt result #{$b}")
  53. puts ("Swap 32 decrypt result #{$c}")
  54. puts ("Swap 8 decrypt result #{$d}")
  55. puts ("Rlc conversion result #{$e}")
  56. puts ("Rrc conversion result #{$f}")
  57. puts ("Swap encrypt result #{$g}")
  58. puts ("Swap decrypt result #{$h}")
  59. puts ("Sla transform 1 result #{$i}")
  60. puts ("Sla transform 2 result #{$j}")
  61. puts ("Sla16 transfrom 1 result #{$k}")
  62. puts ("Sla16 transform 2 result #{$l}")
  63. $val = (($val*7+3+rand(999)&0xFFFF)|($val*6+4+rand(888)&0x00FF))*2+113+rand(999)
  64. }
  65. $stdin.gets
  66. #End of code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement