LoginSignup
5

More than 5 years have passed since last update.

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

Last updated at Posted at 2013-04-02

text.txtをtext.encode.txtに出力する。

Pythonだと

encode.py

# coding:UTF-8

# ストリームを読み取る
with open("text.txt","r") as origin_file:
  stream_raw = origin_file.read()
  # ord()でコード値取得
  str_code_num = (ord(str) for str in stream_raw)
  # 0から255のコード値
  seed = 255
  # xorしながら連結しちゃう
  enc_result = "".join(map(lambda str: chr(str^seed), str_code_num))

# ファイルに書き出し
with open('text.encode.txt', 'w') as new_file:
  new_file.write(enc_result)

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
5