Posted By

mcandre on 08/08/07


Tagged

password ruby ios7crypt ios7 cisco 7 decrypt encrypt


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


ios7crypt.rb


 / Published in: Ruby
 

  1. #!/usr/bin/env ruby
  2.  
  3. # Author:: Andrew Pennebaker
  4. # Copyright:: Copyright 2007 Andrew Pennebaker
  5. # License:: GPL
  6. #
  7. # == Synopsis
  8. #
  9. # ios7crypt: encrypts and decrypts passwords with Cisco IOS7 algorithm
  10. #
  11. # == Usage
  12. #
  13. # ios7crypt [OPTIONS]
  14. #
  15. # --help, -h:
  16. # show help
  17. #
  18. # --encrypt, -e <password1> <password2> <password3> ...:
  19. # prints out the encrypted passwords as hashes
  20. #
  21. # --decrypt, -d <hash1> <hash2> <hash3> ...:
  22. # prints out the decrypted hashes as passwords
  23.  
  24. require "getoptlong"
  25. require "rdoc/usage"
  26.  
  27. $xlat=[
  28. 0x64, 0x73, 0x66, 0x64, 0x3b, 0x6b, 0x66, 0x6f,
  29. 0x41, 0x2c, 0x2e, 0x69, 0x79, 0x65, 0x77, 0x72,
  30. 0x6b, 0x6c, 0x64, 0x4a, 0x4b, 0x44, 0x48, 0x53,
  31. 0x55, 0x42, 0x73, 0x67, 0x76, 0x63, 0x61, 0x36,
  32. 0x39, 0x38, 0x33, 0x34, 0x6e, 0x63, 0x78, 0x76,
  33. 0x39, 0x38, 0x37, 0x33, 0x32, 0x35, 0x34, 0x6b,
  34. 0x3b, 0x66, 0x67, 0x38, 0x37
  35. ]
  36.  
  37. def encrypt(password)
  38. seed=rand(16)
  39. password=password[0, 11]
  40.  
  41. hash=(0 .. (password.length-1)).collect { |i| $xlat[(seed+i)%$xlat.length] ^ password[i] }
  42.  
  43. return format("%02d", seed) + hash.collect { |e| format("%02x", e) }.join("")
  44. end
  45.  
  46. def decrypt(hash)
  47. seed=hash[0, 2].to_i
  48.  
  49. hash=hash[2, hash.length-1]
  50.  
  51. pairs=(0 .. (hash.length/2-1)).collect { |i| hash[i*2, 2].to_i(16) }
  52.  
  53. decrypted=(0 .. (pairs.length-1)).collect { |i| $xlat[(seed+i)%$xlat.length] ^ pairs[i] }
  54.  
  55. return (decrypted.collect { |e| e.chr }).join("")
  56. end
  57.  
  58. opts=GetoptLong.new(
  59. ["--help", "-h", GetoptLong::NO_ARGUMENT],
  60. ["--encrypt", "-e", GetoptLong::NO_ARGUMENT],
  61. ["--decrypt", "-d", GetoptLong::NO_ARGUMENT]
  62. )
  63.  
  64. mode = :help
  65.  
  66. opts.each do |option, value|
  67. case option
  68. when "--help"
  69. RDoc::usage
  70. when "--encrypt"
  71. mode = :encrypt
  72. when "--decrypt"
  73. mode = :decrypt
  74. end
  75. end
  76.  
  77. case mode
  78. when :help
  79. RDoc::usage
  80. when :encrypt
  81. ARGV.each { |arg| puts encrypt(arg) }
  82. when :decrypt
  83. ARGV.each { |arg| puts decrypt(arg) }
  84. end

Report this snippet  

You need to login to post a comment.