Created
May 1, 2023 12:22
-
-
Save thomasjslone/88b8bf429c013a1c482c097135db54fd to your computer and use it in GitHub Desktop.
obfascate filedata from user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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