class Struggle::Aes
Public Class Methods
new(key)
click to toggle source
aes =
# File lib/struggle/aes.rb, line 4 def initialize(key) @@key = key end
Public Instance Methods
decrypt(decrypted_string)
click to toggle source
解密,key 秘钥, decrypted_string 需要解密的内容
# File lib/struggle/aes.rb, line 18 def decrypt(decrypted_string) aes = OpenSSL::Cipher::Cipher.new("AES-256-ECB").clone aes.decrypt aes.key = @@key aes.update([decrypted_string].pack('H*')) << aes.final end
dir_decrypt(path)
click to toggle source
目录递归所有文件解密
# File lib/struggle/aes.rb, line 55 def dir_decrypt(path) get_file_list(path).each do |fn| file_decrypt(fn) end end
dir_encrypt(path)
click to toggle source
目录递归所有文件加密
# File lib/struggle/aes.rb, line 48 def dir_encrypt(path) get_file_list(path).each do |fn| file_encrypt(fn) end end
encrypt(encrypted_string)
click to toggle source
加密,key 秘钥, encrypted_string 需要加密的内容
# File lib/struggle/aes.rb, line 9 def encrypt(encrypted_string) aes = OpenSSL::Cipher::Cipher.new("AES-256-ECB").clone aes.encrypt aes.key = @@key txt = aes.update(encrypted_string) << aes.final txt.unpack('H*')[0].upcase end
file_decrypt(filename)
click to toggle source
文件内容解密并保存
# File lib/struggle/aes.rb, line 37 def file_decrypt(filename) if File.exist? filename puts filename content = File.read(filename) unless content.blank? File.write(filename, Base64.decode64(decrypt(content)).force_encoding("UTF-8")) end end end
file_encrypt(filename)
click to toggle source
文件内容加密并保存
# File lib/struggle/aes.rb, line 26 def file_encrypt(filename) if File.exist? filename puts filename content = File.read(filename) unless content.blank? File.write(filename, encrypt(Base64.encode64(content.force_encoding("UTF-8")))) end end end
Private Instance Methods
get_file_list(path)
click to toggle source
递归目录下所有文件
# File lib/struggle/aes.rb, line 64 def get_file_list(path) files = [] Dir.entries(path).each do |sub| if sub != '.' && sub != '..' if File.directory?("#{path}/#{sub}") get_file_list("#{path}/#{sub}") else files << "#{path}/#{sub}" end end end files end