Class: Aes
- Inherits:
-
Object
- Object
- Aes
- Defined in:
- lib/struggle/aes.rb
Instance Method Summary collapse
-
#decrypt(decrypted_string) ⇒ Object
解密,key 秘钥, decrypted_string 需要解密的内容.
-
#dir_decrypt(path) ⇒ Object
目录递归所有文件解密.
-
#dir_encrypt(path) ⇒ Object
目录递归所有文件加密.
-
#encrypt(encrypted_string) ⇒ Object
加密,key 秘钥, encrypted_string 需要加密的内容.
-
#file_decrypt(filename) ⇒ Object
文件内容解密并保存.
-
#file_encrypt(filename) ⇒ Object
文件内容加密并保存.
-
#initialize(key) ⇒ Aes
constructor
aes =.
Constructor Details
#initialize(key) ⇒ Aes
aes =
3 4 5 |
# File 'lib/struggle/aes.rb', line 3 def initialize(key) @@key = key end |
Instance Method Details
#decrypt(decrypted_string) ⇒ Object
解密,key 秘钥, decrypted_string 需要解密的内容
17 18 19 20 21 22 |
# File 'lib/struggle/aes.rb', line 17 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) ⇒ Object
目录递归所有文件解密
54 55 56 57 58 |
# File 'lib/struggle/aes.rb', line 54 def dir_decrypt(path) get_file_list(path).each do |fn| file_decrypt(fn) end end |
#dir_encrypt(path) ⇒ Object
目录递归所有文件加密
47 48 49 50 51 |
# File 'lib/struggle/aes.rb', line 47 def dir_encrypt(path) get_file_list(path).each do |fn| file_encrypt(fn) end end |
#encrypt(encrypted_string) ⇒ Object
加密,key 秘钥, encrypted_string 需要加密的内容
8 9 10 11 12 13 14 |
# File 'lib/struggle/aes.rb', line 8 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) ⇒ Object
文件内容解密并保存
36 37 38 39 40 41 42 43 44 |
# File 'lib/struggle/aes.rb', line 36 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) ⇒ Object
文件内容加密并保存
25 26 27 28 29 30 31 32 33 |
# File 'lib/struggle/aes.rb', line 25 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 |