- 教程
- 教程
- 双倍强度加密(Double Strength Encryption)
- Python Overview and 安装
- Reverse Cipher
- Caesar Cipher
- ROT13算法(ROT13 Algorithm)
- 换位密码(Transposition Cipher)
- 转置密码的加密(Encryption of Transposition Cipher)
- 换位密码的解密(Decryption of Transposition Cipher)
- 加密文件(Encryption of files)
- 解密文件(Decryption of files)
- Base64编码和解码(Base64 Encoding & Decoding)
- XOR Process
- 乘法密码(Multiplicative Cipher)
- Affine Ciphers
- 黑客单字母密码(Hacking Monoalphabetic Cipher)
- 简单的替代密码(Simple Substitution Cipher)
- 简单替换密码的测试(Testing of Simple Substitution Cipher)
- 简单替换密码的解密(Decryption of Simple Substitution Cipher)
- Python Modules of Cryptography
- 了解Vigenere Cipher(Understanding Vignere Cipher)
- 实现Vigenere Cipher(Implementing Vignere Cipher)
- 一次性密码(One Time Pad Cipher)
- 一次性密码密码的实现(Implementation of One Time Pad Cipher)
- 对称和非对称密码学(Symmetric & Asymmetric Cryptography)
- 理解RSA算法(Understanding RSA Algorithm)
- 创建RSA密钥(Creating RSA Keys)
- RSA密码加密(RSA Cipher Encryption)
- RSA密码解密(RSA Cipher Decryption)
- 黑客攻击RSA密码(Hacking RSA Cipher)
- 资源
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
简单的替代密码(Simple Substitution Cipher)
简单替换密码是最常用的密码,包括为每个密文文本字符替换每个纯文本字符的算法。 在这个过程中,与凯撒密码算法相比,字母表是混乱的。
例子 (Example)
简单替换密码的密钥通常由26个字母组成。 一个示例关键是 -
plain alphabet : abcdefghijklmnopqrstuvwxyz
cipher alphabet: phqgiumeaylnofdxjkrcvstzwb
使用上述密钥的示例加密是 -
plaintext : defend the east wall of the castle
ciphertext: giuifg cei iprc tpnn du cei qprcni
以下代码显示了一个实现简单替换密码的程序 -
import random, sys
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def main():
message = ''
if len(sys.argv) > 1:
with open(sys.argv[1], 'r') as f:
message = f.read()
else:
message = raw_input("Enter your message: ")
mode = raw_input("E for Encrypt, D for Decrypt: ")
key = ''
while checkKey(key) is False:
key = raw_input("Enter 26 ALPHA key (leave blank for random key): ")
if key == '':
key = getRandomKey()
if checkKey(key) is False:
print('There is an error in the key or symbol set.')
translated = translateMessage(message, key, mode)
print('Using key: %s' % (key))
if len(sys.argv) > 1:
fileOut = 'enc.' + sys.argv[1]
with open(fileOut, 'w') as f:
f.write(translated)
print('Success! File written to: %s' % (fileOut))
else: print('Result: ' + translated)
# Store the key into list, sort it, convert back, compare to alphabet.
def checkKey(key):
keyString = ''.join(sorted(list(key)))
return keyString == LETTERS
def translateMessage(message, key, mode):
translated = ''
charsA = LETTERS
charsB = key
# If decrypt mode is detected, swap A and B
if mode == 'D':
charsA, charsB = charsB, charsA
for symbol in message:
if symbol.upper() in charsA:
symIndex = charsA.find(symbol.upper())
if symbol.isupper():
translated += charsB[symIndex].upper()
else:
translated += charsB[symIndex].lower()
else:
translated += symbol
return translated
def getRandomKey():
randomList = list(LETTERS)
random.shuffle(randomList)
return ''.join(randomList)
if __name__ == '__main__':
main()
输出 (Output)
实现上面给出的代码时,您可以观察到以下输出 -
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论