pad: ZeroPadding

mode: cbc

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 这里使用pycrypto‎库
# 按照方法:easy_install pycrypto‎

from Crypto.Cipher import AES
import base64

class prpcrypt():

def __init__(self, key, iv):
self.key = key
self.mode = AES.MODE_CBC
self.iv = iv

# 加密函数,如果text不足16位就用空格补足为16位,
# 如果大于16当时不是16的倍数,那就补足为16的倍数。
def encrypt(self, text):
cryptor = AES.new('123454536f667445454d537973576562',
self.mode, IV=self.iv)
# 这里密钥key 长度必须为16(AES-128),
# 24(AES-192),或者32 (AES-256)Bytes 长度
# 目前AES-128 足够目前使用
length = 16
count = len(text)
if count < length:
add = (length - count)
#\0 backspace
text = text + ('\0' * add)
elif count > length:
add = (length - (count % length))
text = text + ('\0' * add)
self.ciphertext = cryptor.encrypt(text)
# 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
# 所以这里统一把加密后的字符串转化为16进制字符串
return base64.b64encode(self.ciphertext)

# 解密后,去掉补足的空格用strip() 去掉
def decrypt(self, text):
cryptor = AES.new(self.key, self.mode, IV=self.iv)
plain_text = cryptor.decrypt(base64.b64decode(text))
return plain_text.rstrip('\0')

if __name__ == '__main__':
pc = prpcrypt('123454536f667445454d537973576562',
'1234577290ABCDEF1264147890ACAE45'[0:16]) # 初始化密钥

e = pc.encrypt('T10515') # 加密
print "加密:", e

d = pc.decrypt(e) # 解密
print "解密:", d

AES:

pad pkcs7

mode cbc

# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import base64
import os

BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s: s[0:-ord(s[-1])]

key = os.urandom(16) # the length can be (16, 24, 32)
text = '123456'

cipher = AES.new(key)
cipher = AES.new('33b21adee1b8620a7ba81aea1a80c724',
AES.MODE_CBC, IV='1234567812345678')

encrypted = cipher.encrypt(pad(text))
encrypted = base64.b64encode(encrypted)
print encrypted # will be something like 'f456a6b0e54e35f2711a9fa078a76d16'

python AES 加密的更多相关文章

  1. Python: AES加密与解密

    起源: 视频下载,解析到一个网站时,发现其视频id是用AES加密过的,用的是https://code.google.com/archive/p/crypto-js/这个库.解密很简单的一句js代码: ...

  2. Python AES加密

    使用pycrypto模块https://pypi.python.org/pypi/pycrypto/ >>> from Crypto.Cipher import AES>> ...

  3. python AES 加密与解密

    #用aes加密,再用base64 encode def aes_encrypt(data): from Crypto.Cipher import AES import base64 key=setti ...

  4. python AES加密 ECB PKCS5

    class AesEbc16:  # 按块的大小, 一块一块的加密, 明文和密文长度一样 def __init__(self): self.key = b"123qweqqqwerqwer& ...

  5. python AES加密解密 pycryptodome

    环境 pyhton3.6 博主为了解码 AES 用了1天的时间,安了各种包,然而走了很多坑,在这里给大家提供一个简便的方法 首先在命令行(推荐)  pip install Crypto 你会发现安装下 ...

  6. python aes加解密

    python AES加密解密 python AES 双向对称加密解密 Python中进行Base64编码和解码 # encoding:utf-8 import base64 from Crypto.C ...

  7. C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现[转载]

    原文:http://outofmemory.cn/code-snippet/35524/AES-with-javascript-java-csharp-python-or-php c#里面的AES加密 ...

  8. python AES 双向对称加密解密

    高级加密标准(Advanced Encryption Standard,AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分 ...

  9. 使用python进行加密解密AES算法

    使用python进行加密解密AES算法-代码分享-PYTHON开发者社区-pythoner.org 使用python进行加密解密AES算法 TY 发布于 2011-09-26 21:36:53,分类: ...

随机推荐

  1. 阿里云ESC搭建SVN服务端

    CentOS7)下yum命令快速安装svn服务端,学习在思考中独孤中度过,在孤独中进取! 01.SVN服务的安装(subversion) 02.ESC安全组策略 1.在线安装svn服务 $ sudo  ...

  2. result-charts

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. HDUOJ----(1031)Design T-Shirt

    Design T-Shirt Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. HDUOJ--Strange fuction

    Strange fuction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. IOS中通知中心(NSNotificationCenter)

    摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广.   IOS中通知中心NSNotificationCenter应用总结 一.了 ...

  6. Hadoop Streaming

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/streaming.html Hadoop Streaming Streaming工作原理 将文件打包到提交的 ...

  7. SDL 2.0 API by Category

    Basics View information and functions related to... View the header Initialization and Shutdown SDL. ...

  8. 关于ftpshell脚本中mget中去除多余交互式提示的方法

    默认情况下ftp里面的交互式提示是开启的,平常如果下载多个文件时,这种提示很让人烦,如果是在shell脚本里面要从ftp服务器上一次mget多个文件,写个交互式很麻烦,最好是把这个交互式提示关掉. 进 ...

  9. python学习笔记——

    python线程的GIL GIL (全局解释器锁)python --- > 支持多线程 ----> 同步和互斥 --->加锁 --->解释器加锁 ————> 解释器同一时 ...

  10. GL_会计科目子模组追溯至总账分析(案例)

    2014-06-02 BaoXinjian