Skip to content

Instantly share code, notes, and snippets.

@poochin
Created May 25, 2011 01:27
Show Gist options
  • Select an option

  • Save poochin/990144 to your computer and use it in GitHub Desktop.

Select an option

Save poochin/990144 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
排他的論理和を使った簡単な暗号
101 = 5 を使って変換
'''
import unittest
class TestCoder(unittest.TestCase):
def setUp(self):
pass
def testCoder(self):
base = 'bow wow'
coded = 'gjr%rjr'
self.assertEqual(exor(base), coded)
def testHoge(self):
base = 'hoge'
coded = 'mjb`'
self.assertEqual(exor(base), coded)
def testHomu(self):
base = 'homu'
coded = 'mjhp'
self.assertEqual(exor(base), coded)
def testDecoder(self):
base = 'co452865'
coded = 'fj107=30'
self.assertEqual(base, exor(coded))
def exor(src):
xornum = 0b101
result = ''
for ch in src:
# ch[str] => ord[int] => xor[int] => chr[str]
result += chr(ord(ch) ^ xornum)
return result
def main():
unittest.main()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment