PythonでURLの暗号化
PythonでURLを暗号化した時のプログラム
下記の人のサイトを参考にして頂きました。
Javaの標準AES暗号化アルゴリズム互換のRuby(Python)実装 - OTメモ帳
暗号した時に「/」->「!」を処理にしないとURLなので注意ですね。
# -*- coding: utf-8 -*- from Crypto.Cipher import AES import base64 BS = 32 pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) unpad = lambda s : s[0:-ord(s[-1])] class AESCipher: def __init__(self, key): self.key = key def encrypt(self, text): cipher = AES.new(self.key, AES.MODE_ECB) raw = pad(text) enc = cipher.encrypt(raw) return base64.b64encode(enc) def decrypt(self, text): cipher = AES.new(self.key, AES.MODE_ECB) enc = base64.b64decode(text) return unpad(cipher.decrypt(enc)) def changeslash(self, text): ''' base64デコード(バイナリ変換) 「/」->「!」を処理''' text = text.replace('/', '!') return text def changexnpoint(self, text): ''' base64デコード(バイナリ変換) 「!」->「/」を処理''' text = text.replace('!', '/') return text