Security and Cryptography in Python - Block Cipher(1)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Security and Cryptography in Python - Block Cipher(1),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2545字,纯文字阅读大概需要4分钟。
内容图文
![Security and Cryptography in Python - Block Cipher(1)](/upload/InfoBanner/zyjiaocheng/606/ea65b514084d4908aece0da3dbc1bc68.jpg)
Security and Cryptography in Python - Block Cipher(1)
DES
https://en.wikipedia.org/wiki/Data_Encryption_Standard
GOST
https://en.wikipedia.org/wiki/GOST_(block_cipher)
pyDes
https://github.com/twhiteman/pyDes/blob/master/pyDes.py
DES
ECB mode
from pyDes import *
message = "0123456701234567"
key = "DESCRYPT"
iv = bytes([0]*8)
k = des(key, ECB, iv, pad=None, padmode=PAD_PKCS5)
cipher = k.encrypt(message)
print("Length of plain text:", len(message ))
print("Length of cipher text:", len(cipher))
print("Encrypted:", cipher[0:8])
print("Encrypted:", cipher[8:16])
print("Encrypted:", cipher[16:])
message = k.decrypt(cipher)
print("Decrypted:", message)
CBC Mode
from pyDes import *
message = "0123456701234567"
key = "DESCRYPT"
iv = bytes([0]*8)
k = des(key, CBC, iv, pad=None, padmode=PAD_PKCS5)
cipher = k.encrypt(message)
print("Length of plain text:", len(message ))
print("Length of cipher text:", len(cipher))
print("Encrypted:", cipher[0:8])
print("Encrypted:", cipher[8:16])
print("Encrypted:", cipher[16:])
message = k.decrypt(cipher)
print("Decrypted:", message)
DES and modifying the cipher texts - ECB Mode
from pyDes import *
def modify(cipher):
mod = [0]*len(cipher)
mod[10] = ord(' ') ^ ord('1')
mod[11] = ord(' ') ^ ord('0')
mod[12] = ord('1') ^ ord('0')
return bytes([mod[i] ^ cipher[i] for i in range(len(cipher))])
message = "Give Bob: 10$"
key = "DESCRYPT"
iv = bytes([0]*8)
k = des(key, ECB, iv, pad=None, padmode=PAD_PKCS5)
# Alice sending the encrypted message
cipher = k.encrypt(message)
print("Length of plain text:", len(message ))
print("Length of cipher text:", len(cipher))
print("Encrypted:", cipher)
# Bob modifying the cipher text
cipher = modify(cipher)
# this is the bank decrypting the message
message = k.decrypt(cipher)
print("Decrypted:", message)
Running Result:
Change the modified block and run it again:
Change the modified block again and run it again:
DES and modifying the cipher texts - CBC Mode
from pyDes import *
def modify(cipher):
mod = [0]*len(cipher)
mod[9] = 1
return bytes([mod[i] ^ cipher[i] for i in range(len(cipher))])
message = "Give Bob: 10$ and send them to him"
key = "DESCRYPT"
iv = bytes([0]*8)
k = des(key, CBC, iv, pad=None, padmode=PAD_PKCS5)
# Alice sending the encrypted message
cipher = k.encrypt(message)
print("Length of plain text:", len(message ))
print("Length of cipher text:", len(cipher))
print("Encrypted:", cipher)
# Bob modifying the cipher text
cipher = modify(cipher)
# this is the bank decrypting the message
message = k.decrypt(cipher)
print("Decrypted:", message)
Running Result:
内容总结
以上是互联网集市为您收集整理的Security and Cryptography in Python - Block Cipher(1)全部内容,希望文章能够帮你解决Security and Cryptography in Python - Block Cipher(1)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。