Created
May 25, 2011 01:27
-
-
Save poochin/990144 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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