Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Created May 1, 2023 12:22
Show Gist options
  • Save thomasjslone/88b8bf429c013a1c482c097135db54fd to your computer and use it in GitHub Desktop.
Save thomasjslone/88b8bf429c013a1c482c097135db54fd to your computer and use it in GitHub Desktop.
obfascate filedata from user
require 'openssl'
# Method to encrypt a file using AES
def encrypt_file_aes(path)
# Generate a random key and initialization vector (IV)
key = OpenSSL::Random.random_bytes(32)
iv = OpenSSL::Random.random_bytes(16)
# Read the file content
file_content = File.read(path)
# Create an AES cipher with the key and IV
cipher = OpenSSL::Cipher.new('AES-256-CBC')
cipher.encrypt
cipher.key = key
cipher.iv = iv
# Encrypt the file content
encrypted = cipher.update(file_content) + cipher.final
# Write the encrypted content to a new file
File.write("#{path}.enc", encrypted)
# Return the key and IV
[key, iv]
end
# Method to decrypt a file using AES
def decrypt_file_aes(path, key, iv)
# Read the encrypted content from the file
encrypted = File.read(path)
# Create an AES cipher with the key and IV
cipher = OpenSSL::Cipher.new('AES-256-CBC')
cipher.decrypt
cipher.key = key
cipher.iv = iv
# Decrypt the content
decrypted = cipher.update(encrypted) + cipher.final
# Write the decrypted content to a new file
File.write("#{path}.dec", decrypted)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment