des/3des
Posted dong1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了des/3des相关的知识,希望对你有一定的参考价值。
一、python
1、 des3
python平台的DES3 + base64 加密解密, 有两个常用的库pycrypto和pyDes
1)pycrypto
des3.py
#coding=utf-8
from Crypto.Cipher import _DES3
import base64
import json
BS = _DES3.block_size
def pad(s):
return s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
#定义 padding 即 填充 为PKCS7
def unpad(s):
return s[0:-ord(s[-1])]
class prpcrypt():
def __init__(self, key):
self.key = key
self.mode = _DES3.MODE_CBC #模式为CBC
self.iv = IV #self.iv 为 IV 即偏移量,ECB模式不使用IV
# DES3的加密模式为CBC
def encrypt(self, text):
text = pad(text)
cryptor = _DES3.new(self.key, self.mode, self.iv)
x = len(text) % 8
if x != 0:
text = text + ‘