记录顺带保存一次课堂作业,部分参考了现有的实现,结合需求进行了一波码,能用就行,人和代码能跑一个就好,替换密码暴力破解实在懒得搞了,或许以后会更?

import getopt
import sys#接收参数,规定参数名
opts, args = getopt.getopt(sys.argv[1:], '-h-v-m:-a:-p:-c:-k:-A:', ['help', 'version', 'mode=', 'action=', 'plaintext=', 'ciphertext=', 'key=', 'admin='])
for opt_name, opt_value in opts:if opt_name in ('-h', '--help'):print("Help Info: \n""-m or --mode, value can use caesar or permute \n""-a or --action, value can use encode or decode or bruteAttack \n""-p or --plaintext \n""-c or --ciphertext \n""-k or --key \n""example-1:-m caesar -a encode -p helloworld -k 3 \n""example-2:--mode permute --action=decode --ciphertext=helloworld -k word \n")sys.exit()elif opt_name in ('-v', '--version'):print("Version is 1.0, Update in 2022/4/26 ")sys.exit()elif opt_name in ('-m', '--mode'):mode = opt_valueelif opt_name in ('-a', '--action'):action = opt_valueelif opt_name in ('-p', '--plaintext'):plaintext = opt_valueelif opt_name in ('-c', '--ciphertext'):ciphertext = opt_valueelif opt_name in ('-k', '--key'):key = opt_valueelif opt_name in ('-A', '--admin'):if(opt_value == "admin"):print("Developer:SZPT-AI")print("Mode =", mode)print("Action =", action)#凯撒加密
def caesarEncode(plaintext, key):encodeText = []for word in plaintext:encodeText.append(chr(ord('a') + ((ord(word) - ord('a')) + int(key)) % 26))ciphertext = "".join(encodeText)print(ciphertext)#凯撒解密
def caesarDecode(ciphertext, key):DecodeText = []for word in ciphertext:DecodeText.append(chr(ord('a') + ((ord(word) - ord('a')) - int(key)) % 26))plaintext = "".join(DecodeText)print(plaintext)#凯撒暴力破解
def caesarBruteAttack(ciphertext):#遍历所有偏移量for key in range(1,26):DecodeText = []for word in ciphertext:DecodeText.append(chr(ord('a') + ((ord(word) - ord('a')) - int(key)) % 26))plaintext = "".join(DecodeText)print(plaintext)def keyIntFormat(key):# 将key转化为数字tmp_key1 = []for keyword in key:tmp_key1.append(ord(keyword) - 96)# 建立临时列表进行排序tmp_key2 = sorted(tmp_key1)tmp_key = [] * len(key)for num in range(len(tmp_key2)):for keynum in range(len(tmp_key1)):if (tmp_key1[num] == tmp_key2[keynum]):tmp_key.append(str(keynum + 1))key = "".join(tmp_key)return key#置换加密
def permuteEncode(plaintext, key):keylen = len(key)ciphertext = ""key = keyIntFormat(key)#根据key长度对明文进行分段for part in range(0, len(plaintext), keylen):tmp_ciphertext = [""] * keylen#超长度内容处理if part + keylen > len(plaintext):tmp_plaintext = plaintext[part:]#内容处理else:tmp_plaintext = plaintext[part:part + keylen]for word in range(len(tmp_plaintext)):tmp_ciphertext[int(key[word]) - 1] = tmp_plaintext[word]ciphertext += "".join(tmp_ciphertext)print(ciphertext)#置换解密
def permuteDecode(ciphertext, key):keylen = len(key)plaintext = ""key = keyIntFormat(key)#根据key长度对密文进行分段for part in range(0, len(ciphertext), keylen):#构筑矩阵tmp_plaintext = [""] * keylen#超长度内容处理if part + keylen >= len(ciphertext):tmp_ciphertext = ciphertext[part:]re_key = []#根据key确认位置for word in range(len(tmp_ciphertext)):re_key.append(int(key[word]) - 1)re_key.sort()for word in range(len(tmp_ciphertext)):tmp_plaintext[word] = tmp_ciphertext[re_key.index(int(key[word]) - 1)]#内容处理else:#分段内容tmp_ciphertext = ciphertext[part:part + keylen]for word in range(len(tmp_ciphertext)):tmp_plaintext[word] = tmp_ciphertext[int(key[word]) - 1]plaintext += "".join(tmp_plaintext)print(plaintext)#置换暴力破解
def permuteBruteAttack(ciphertext):#其实就是太麻烦了我不想写#此处思路可以根据出现频率高的单词字母去匹配字典有可能的日常高频单词(就是随缘撞库)print("Develop in future release")if(mode == "caesar"):   #凯撒加密if(action == "encode"):caesarEncode(plaintext, key)elif(action == "decode"):caesarDecode(ciphertext, key)elif(action == "bruteAttack"):caesarBruteAttack(ciphertext)elif(mode == "permute"):    #置换加密if(action == "encode"):permuteEncode(plaintext, key)elif(action == "decode"):permuteDecode(ciphertext, key)elif(action == "bruteAttack"):permuteBruteAttack(ciphertext)

云安全-Python实现凯撒密码和替换密码的加密解密与暴力破解相关推荐

  1. python密码学凯撒密码_凯撒密码在Python

    python密码学凯撒密码 Hello everyone, in this tutorial you'll learn about Caesar cipher in Python. If you ha ...

  2. python实现凯撒密码、凯撒加解密算法

    python实现凯撒密码.凯撒加解密算法 更多python视频教程请到菜鸟教程https://www.piaodoo.com/ 凯撒密码的原理:计算并输出偏移量为3的凯撒密码的结果 注意:密文是大写字 ...

  3. python 实现凯撒加密

    凯撒密码(Caesar)基本原理 字母表一共有26个英文字母,加密时,我们需要将某个明文字母做N位偏移得到密文,这个N最多为26,而且偏移为26时和偏移为0时一样,明文和密文对应相等,实际上可以说最大 ...

  4. Python 实现凯撒加解密

    凯撒加密法指的是两千年前由凯撒大帝使用的加密法,是一种最简单且最广为人知的加密技术.它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文.历史上,通 ...

  5. Java版 凯撒密码 加密、解密、暴力破解

    Java版 凯撒密码 加密.解密.暴力破解 用Java实现凯撒密码的 '加密' 和 '解密' 工作 代码实现如下: 代码片 package com.hellow.demo;import java.ut ...

  6. Java加密算法—凯撒加密实现以及暴力破解

    目录 1.概念 2.加密实现 3.解密实现 4.频率分析法破解 1.概念 凯撒密码最早由古罗马军事统帅盖乌斯·尤利乌斯·凯撒在军队中用来传递加密信息,故称凯撒密码.这是一种位移加密方式,只对26个字母 ...

  7. python中凯撒密码_python实现凯撒密码、凯撒加解密算法

    凯撒密码的原理:计算并输出偏移量为3的凯撒密码的结果 注意:密文是大写字母,在变换加密之前把明文字母都替换为大写字母 def casar(message): # *************begin* ...

  8. python解决凯撒密码

    恺撒密码是古罗马恺撒大帝用来对军事情报进行加解密的算法,它采用了替换方法对信息中的每一个英文字符循环替换为字母表序列中该字符后面的第三个字符,即,字母表的对应关系如下: 原文:A B C D E F ...

  9. python:凯撒密码

    恺撒密码是对信息中的每一个英文字符循环替换为字母表序列中该字符后面的第三个字符. 对于原文字符P,其密文字符C满足如下条件:C=(P+3) mod 26‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬ ...

  10. python实现凯撒密码

    这是我首次用python语言写东西,里边代码肯定不能算非常高效,如有疑问,欢迎在博客留言 我觉得应该先对凯撒密码的原理有一定的了解,再看代码才最有效 凯撒的百度百科:https://baike.bai ...

最新文章

  1. 【Spring】BeanFactory解析bean详解
  2. 魔兽老玩家无需购买《燃烧远征》资料片序列号
  3. win7 双屏 双工具栏_不知道这几个双屏操作技巧,你的显示器就白买了
  4. 计算与推断思维 四、数据类型
  5. 常见解压缩软件与其zip格式
  6. 用手画了11张图终于搞明白了Git工作流,我怀疑你用的是假 Git
  7. 数学建模——matlab基本使用
  8. 量化投资中的特征工程
  9. Android RadioGroup中横向 竖向布局RadioButton的问题
  10. 国内物联网平台(7):Ablecloud物联网自助开发和大数据云平台
  11. VMD确定分解个数K(matlab)
  12. PCB设计---无源晶振和有源晶振
  13. mongodb Reconfig attempted to install a config that would change the implicit default write concern.
  14. lower_bound和upper_bound的用法
  15. Android应用之个人日记本
  16. 可由线性表示且表达式唯一_线性代数期末模拟题一8p
  17. 无盘服务器万兆网卡吃鸡报错,(2018.05.26)召唤绿化大师V3.2无盘万能包-xp-Win7x64
  18. 计算机模拟泰勒公式,泰勒公式有什么实际性的应用?
  19. video 元素自定义 controls 控件以及常用事件、方法
  20. 职业选择测试(A/B卷)

热门文章

  1. Android手机sock5代理免root
  2. RGB 和 YUV之间的转换
  3. jupyter notebook 快捷键
  4. NavicatPremium-Mac-无法打开问题
  5. Electron下使用samba相关问题记录
  6. python怎么读取dat类型文件_基于python批量处理dat文件及科学计算方法详解
  7. qt值qml制作足球动画(参考qmlbook)
  8. yagmail发送邮件
  9. 何为仿射变换(Affine Transformation)
  10. Windows XP 系统优化-百度转载