Search code examples
rubyencryptionterminalopensslkali-linux

How do I encrypt/decrypt strings in Ruby via Terminal?


So I'm having some trouble running my script.

My script has two commands : -e which is for encryption, and -d for decryption. The second ARGV is the key for the asymmetric cryptography and the string that it encrypt/decrypts is just static you will see it says 'Words and Stuff' in the code.

When I run the script it just pops up blank and the command is not ran, and when I do try to run -e for example ruby encryptor.rb -e sup3rS3cretKey it just says Invalid command '-e'; type "help" for a list. error in -e. So it seems its running openssl for some reason because of my require `openssl` statement and it does not operate my commands its seems as my script is not being ran from the terminal. So how do I fix this, and what is the openssl thing that it is doing called?

-Script

require `openssl`

if ARGV[0] == '-e' #Encrypt

 if ARGV.length != 2
  puts "Please input a key."
  exit
 end

 puts "Encrypting"
 key = ARGV[1]
 cipher = OpenSSL::Cipher.new('Words and Stuff').encrypt
 cipher.key = Digest::SHA1.hexdigest key
 s = cipher.update(self) + cipher.final

 s.unpack('H*')[0].upcase
 puts "Encrypted"

elsif ARGV[0] == '-d' #Decrypt

 if ARGV.length != 2
  puts "Please input a key."
  exit
 end

 puts "Decrypting"
 key = ARGV[1]
 cipher = OpenSSL::Cipher.new('Words and Stuff').decrypt
 cipher.key = Digest::SHA1.hexdigest key
 s = [self]/pack("H*").unpack("C*").pack("c*") 

 cipher.update(s) + cipher.final
 puts "String decrypted."
end


Solution

  • Your initial problem is that you are requiring 'openssl' with backticks so it is trying to call it in the shell. Swap the backticks for normal quotes and you will make it past that line.