LoginSignup
3
3

More than 5 years have passed since last update.

ファイルの中身を簡単な暗号化(Ruby)

Posted at

text.txtをtext.encode.txtに出力する。
Rubyで書くと

encode.rb
# ストリームを読み取る
src_stream = File.open("text.txt", "r"){ |f| f.read }

tmp = []

# 0から255のコード値でxorする
src_stream.each_codepoint do |cp| 
  tmp << (cp ^ 255) 
end

# 連結する
enc_result = tmp.map{|i| i.chr}.join("")

# ファイルに書き出し
File.open("text.encode.txt","w") do |f|
  f.write(enc_result)
end

元ネタ

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3