在本章中,让我们了解XOR过程及其在Python中的编码以及乘法密码的教程
XOR算法
XOR算法的加密和解密转换ASCII字节格式的纯文本,并使用XOR过程将其转换为指定的字节.它为用户提供以下优势 :
- 快速计算
- 没有区别标记左侧和右侧
- 易于理解和分析
代码
您可以使用以下代码执行XOR过程 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
def xor_crypt_string(data, key = 'awesomepassword' , encode = False , decode = False ): from itertools import izip, cycle import base64 if decode: data = base64.decodestring(data) xored = ''.join( chr ( ord (x) ^ ord (y)) for (x,y) in izip(data, cycle(key))) if encode: return base64.encodestring(xored).strip() return xored secret_data = "XOR procedure" print ( "The cipher text is" ) print xor_crypt_string(secret_data, encode = True ) print ( "The plain text fetched" ) print xor_crypt_string(xor_crypt_string(secret_data, encode = True ), decode = True ) |
输出
XOR流程的代码为您提供以下输出 :
说明
- 函数 xor_crypt_string()包括一个参数,用于指定编码和解码模式以及字符串值.
- 基本功能是使用base64模块进行的,这些模块遵循XOR过程/操作来加密或解密纯文本/密文.
注意 : XOR加密用于加密数据,并且难以通过强力方法破解,即通过生成随机加密密钥以匹配正确的密文.
乘法密码
使用凯撒密码技术时,加密和解密符号涉及使用简单的加法或减法基本过程将值转换为数字.
如果使用乘法转换为密文,它被称为环绕情况.考虑使用的字母和相关数字如下所示 :
这些数字将用于乘法过程,相关密钥为7.在这种场景中用于生成乘法密码的基本公式如下 :
(Alphabet Number * key)mod(total number of alphabets)
通过输出获取的数字映射到上面提到的表格和相应的字母作为加密字母.
Python中乘法密码的基本调制函数如下:
1
2
3
|
def unshift(key, ch): offset = ord (ch) - ASC_A return chr (((key[ 0 ] * (offset + key[ 1 ])) % WIDTH) + ASC_A) |
注意 : 乘法密码的优点是它可以使用非常大的密钥,如8,953,851.计算机需要相当长的时间才能通过大多数900万个密钥进行暴力破解.
以上就是Python密码学XOR编码流程及乘法密码教程的详细内容,更多关于Python密码学XOR乘法密码的资料请关注服务器之家其它相关文章!
原文链接:https://www.it1352.com/OnLineTutorial/cryptography_with_python/cryptography_with_python_multiplicative_cipher.html