关于AES-CBC模式字节翻转攻击(python3)
# coding:utf-8
from Crypto.Cipher import AES
import base64
def encrypt(iv, plaintext):
if len(plaintext) % 16 != 0:
print("plaintext length is invalid")
return
if len(iv) != 16:
print("IV length is invalid")
return
key = b"1234567890123456"
aes_encrypt = AES.new(key, AES.MODE_CBC, IV=iv)
result = base64.b64encode(aes_encrypt.encrypt(plaintext))
return result
def decrypt(iv, cipher):
if len(iv) != 16:
print("IV length is invalid")
print(len(iv))
return
key = b"1234567890123456"
aes_decrypt = AES.new(key, AES.MODE_CBC, IV=iv)
result = (aes_decrypt.decrypt(base64.b64decode(cipher)))
return result
def test1():
print("Change the first block plaintext:\n")
iv = b'ABCDEFGH12345678'
plaintext = b'0123456789ABCDEFhellocbcflipping0123456789123456'
cipher = encrypt(iv, plaintext)
print("NO ATTACK:", end='')
print(cipher)
local=2
before='2'
target='z'
iv = list(iv)
iv[local] = iv[local] ^ ord(before) ^ ord(target)
decipher = decrypt(bytes(iv), cipher)
print("ATTACK SUCCESS: Ciphertext doesn't need to be changed")
print("NOW PLAINTEXT:",end='')
print(decipher)
print("NEW IV(base64 encode):",end='')
print(base64.b64encode(bytes(iv)))
def test2():
iv = b'ABCDEFGH12345678'
plaintext = b'0123456789ABCDEFhellocbcflipping0123456789123456'
cipher = encrypt(iv, plaintext)
print("NO ATTACK:",end='')
print(cipher)
cipher = attack(cipher, 31, 'g', 'G')
# 进行攻击 cipher是密文,31代表第几位,‘g’是原本的字符,‘G’是改变的字符
decipher=decrypt(iv,cipher)
print("NOW PLAINTEXT:", end='')
print(decipher)
#由于改变了密文,导致前一组解密乱码,需要修改iv值,先要求出新密文
newiv=re(iv,decipher,b'0123456789ABCDEF')
#求新的iv值,传入原iv值,新密文,和已知明文
de_cipher = decrypt(newiv, cipher)
print("NOW PLAINTEXT(NEW IV):", end='')
print(de_cipher)
def attack( cipher, local, before, target):
cipher = base64.b64decode(cipher)
cipher = list(cipher)
cipher[local - 16] = cipher[local - 16] ^ ord(before) ^ ord(target)
cipher = base64.b64encode(bytes(cipher))
print("ATTACK SUCCESS:",end='')
print(cipher)
return cipher
def re(iv, decipher, cleartext):
decipher = list(decipher)
cleartext = bytearray(cleartext)
bin_iv=bytearray(iv)
for i in range(0,len(iv)):
bin_iv[i]= (decipher[i] ^ bin_iv[i] ^ cleartext[i])
print("NEW IV(base64 encode):",end='')
print(base64.b64encode(bin_iv))
return bin_iv
test1()
#test1为改变第一组的函数
#test2()
#test2为改变不是第一组的函数
关于AES-CBC模式字节翻转攻击(python3)的更多相关文章
- CBC字节翻转攻击
iscc2018线上赛开始两周多了,学到了很多,写几篇文章总结一下遇到的知识点,做一个归纳,方便以后查找. web300-----CBC字节翻转攻击 cbc是AES加密的cbc模式 即密码分组链模式: ...
- Padding Oracle 和 CBC字节翻转攻击学习
以前一直没时间来好好研究下这两种攻击方式,虽然都是很老的点了= =! 0x01:Padding oracle CBC加密模式为分组加密,初始时有初始向量,密钥,以及明文,明文与初始向量异或以后得到中间 ...
- php AES cbc模式 pkcs7 128位加密解密(微信小程序)
PHP AES CBC模式PKCS7 128位加密 加密: $key = '1234567812345678'; $iv = '1234567890123456'; $message = '12345 ...
- PHP AES cbc模式 pkcs7 128加密解密
今天在对接一个第三方接口的时候,对方需要AES CBC模式下的加密.这里简单写一个demo class Model_Junjingbao extends Model { private static ...
- python 实现 AES CBC模式加解密
AES加密方式有五种:ECB, CBC, CTR, CFB, OFB 从安全性角度推荐CBC加密方法,本文介绍了CBC,ECB两种加密方法的python实现 python 在 Windows下使用AE ...
- CBC 字节反转攻击
一.CBC 简介 现代密码体制 现代密码中的加密体制一般分为对称加密体制(Symmetric Key Encryption)和非对称加密体制(Asymmetric Key Encryption).对称 ...
- 实验吧之【简单的登录题(】CBC字节反转攻击)
开始刷ctf题吧 慢慢来. 实验吧---简单的登录题 题目地址:http://ctf5.shiyanbar.com/web/jiandan/index.php 随便提交一个id,看到后台set了两个 ...
- 解决AES算法CBC模式加密字符串后再解密出现乱码问题
问题 在使用 AES CBC 模式加密字符串后,再进行解密,解密得到的字符串出现乱码情况,通常都是前几十个字节乱码: 复现 因为是使用部门 cgi AESEncryptUtil 库,找到问题后,在这里 ...
- CBC加密原理及攻击
原理基于分组加密加密过程 Plaintext:明文,待加密的数据.IV:用于随机化加密的比特块,保证即使对相同明文多次加密,也可以得到不同的密文,初始向量,用来与第一块的明文异或运算.Key:被一些如 ...
随机推荐
- 从维基百科等网站复制数据和公式到MathType里编辑
在我们写论文的时候,经常会需要用一些实际案例以及数据,而这些数据和案例有很大一部分可以直接在网络上找到.但是有时候也会发现我们想要的内容和公式,从网页上复制粘贴后太模糊,不适合打印和投影.就需要我们将 ...
- C语言讲义——链表完整代码
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { int _id; ...
- java数组作为函数返回值
1 //将一个二维数组行和列元素互换.存到另一个二维数组 2 package test; 3 4 public class test1_8 { 5 public static int[][] huhu ...
- pixi.js持续渲染页面
Pixi是一个超快的2D渲染引擎,通过Javascript和Html技术创建动画或管理交互式图像,从而制作游戏或应用. 项目地址:https://github.com/pixijs/pixi.js A ...
- 6.2 Binding基础
WPF最为核心的思想是数据驱动UI,实现这一技术的基石就是绑定技术(binding).如果把Binding比作数据的桥梁,那么它的两端分别是源(Source)和目标(Target),Binging是架 ...
- 「刷题笔记」LCA问题相关
板子 ll lg[40]; ll dep[N],fa[N][40]; ll dis[N]; void dfs(ll u,ll f) { dep[u]=dep[f]+1; fa[u][0]=f; for ...
- 虚拟机下Ubuntu共享文件夹不能显示的一种解决方法
原文链接:https://blog.csdn.net/huyangzhilin/article/details/70666937
- IdentityServer4系列 | 授权码模式
一.前言 在上一篇关于简化模式中,通过客户端以浏览器的形式请求IdentityServer服务获取访问令牌,从而请求获取受保护的资源,但由于token携带在url中,安全性方面不能保证.因此,我们可以 ...
- 【万字长文】Dubbo 入门总结 ,一款高性能的 Java RPC 框架
这篇文章是我学习整理 Dubbo 的一篇文章,首先大部分内容参考了官网 + 某硅谷的视频,内容讲解进行了重新编排,40多张图片,也都是我修改重制的,虽然一万多字,但是其实也可以看出来,更多的内容集中在 ...
- Python中使用eval执行下面函数的结果怎么是字符串'10020'?
定义了函数: def add(a,b): s='a+b' c=compile(s,'','eval') gArea,lArea = {},{} gArea['a']=str(a10) gArea['b ...