zoukankan      html  css  js  c++  java
  • 使用 PyCrypto 进行 AES/ECB/PKCS#5(7) 加密

    东篱

    使用 PyCrypto 进行 AES/ECB/PKCS#5(7) 加密

    2013/06/05 · tech

    PyCrypto 是流行的 Python 加密/解密库。但是其 AES 的 ECB 模式在加密解密时未提供适合的密文填充工具,因此有必要自己实现一个,下面是常见的 PKCS#5 / PKCS#7 填充模式的一般写法:

    '''
    PKCS#5 padding is identical to PKCS#7 padding, except that 
    it has only been defined for block ciphers that use a 64 bit (8 byte) 
    block size.
    But in AES, there is no block of 8 bit, so PKCS#5 is PKCS#7.
    '''
    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])]

    在进行加密/解密前,只需使用 pad/unpad 进行填充/截断即可。下面是具体实例:

    # -*- coding: utf-8 -*-fromCrypto.Cipherimport AES
    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 ='to be encrypted'
    
    cipher = AES.new(key)
    
    encrypted = cipher.encrypt(pad(text)).encode('hex')print encrypted  # will be something like 'f456a6b0e54e35f2711a9fa078a76d16'
    
    decrypted = unpad(cipher.decrypt(encrypted.decode('hex')))print decrypted  # will be 'to be encrypted'

    提醒一点,PKCS#5 和 PKCS#7 唯一的区别就是前者规定了数据块大小是 64 比特(8 字节),而 AES 中数据块大小是 128 比特(16字节),因此在 AES 中, PKCS#5 和 PKCS#7 没有区别。

    顺便提一下,在 Java 中偶尔遇到如下方式获取 AES 实例:

    Cipher cipher = javax.crypto.Cipher.getInstance("AES")

    但是在文档 Java™ Cryptography Architecture Standard Algorithm Name Documentation 以及 Cipher Java doc 中并未直接说明这种方式所使用的模式以及填充方式。而AES默认的 Provider AESCipher 中则有所说明,其使用的默认模式和填充方式正是 ECB 和 PKCS5Padding。今天写一个 Python 工具需要与 Java 通讯,我使用的 Java 库正是使用上述方式构造 Cipher,所以进行了一番了解。

  • 相关阅读:
    JAVA学习日报 8.26
    JAVA学习日报 8.25
    JAVA学习日报 8.24
    JAVA学习日报 8.23
    Docker 详解
    DRF 3 请求响应异常处理
    DRF 2 序列化器
    DRF 1 API接口规范
    计算机计算小数的方法
    软件结构体系第二章
  • 原文地址:https://www.cnblogs.com/lexus/p/3382620.html
Copyright © 2011-2022 走看看