caesar_cipher凯撒密码

更新时间:2023-05-09 12:38:37 阅读: 评论:0

caesar_cipher凯撒密码
caesar_cipher 凯撒密码
概念
凯撒密码是⼀种简单的替代密码,根据苏维托尼乌斯的记载,凯撒密码是由罗马共和国独裁官盖乌斯·尤利乌斯·恺撒发明的,他曾⽤凯撒密码来加密重要的军事情报。
作为⼀种替代加密算法,凯撒密码在如今看来,并⾮那么安全,它的加密⽅式只是简单的移位和替换,例如,如果明⽂移位1,则A被B替代,B将变为C,依此类推。
如果知道偏移位密钥,对密⽂解密是很简单的,只需⽤移位密钥表替换回正常字母表字母即可
移位密钥表的规律很简单,不管偏移⼏位数,这些字母都移动到正常字母表的最后。
如果不知道偏移位数密钥,怎么解密密⽂呢,⽅式其实也并不困难,因为凯撒密码只有25种偏移位数的可能性,所以,只需要计算出这25种结果,必然有⼀种结果是明⽂。算法
encrypt 加密
def encrypt(input_string: str, key: int, alphabet: Optional[str] = None) -> str:
"""
encrypt
=======
Encodes a given string with the caesar cipher and returns the encoded
message
Parameters:
-----------
*  input_string: the plain-text that needs to be encoded
*  key: the number of letters to shift the message by
Optional:
*  alphabet (None): the alphabet ud to encode the cipher, if not
specified, the standard english alphabet with upper and lowerca
letters is ud
Returns:
*  A string containing the encoded cipher-text
decrypt 解密
def decrypt(input_string: str, key: int, alphabet: Optional[str] = None) -> str:
brute_force 暴⼒破解
def brute_force(input_string: str, alphabet: Optional[str] = None) -> Dict[int, str]:
"""
brute_force
===========
Returns all the possible combinations of keys and the decoded strings in the
form of a dictionary
Parameters:
-----------
*  input_string: the cipher-text that needs to be ud during brute-force
Optional:
*  alphabet:  (None): the alphabet ud to decode the cipher, if not
specified, the standard english alphabet with upper and lowerca
letters is ud
代码
[caesar_cipher.py]{..\src\ciphers\caesar_cipher.py}
"""
Prepare
1. sys.path 中增加 TheAlgorithms\src ⼦模块
"""
import sys
sys.path.append('E:\dev\AI\TheAlgorithms\src')
案例⼀:
encrypt 加密
from ciphers.caesar_cipher import encrypt,decrypt,brute_force
"""
"""
'''
encrypt('The quick brown fox jumps over the lazy dog', 8)
'bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo'
'''
input_string = 'The quick brown fox jumps over the lazy dog'
key= 8+52
result = encrypt(input_string,key)
print(f'input_string:{input_string}' )
print(f'key:{key}' )
print(f'encrypt result:{result}' )
'''
encrypt('A very large key', 8000)
's nWjq dSjYW cWq'
'''
input_string = 'A very large key'
key= 8000
result = encrypt(input_string,key)
print(f'input_string:{input_string}' )
print(f'key:{key}' )
print(f'encrypt result:{result}' )
'''
encrypt('a lowerca alphabet', 5, 'abcdefghijklmnopqrstuvwxyz')
'f qtbjwhfxj fqumfgjy'
'''
input_string = 'a lowerca alphabet'
key= 5
alphabet ='abcdefghijklmnopqrstuvwxyz'
result = encrypt(input_string,key,alphabet)
print(f'input_string:{input_string}' )
print(f'key:{key}' )
print(f'encrypt result:{result}' )
input_string:The quick brown fox jumps over the lazy dog
key:60
encrypt result:bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo
input_string:A very large key
key:8000
encrypt result:s nWjq dSjYW cWq
input_string:a lowerca alphabet
key:5
encrypt result:f qtbjwhfxj fqumfgjy
案例⼆:
decrypt 解密
from ciphers.caesar_cipher import encrypt,decrypt,brute_force
"""
"""
'''
>>> decrypt('bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo', 8)
'The quick brown fox jumps over the lazy dog'
>>> decrypt('s nWjq dSjYW cWq', 8000)
'A very large key'
>>> decrypt('f qtbjwhfxj fqumfgjy', 5, 'abcdefghijklmnopqrstuvwxyz')
'a lowerca alphabet'
'''
print(decrypt('bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo', 8))
print(decrypt('s nWjq dSjYW cWq', 8000))
print(decrypt('f qtbjwhfxj fqumfgjy', 5, 'abcdefghijklmnopqrstuvwxyz'))
The quick brown fox jumps over the lazy dog
A very large key
a lowerca alphabet
案例三:
brute_force 暴⼒破解
brute_force(input_string: str, alphabet: Optional[str] = None) -> Dict[int, str]:
from ciphers.caesar_cipher import encrypt,decrypt,brute_force
"""
"""
'''
>>> brute_force("jFyuMy xIH'N vLONy zILwy Gy!")[20]
"Plea don't brute force me!"
'''
s = brute_force("jFyuMy xIH'N vLONy zILwy Gy!")
print(type(s))
print(s)
print(s[20])
<class 'dict'>
{1: "iExtLx wHG'M uKNMx yHKvx Fx!", 2: "hDwsKw vGF'L tJMLw xGJuw Ew!", 3: "gCvrJv uFE'K sILKv wFItv Dv!", 4: "fBuqIu tED'J rHKJu vEHsu Cu!", 5: "eAtpHt sDC'I qGJIt uDGrt Bt!", 6: "dzsoGs rCB'H pFIHs tCFqs As!", 7: "cyrnFr qBA'G o Plea don't brute force me!

本文发布于:2023-05-09 12:38:37,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/564415.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:密码   偏移   密钥   替代   位数   替换
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图