naya_hira’s diary

三日坊主の備忘録

【ROT13】 任意の数ずらすROT13 【Python3】

はじめに

ROT13 とは、ユリウス・カエサル(ジュリウス・シーザー)が用いたとされる有名な暗号です。 今回は、Python3.6を用いてrot13を実装しました。

ソースコード

def rot13(crypt,shift):
  plain = ''
  for c in crypt:
    if 'A' <= c and c <= 'Z':
      plain += chr((ord(c) - ord('A') + shift) % 26 + ord('A'))

    elif 'a' <= c and c <= 'z':
      plain += chr((ord(c) - ord('a') + shift) % 26 + ord('a'))

    else :
      plain += c

  return plain

def main():
  text= 'fsdz{Fdhvdu_flskhu_lv_fodvvlfdo_flskhu}'
  n = -3
  print(rot13(text,n))

if __name__ == "__main__":
  main()