{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### 上节课最后版本:独立出的convert_char函数" ] }, { "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", " ALPHABET_SRC = 'abcdefghijklmnopqrstuvwxyz'\n", " ALPHABET_TAR = 'defghijklmnopqrstuvwxyzabc'\n", " \n", " result = ''\n", " if single_char in ALPHABET_SRC:\n", " if operation == 'encrypt':\n", " result = ALPHABET_TAR[ALPHABET_SRC.index(single_char)]\n", " elif operation == 'decrypt':\n", " result = ALPHABET_SRC[ALPHABET_TAR.index(single_char)]\n", " else:\n", " result = single_char\n", " return result\n", " \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", "\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", "#对两个函数互反性进行验证\n", "assert(decrypt_it(encrypt_it('AbCdefgH!')) == 'AbCdefgH!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 字母表位置偏移法实现" ] }, { "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", " OFFSET = 10\n", " ALPHABET_SRC = 'abcdefghijklmnopqrstuvwxyz'\n", " \n", " result = ''\n", " if single_char in ALPHABET_SRC:\n", " if operation == 'encrypt':\n", " result = ALPHABET_SRC[(ALPHABET_SRC.index(single_char) + OFFSET) % 26]\n", " elif operation == 'decrypt':\n", " result = ALPHABET_SRC[(ALPHABET_SRC.index(single_char) - OFFSET) % 26]\n", " else:\n", " result = single_char\n", " return result\n", " \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", "\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", "#对两个函数互反性进行验证\n", "assert(decrypt_it(encrypt_it('AbCdefgH!')) == 'AbCdefgH!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "encrypt_it('AbCdefgH!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ASCII偏移置换实现(ASCII范围:33-126)" ] }, { "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", " OFFSET = 10\n", " \n", " result = ''\n", " if ord(single_char) >= 33 and ord(single_char) <= 126:\n", " if operation == 'encrypt':\n", " result = chr((ord(single_char) - 33 + OFFSET) % (126 - 33 + 1) + 33)\n", " elif operation == 'decrypt':\n", " result = chr((ord(single_char) - 33 - OFFSET) % (126 - 33 + 1) + 33)\n", " else:\n", " result = single_char\n", " return result\n", " \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", "\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", "#对两个函数互反性进行验证\n", "assert(decrypt_it(encrypt_it('AbCdefgH!')) == 'AbCdefgH!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "encrypt_it('AbCdefgH!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 用字典实现的表-表随机加密" ] }, { "cell_type": "code", "execution_count": null, "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", " \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", " \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", "\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", "#对两个函数互反性进行验证\n", "assert(decrypt_it(encrypt_it('AbCdefgH!')) == 'AbCdefgH!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "encrypt_it('I love u!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "decrypt_it(', IW2a kg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 用random.shuffle()打乱列表顺序" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(random.shuffle)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 用列表推导生成原始字母表" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alphabet_src = [chr(i) for i in range(33, 127)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 用浅拷贝避免冲突" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alphabet_tar = alphabet_src.copy()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alphabet_tar is alphabet_src" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 用pickle实现序列化/反序列化,将随机密码映射表通过磁盘文件进行存/取" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pickle" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pickle.dumps([alphabet_src, alphabet_tar])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 用open()以指定模式打开文件" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f = open('test.txt', 'w')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f.write('12312324235345')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!ls test.txt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!cat test.txt" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f = open('key.dat', 'wb')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f.write(pickle.dumps([alphabet_src, alphabet_tar]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 用with实现文件的自动关闭" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open('key.dat', 'wb') as f:\n", " f.write(pickle.dumps([alphabet_src, alphabet_tar]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open('key.dat', 'rb') as f:\n", " print(pickle.loads(f.read()))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 用pickle的dump、load直接序列化文件访问" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pickle.dump(\n", " [alphabet_src, alphabet_tar], \n", " open('key1.dat', 'wb')\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!ls key1.dat" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!cat key1.dat" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pickle.load(open('key.dat', 'rb'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir(pickle)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(open)" ] } ], "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 }