Skip to content

Instantly share code, notes, and snippets.

@Xvezda
Last active May 22, 2020 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xvezda/cdd889ff5200c828b0a5291e3396cd2f to your computer and use it in GitHub Desktop.
Save Xvezda/cdd889ff5200c828b0a5291e3396cd2f to your computer and use it in GitHub Desktop.
CloudFlare email protection encoding, decoding with python.
# -*- coding: utf-8 -*-
def decode_cfemail(enc):
from textwrap import wrap
res = ''
key = int(enc[:2], 16)
for v in wrap(enc[2:], 2):
res += chr(int(v, 16) ^ key)
return res
"""
e.g.
`<span class="__cf_email__" data-cfemail="ee96988b948a8fae808f988b9cc08d8183">[email&#160;protected]</span>`
>>> decode_cfemail('ee96988b948a8fae808f988b9cc08d8183')
'xvezda@naver.com'
"""
# -*- coding: utf-8 -*-
from __future__ import print_function
def encode_cfemail(email):
def hexpad(char):
return char[2:].zfill(2)
from random import random as rand
# Generate random key
key = int(rand() * (0xff+1) + 1) # 0x01 ~ 0xff
# Prefix key
enc = hexpad(hex(key))
for c in email:
# Build string
enc += hexpad(hex(ord(c) ^ key))
return enc
"""
e.g.
>>> encode_cfemail('xvezda@naver.com')
'cdb5bba8b7a9ac8da3acbba8bfe3aea2a0'
"""
@Xvezda
Copy link
Author

Xvezda commented May 22, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment