-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.rb
More file actions
43 lines (36 loc) · 1.33 KB
/
Copy pathencrypt.rb
File metadata and controls
43 lines (36 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
=begin
文字列の暗号化と復号を行うメソッド。ふい文字かわいすぎてやゔぁい!
=end
require 'openssl'
require 'bin_dump.rb'
def encrypt(s, key, iv)
enc = OpenSSL::Cipher::Cipher.new("aes-128-cbc") #128bitのAES暗号をCBCモードで使うインスタンス生成
enc.encrypt #暗号化モード
enc.key = key
enc.iv = iv
return enc.update(s) + enc.final #暗号化
end
def decrypt(s, key, iv)
dec = OpenSSL::Cipher::Cipher.new("aes-128-cbc") #128bitのAES暗号をCBCモードで使うインスタンス生成
dec.decrypt #復号モード
dec.key = key
dec.iv = iv
return dec.update(s) + dec.final #復号
end
if __FILE__ == $0 #このプログラムが単体で実行される場合のみ以下を実行
plaintext = ARGV[0]
io = File.open('k1.txt', 'rb')
io2 = File.open('iv.txt', 'rb')
key = io.gets
iv = io2.gets
print "[key] : "
bin_dump(key, 16)
print "[iv] : "
bin_dump(iv, 16)
ciphertext = encrypt plaintext, key, iv #__FILE__ と $0は他のプログラムにrequireされると一致しない。
print "[ciphertext] : "
bin_dump(ciphertext, 16)
b = decrypt ciphertext, key, iv
p b
end