pythonaesecb
① python进行 AES CBC-128bit PKCS7/PKCS5 填充加密解密
你看一下这个例子吧。可以参考下面的地址:前面加上http,把句号改成点。
likang。me/blog/2013/06/05/python-pycrypto-aes-ecb-pkcs-5/
#-*-coding:utf-8-*-
fromCrypto.CipherimportAES
importos
BS=AES.block_size
pad=lambdas:s+(BS-len(s)%BS)*chr(BS-len(s)%BS)
unpad=lambdas:s[0:-ord(s[-1])]
key=os.urandom(16)#thelengthcanbe(16,24,32)
text='tobeencrypted'
cipher=AES.new(key)
encrypted=cipher.encrypt(pad(text)).encode('hex')
printencrypted#willbesomethinglike''
decrypted=unpad(cipher.decrypt(encrypted.decode('hex')))
printdecrypted#willbe'tobeencrypted'
② 如何在AES CTR适用于Python和PyCrypto
首先,AES-CTR的整点是,你并不需要填充。这是一个流密码(不同于ECB / CBC等)!
之所以出现这种行为,你在问题中所描述的是明文(4字节/ 32位)为四个小如AES(128位)的块大小。而到了crypto反对你只得到数据经过块大小后回来。所以,你的问题将通过实例化一个新的解决cryto对象,像这样:
counter = os.urandom(16) # fixed counter - do not use this in proction!
key = os.urandom(32) # 256 bits key
encrypto = AES.new(key, AES.MODE_CTR, counter=lambda: counter)
encrypted = encrypto.encrypt("asdk")
# Instantiate a new cipher for decryption
decrypto = AES.new(key, AES.MODE_CTR, counter=lambda: counter)
print decrypto.decrypt(encrypted) # prints "asdk"
③ java 的cipher AES/CBC/PKCS5Padding 加密后,使用openssl的AES_cbc_encrypt无法解密
你看一下这个例子吧。可以参考下面的地址:前面加上http,把句号改成点。
likang。me/blog/2013/06/05/python-pycrypto-aes-ecb-pkcs-5/
# -*- coding: utf-8 -*-
from Crypto.Cipher import 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 ''
decrypted = unpad(cipher.decrypt(encrypted.decode('hex')))
print decrypted # will be 'to be encrypted'
④ 如何使用Python进行Rijndael方式的加密解密
Rijndael,在高级加密标准(AES)中使用的基本密码算法。
概述 (美国)国家标准技术研究所(NIST)选择Rijndael作为美国政府加密标准(AES)的加密算法,AES取代早期的数据加密标准(DES)。Rijndael由比利时计算机科学家Vincent Rijmen和Joan Daemen开发,它可以使用128位,192位或者256位的密钥长度,使得它比56位的DES更健壮可靠。Rijndael也有一个非常小的版本(52位),合适用在蜂窝电话、个人数字处理器(PDA)和其他的小设备上。
近似读音:Rijn [rain] dael [del] (莱恩戴尔) Rijn 来源 Rhine [莱茵河]的荷兰语(Dutch)发音。
dael 是常用的人名 这词是两个科学家的名字各出一段拼成的。
Rijndael.h
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <exception>
#include <string.h>
using namespace std;
class CRijndael
{
public:
enum { ECB=0, CBC=1, CFB=2 };
private:
enum { DEFAULT_BLOCK_SIZE=16 };
enum { MAX_BLOCK_SIZE=32, MAX_ROUNDS=14, MAX_KC=8, MAX_BC=8 };
static int Mul(int a, int b)
{
return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0;
}
static int Mul4(int a, char b[])
{
if(a == 0)
return 0;
a = sm_log[a & 0xFF];
int a0 = (b[0] != 0) ? sm_alog[(a + sm_log[b[0] & 0xFF]) % 255] & 0xFF : 0;
int a1 = (b[1] != 0) ? sm_alog[(a + sm_log[b[1] & 0xFF]) % 255] & 0xFF : 0;
int a2 = (b[2] != 0) ? sm_alog[(a + sm_log[b[2] & 0xFF]) % 255] & 0xFF : 0;
int a3 = (b[3] != 0) ? sm_alog[(a + sm_log[b[3] & 0xFF]) % 255] & 0xFF : 0;
return a0 << 24 | a1 << 16 | a2 << 8 | a3;
}
public:
CRijndael();
virtual ~CRijndael();
void MakeKey(char const* key, char const* chain,
int keylength=DEFAULT_BLOCK_SIZE, int blockSize=DEFAULT_BLOCK_SIZE);
private:
void Xor(char* buff, char const* chain)
{
if(false==m_bKeyInit)
throw exception(sm_szErrorMsg1);
for(int i=0; i<m_blockSize; i++)
*(buff++) ^= *(chain++);
}
void DefEncryptBlock(char const* in, char* result);
void DefDecryptBlock(char const* in, char* result);
public:
void EncryptBlock(char const* in, char* result);
void DecryptBlock(char const* in, char* result);
void Encrypt(char const* in, char* result, size_t n, int iMode=ECB);
void Decrypt(char const* in, char* result, size_t n, int iMode=ECB);
int GetKeyLength()
{
if(false==m_bKeyInit)
throw exception(sm_szErrorMsg1);
return m_keylength;
}
int GetBlockSize()
{
if(false==m_bKeyInit)
throw exception(sm_szErrorMsg1);
return m_blockSize;
}
int GetRounds()
{
if(false==m_bKeyInit)
throw exception(sm_szErrorMsg1);
return m_iROUNDS;
}
void ResetChain()
{
memcpy(m_chain, m_chain0, m_blockSize);
}
public:
static char const* sm_chain0;
private:
static const int sm_alog[256];
static const int sm_log[256];
static const char sm_S[256];
static const char sm_Si[256];
static const int sm_T1[256];
static const int sm_T2[256];
static const int sm_T3[256];
static const int sm_T4[256];
static const int sm_T5[256];
static const int sm_T6[256];
static const int sm_T7[256];
static const int sm_T8[256];
static const int sm_U1[256];
static const int sm_U2[256];
static const int sm_U3[256];
static const int sm_U4[256];
static const char sm_rcon[30];
static const int sm_shifts[3][4][2];
static char const* sm_szErrorMsg1;
static char const* sm_szErrorMsg2;
bool m_bKeyInit;
int m_Ke[MAX_ROUNDS+1][MAX_BC];
int m_Kd[MAX_ROUNDS+1][MAX_BC];
int m_keylength;
int m_blockSize;
int m_iROUNDS;
char m_chain0[MAX_BLOCK_SIZE];
char m_chain[MAX_BLOCK_SIZE];
int tk[MAX_KC];
int a[MAX_BC];
int t[MAX_BC];
};