python - 密码学 - 如何使用 ofb 模式实现 MARS?
问题描述
我正在尝试在 python 中为我正在处理的项目找到一个使用OFB操作模式的MARS算法的实现,但我找不到一个可行的。
我在stackoverflow中只找到了OFB模式的实现
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
plainText = b"Lorem ipsum dolor sit amet, consectetur adipiscing e"
key = b"ANAAREMEREAAAAAA"
def ofbEnc(plainText, key):
pos = 0
cipherTextChunks = []
iv = get_random_bytes(16)
originalIV = iv
cipher = AES.new(key, AES.MODE_ECB)
if len(plainText) % 16 != 0:
plainText += b"1"
while len(plainText) % 16 != 0:
plainText += b"0"
while pos + 16 <= len(plainText):
toXor = cipher.encrypt(iv)
nextPos = pos + 16
toEnc = plainText[pos:nextPos]
cipherText = bytes([toXor[i] ^ toEnc[i] for i in range(16)])
cipherTextChunks.append(cipherText)
pos += 16
iv = toXor
return (originalIV, cipherTextChunks)
def ofbDec(cipherTextChunks, key, iv):
plainText = b""
cipher = AES.new(key, AES.MODE_ECB)
for chunk in cipherTextChunks:
toXor = cipher.encrypt(iv)
plainText += bytes([toXor[i] ^ chunk[i] for i in range(15)])
iv = toXor
while plainText[-1] == 48:
plainText = plainText[0:-1]
if plainText[-1] == 49:
plainText = plainText[0:-1]
return plainText
iv, result = ofbEnc(plainText, key)
print(iv, result)
plain = ofbDec(result, key, iv)
print(plain)
但我在 python 中找不到MARS实现。
另外,为了理解一些事情,当我在OFB模式下使用MARS加密时,这意味着我使用MARS算法加密每个块,然后对于每个块,我使用OFB模式算法再次加密它?
例如:我正在尝试使用 128 位加密字符串:'text',而MARS将使用 64 位块大小进行加密。
所以我将字符串分成 2 个块:b1、b2,每个块 64 位。
对于我用MARS算法加密的每个块 b1、b2 ,我们称它们为 encb1、encb2。
那么对于每个加密块 encb1, encb2 我用OFB算法加密它们?
解决方案
推荐阅读
- javascript - TypeError statusText:Django 中使用 Ajax 的简单帖子“LIKE”功能的“内部服务器错误”
- python - 为什么使用 Series 设置 MultiIndex 数据框会给出一列 NaN?
- tfs - 如何在扩展的对话框中显示 TFS 工作项?
- javascript - 将 JSON.parse 与 Node.js 一起使用时获得更详细的错误消息
- javascript - Chrome 扩展程序在选项卡中接收消息。chrome.tabs.sendMessage
- php - 限制评论 wordpress
- php - 内连接-Laravel
- java - 无法在Java中连接两个句子
- r - 如何在 R 中列出多个计算的结果
- loops - Pascal 中的“for i:=0 to i”循环 - 为什么它会起作用?