{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### 用字典实现的表-表随机加密" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "alphabet_src = [chr(i) for i in range(33, 127)]\n", "alphabet_tar = alphabet_src.copy()\n", "random.shuffle(alphabet_tar)\n", "alphabet_s2t_dict = dict()\n", "alphabet_t2s_dict = dict()\n", "for i in range(len(alphabet_src)):\n", " alphabet_s2t_dict[alphabet_src[i]] = alphabet_tar[i]\n", " alphabet_t2s_dict[alphabet_tar[i]] = alphabet_src[i]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def convert_char(single_char: str, operation: str) -> str:\n", " '''对单个字符进行加密\n", " 输入参数:\n", " single_char: 要加密的单个字符\n", " operation: 加密还是解密,encrypt->加密,decrypt->解密\n", " 返回结果:加密/解密后的单个字符\n", " '''\n", " global alphabet_src, alphabet_tar\n", " result = ''\n", " if ord(single_char) >= 33 and ord(single_char) <= 126:\n", " if operation == 'encrypt':\n", " result = alphabet_s2t_dict[single_char]\n", " elif operation == 'decrypt':\n", " result = alphabet_t2s_dict[single_char]\n", " else:\n", " result = single_char\n", " return result\n", "def encrypt_it(src_str: str) -> str:\n", " '''用于对字符串进行简单替换加密\n", " 输入参数:\n", " src_str: 原始文本内容\n", " 返回结果:加密/解密文本\n", " '''\n", " encrypted_str = ''\n", " for single_char in src_str:\n", " encrypted_str += convert_char(single_char, 'encrypt')\n", " return encrypted_str\n", "def decrypt_it(encrypted_str: str) -> str:\n", " '''用于对字符串进行简单替换解密\n", " 输入参数:\n", " encrypted_str: 加密文本内容\n", " 返回结果:解密文本\n", " '''\n", " decrypted_str = ''\n", " for single_char in encrypted_str:\n", " decrypted_str += convert_char(single_char, 'decrypt')\n", " return decrypted_str\n", "#对两个函数互反性进行验证\n", "assert(decrypt_it(encrypt_it('AbCdefgH!')) == 'AbCdefgH!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 类化的改造:对紧密联系的函数和变量进行封装" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "class CryptIt:\n", " \"\"\" \"\"\"\n", " alphabet_src = [chr(i) for i in range(33, 127)]\n", " alphabet_tar = alphabet_src.copy()\n", " alphabet_s2t_dict = dict()\n", " alphabet_t2s_dict = dict()\n", " \n", " def __init__(self):\n", " random.shuffle(self.alphabet_tar)\n", " for i in range(len(self.alphabet_src)):\n", " self.alphabet_s2t_dict[self.alphabet_src[i]] = self.alphabet_tar[i]\n", " self.alphabet_t2s_dict[self.alphabet_tar[i]] = self.alphabet_src[i]\n", "\n", " def convert_char(self, single_char: str, operation: str) -> str:\n", " '''对单个字符进行加密\n", " 输入参数:\n", " single_char: 要加密的单个字符\n", " operation: 加密还是解密,encrypt->加密,decrypt->解密\n", " 返回结果:加密/解密后的单个字符\n", " '''\n", " result = ''\n", " if ord(single_char) >= 33 and ord(single_char) <= 126:\n", " if operation == 'encrypt':\n", " result = self.alphabet_s2t_dict[single_char]\n", " elif operation == 'decrypt':\n", " result = self.alphabet_t2s_dict[single_char]\n", " else:\n", " result = single_char\n", " return result\n", "\n", " def encrypt_it(self, src_str: str) -> str:\n", " '''用于对字符串进行简单替换加密\n", " 输入参数:\n", " src_str: 原始文本内容\n", " 返回结果:加密/解密文本\n", " '''\n", " encrypted_str = ''\n", " for single_char in src_str:\n", " encrypted_str += self.convert_char(single_char, 'encrypt')\n", " return encrypted_str\n", "\n", " def decrypt_it(self, encrypted_str: str) -> str:\n", " '''用于对字符串进行简单替换解密\n", " 输入参数:\n", " encrypted_str: 加密文本内容\n", " 返回结果:解密文本\n", " '''\n", " decrypted_str = ''\n", " for single_char in encrypted_str:\n", " decrypted_str += self.convert_char(single_char, 'decrypt')\n", " return decrypted_str" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### alphabet_tar是可变类型,作为类变量为所有实例共享,会造成逻辑问题" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "my_crypt_a = CryptIt()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'W6#yIn9;'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_crypt_a.encrypt_it('AbCdefgH')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "my_crypt_b = CryptIt()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'?3=1,TX9'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_crypt_b.encrypt_it('AbCdefgH')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'?3=1,TX9'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_crypt_a.encrypt_it('AbCdefgH')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 前后两次调用 my_crypt_a.encrypt_it('AbCdefgH') 结果不同,所以将可变类型类变量改成\\_\\_init\\_\\_()里初始化的实例变量" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "class CryptIt:\n", " \"\"\" \"\"\"\n", " def __init__(self):\n", " self.alphabet_src = [chr(i) for i in range(33, 127)]\n", " self.alphabet_tar = self.alphabet_src.copy()\n", " self.alphabet_s2t_dict = dict()\n", " self.alphabet_t2s_dict = dict()\n", " random.shuffle(self.alphabet_tar)\n", " for i in range(len(self.alphabet_src)):\n", " self.alphabet_s2t_dict[self.alphabet_src[i]] = self.alphabet_tar[i]\n", " self.alphabet_t2s_dict[self.alphabet_tar[i]] = self.alphabet_src[i]\n", "\n", " def convert_char(self, single_char: str, operation: str) -> str:\n", " '''对单个字符进行加密\n", " 输入参数:\n", " single_char: 要加密的单个字符\n", " operation: 加密还是解密,encrypt->加密,decrypt->解密\n", " 返回结果:加密/解密后的单个字符\n", " '''\n", " result = ''\n", " if ord(single_char) >= 33 and ord(single_char) <= 126:\n", " if operation == 'encrypt':\n", " result = self.alphabet_s2t_dict[single_char]\n", " elif operation == 'decrypt':\n", " result = self.alphabet_t2s_dict[single_char]\n", " else:\n", " result = single_char\n", " return result\n", "\n", " def encrypt_it(self, src_str: str) -> str:\n", " '''用于对字符串进行简单替换加密\n", " 输入参数:\n", " src_str: 原始文本内容\n", " 返回结果:加密/解密文本\n", " '''\n", " encrypted_str = ''\n", " for single_char in src_str:\n", " encrypted_str += self.convert_char(single_char, 'encrypt')\n", " return encrypted_str\n", "\n", " def decrypt_it(self, encrypted_str: str) -> str:\n", " '''用于对字符串进行简单替换解密\n", " 输入参数:\n", " encrypted_str: 加密文本内容\n", " 返回结果:解密文本\n", " '''\n", " decrypted_str = ''\n", " for single_char in encrypted_str:\n", " decrypted_str += self.convert_char(single_char, 'decrypt')\n", " return decrypted_str\n", " \n", " def assert_crypt(self):\n", " assert(self.decrypt_it(self.encrypt_it('AbCdefgH!')) == 'AbCdefgH!')\n", " print('Assertion OK!')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "my_crypt = CryptIt()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assertion OK!\n" ] } ], "source": [ "my_crypt.assert_crypt()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'X)vO%+seB'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_crypt.encrypt_it('AbCdefgH!')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"8<:'H45D+\"" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_crypt.decrypt_it('?#7,eIGp$')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "my_crypt_a = CryptIt()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'@WJ3<%ZC]'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_crypt_a.encrypt_it('AbCdefgH!')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'X)vO%+seB'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_crypt.encrypt_it('AbCdefgH!')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "a = {\n", " 1 : (1,2,4)\n", "}" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 4)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "0", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 0" ] } ], "source": [ "a[0]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,3][1]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: (1, 2, 4)}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "a = dict()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: (1, 2, 4)}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "a[3] = {1, 2, 3}" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: (1, 2, 4), 3: {1, 2, 3}}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 in [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1, 2, 4) in a" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n" ] } ], "source": [ "for i in a:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__class__',\n", " '__contains__',\n", " '__delattr__',\n", " '__delitem__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__iter__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__setitem__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " 'clear',\n", " 'copy',\n", " 'fromkeys',\n", " 'get',\n", " 'items',\n", " 'keys',\n", " 'pop',\n", " 'popitem',\n", " 'setdefault',\n", " 'update',\n", " 'values']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(a)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: (1, 2, 4), 3: {1, 2, 3}}" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys([1, 3])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.keys()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values([(1, 2, 4), {1, 2, 3}])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.values()" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([(1, (1, 2, 4)), (3, {1, 2, 3})])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.items()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 (1, 2, 4)\n", "3 {1, 2, 3}\n" ] } ], "source": [ "for k in a.keys():\n", " print(k, a[k])" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 (1, 2, 4)\n", "3 {1, 2, 3}\n" ] } ], "source": [ "for k, v in a.items():\n", " print(k, v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k, v = 1, 2" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1,2" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "src_text = \"\"\"A\t·-\n", "H\t····\n", "O\t---\n", "V\t···-\n", "B\t-···\n", "I\t··\n", "P\t·--·\n", "W\t·--\n", "C\t-·-·\n", "J\t·---\n", "Q\t--·-\n", "X\t-··-\n", "D\t-··\n", "K\t-·-\n", "R\t·-·\n", "Y\t-·--\n", "D\t-··\n", "K\t-·-\n", "R\t·-·\n", "Y\t-·--\n", "E\t·\n", "L\t·-··\n", "S\t···\n", "Z\t--··\n", "F\t··-·\n", "M\t--\n", "T\t-\n", "G\t--·\n", "N\t-·\n", "U\t··-\"\"\"" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "moorse_table_dict = dict()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "items = src_text.split()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(items)/2" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "for i in range(int(len(items)/2)):\n", "# print(items[2 * i], items[2 * i + 1])\n", " moorse_table_dict[items[2 * i]] = items[2 * i + 1]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'A': '·-',\n", " 'H': '····',\n", " 'O': '---',\n", " 'V': '···-',\n", " 'B': '-···',\n", " 'I': '··',\n", " 'P': '·--·',\n", " 'W': '·--',\n", " 'C': '-·-·',\n", " 'J': '·---',\n", " 'Q': '--·-',\n", " 'X': '-··-',\n", " 'D': '-··',\n", " 'K': '-·-',\n", " 'R': '·-·',\n", " 'Y': '-·--',\n", " 'E': '·',\n", " 'L': '·-··',\n", " 'S': '···',\n", " 'Z': '--··',\n", " 'F': '··-·',\n", " 'M': '--',\n", " 'T': '-',\n", " 'G': '--·',\n", " 'N': '-·',\n", " 'U': '··-'}" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "moorse_table_dict" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "for line in src_text.split('\\n'):\n", " k, v = line.split('\\t')\n", " moorse_table_dict[k] = v" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'A': '·-',\n", " 'H': '····',\n", " 'O': '---',\n", " 'V': '···-',\n", " 'B': '-···',\n", " 'I': '··',\n", " 'P': '·--·',\n", " 'W': '·--',\n", " 'C': '-·-·',\n", " 'J': '·---',\n", " 'Q': '--·-',\n", " 'X': '-··-',\n", " 'D': '-··',\n", " 'K': '-·-',\n", " 'R': '·-·',\n", " 'Y': '-·--',\n", " 'E': '·',\n", " 'L': '·-··',\n", " 'S': '···',\n", " 'Z': '--··',\n", " 'F': '··-·',\n", " 'M': '--',\n", " 'T': '-',\n", " 'G': '--·',\n", " 'N': '-·',\n", " 'U': '··-'}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "moorse_table_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A\t·-\tB\t-···\tC\t-·-·\tD\t-··\tE\t·\tF\t··-·\tG\t--·\n", "H\t····\tI\t··\tJ\t·---\tK\t-·-\tL\t·-··\tM\t--\tN\t-·\n", "O\t---\tP\t·--·\tQ\t--·-\tR\t·-·\tS\t···\tT\t-\tU\t··-\n", "V\t···-\tW\t·--\tX\t-··-\tY\t-·--\tZ\t--··\t" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }