The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Help please!
Can someone write the code below in Java Language? Its in Ruby.
=========================================================================================
#Define a class RC4
class RC4
    #Define function initialize() with string as argument
    def initialize(str)
   Â
         #Begin
         begin
        Â
         #Display message
raise SyntaxError, "RC4: Key supplied is blank" if str.eql?('')
        Â
         #Call initialize_state()
         initialize_state(str)
        Â
         #Set q1 and q2 as 0
         @q1, @q2 = 0, 0
         end
        Â
    #End of function
    end
    #Define function encrypt!()
    def encrypt!(text)
   Â
         #Set index as 0
         index = 0
         #Loop until length of text
         while index < text.length
         #Set q1
         @q1 = (@q1 + 1) % 256
        Â
         #Set q2
         @q2 = (@q2 + @state[@q1]) % 256
        Â
         #State q1 and q2
         @state[@q1], @state[@q2] = @state[@q2], @state[@q1]
        Â
         #Set byte of text
text.setbyte(index, text.getbyte(index) ^ @state[(@state[@q1] + @state[@q2]) % 256])
        Â
         #Increment index
         index += 1
        Â
         #End of loop
         end
        Â
         #Text
         text
        Â
    #End of function
    end
    alias_method :decrypt!, :encrypt!
    #Define encrypt()
    def encrypt(text)
         #Call encrypt!()
         encrypt!(text.dup)
    end
    alias_method :decrypt, :encrypt
    private
    # Modified initial state
    INITIAL_STATE = (0..255).to_a
    # Define initialize_state()
    def initialize_state(key)
         #Set i, j as 0
         i = j = 0
         #Set state
         @state = INITIAL_STATE.dup
         #Set key length
         key_length = key.length
         #Loop until 256
         while i < 256
         #Compute j
j = (j + @state[i] + key.getbyte(i % key_length)) % 256
         @state[i], @state[j] = @state[j], @state[i]
        Â
         #Increment i
         i += 1
      end
    end
end
-----------