Skip to content

Instantly share code, notes, and snippets.

@PrivateGER
Last active February 8, 2022 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrivateGER/5e241269ae55738d70bc8e5f92ba1d1c to your computer and use it in GitHub Desktop.
Save PrivateGER/5e241269ae55738d70bc8e5f92ba1d1c to your computer and use it in GitHub Desktop.
Careful, will encrypt all files in directory except itself.
require 'aes'
require "base64"
def fileEncrypt(file, key)
handler = File.open(file, 'rb')
data = handler.read
handler.close
writefile = File.open(file, "wb")
encryptedContent = AES.encrypt(data, key)
writefile.puts encryptedContent
writefile.close
keystore = File.open("Keystore.txt", "wb")
keystore.puts key
keystore.close
end
def fileDecrypt(file, key)
handler = File.open(file, 'rb')
data = handler.read
handler.close
writefile = File.open(file, "wb")
decryptedContent = AES.decrypt(data, key)
writefile.puts decryptedContent
writefile.close
end
files = Dir.glob("*")
files.delete("Encrypt.rb")
files.delete("Keystore.txt")
files.delete("Gemfile")
files.delete("Encryptor.exe")
print "Encrypt? Y/N: "
mode = gets.chomp
if(mode == "Y")
key = AES.key
files.each do |filename|
fileEncrypt(filename, key)
puts "Encrypted #{filename}"
end
puts "Key: #{key}"
puts "Also saved in Keystore.txt"
gets
elsif(mode == "N")
print "Decrypt? Y/N: "
mode = gets.chomp
if(mode == "Y")
if(File.file?("Keystore.txt"))
print "Load key from Keystore? Y/N: "
mode == gets.chomp
if(mode == "Y")
handler = File.open("Keystore.txt", 'rb')
key = handler.read
handler.close
else
print "Key: "
key = gets.chomp
end
else
print "Key: "
key = gets.chomp
end
files.each do |filename|
fileDecrypt(filename, key)
puts "Decrypted #{filename}"
end
puts "Done"
gets
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment