凯撒密码
凯撒:
除了是一个男人之外,他还是古罗马时期伟大的军事家和政治家。高卢战争的时候,他发明了这个密码,使得可以在敌人无法理解的情况下与联军进行联络。
凯撒密码:
正如刚才我们例子中的,将明文中的各个字符按顺序进行 n 个字符错位转换的加密方法我们称为凯撒密码。
优点:实现了最简单的加密方案,容易理解
缺点:对于有一点点密码学功底的朋友来说,安全强度几乎为零,有点弱不禁风。
密码的安全强度:
刚才我们提到了安全强度这个概念,顺道给大家介绍下。
例如凯撒密码,加密钥匙充其量也只有24个,也就是说,不管移动多少字符,最多只需要进行24次猜解即可破译出来!
知识普及:古代罗马字母只有25个哦 ^_^
栅栏密码
尽管名字看上去挺酷,不过很遗憾让大家失望了,栅栏密码也是很脆弱的。
所谓栅栏密码,就是把要加密的明文分成N个一组,然后把每组的第一个字母连起来,形成一段无规律的密文。
注意,栅栏密码本身有一个潜规则,就是组成栅栏的字母一般不会太多。(一般不超过30个,也就是一、两句话)
在线虚拟加密:https://inventwithpython.com/cipherwheel/
# -*- coding: utf-8 -*-
# @Time : 2017/4/27 22:13
# @Author : xiaojingjing
# @Site :
# @File : pyperclip_test.py
# @Software: PyCharm
import pyperclip
# message='This is my secret message.'
message=input("请输入需要加密的信息(必须为英文):")
key=int(input("请输入加密key的值(0~25整数):"))
#tells the program to encrypt or decrypt
# set to 'encrypt' or 'decrypt'
mode='encrypt'
#every possible symbol that can be encrypted
LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#stores the encrypted/decrypted form of the message
translated=''
#capitalize the string in message
#upper转换成大写,lower转换成小写
message=message.upper()
#run the encryption/decrypted code on each symbol in the message string
for symbol in message:
if symbol in LETTERS:
#get the encrypted(or decrypted) number for this symbol
num=LETTERS.find(symbol)
if mode=='encrypt':
num=num+key
elif mode=='decrypt':
num=num-key
#handle the wrap-around if num is larger than the length of
#LETTERS or less than 0
if num>=len(LETTERS):
num=num-len(LETTERS)
elif num<0:
num=num+len(LETTERS)
#add encrypted/decrypted number's symbol at the end of translated
translated=translated+LETTERS[num]
else:
#just add the symbol without encrypting/decrypting
translated=translated+symbol
print("使用凯撒加密,其中K='",key,"'时,对'",message,"'加密结果为:",translated)
#加密后的字符串结果复制到剪切板,通过Ctrl+V即可粘贴
pyperclip.copy(translated)