當前位置:首頁 » 編程語言 » pythonaesecb

pythonaesecb

發布時間: 2024-10-22 17:30:45

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];
};

熱點內容
用自己電腦當伺服器做網站 發布:2024-10-22 19:46:47 瀏覽:726
安卓抖音瀏覽記錄在哪裡看 發布:2024-10-22 19:29:33 瀏覽:388
python調用靜態方法調用 發布:2024-10-22 19:23:51 瀏覽:50
php添加文本框 發布:2024-10-22 19:08:08 瀏覽:484
oracle存儲過程定時執行 發布:2024-10-22 19:08:07 瀏覽:555
vbnet讀取資料庫數據 發布:2024-10-22 19:06:23 瀏覽:183
2012文件伺服器搭建 發布:2024-10-22 19:04:18 瀏覽:970
悅動悅目有哪些配置 發布:2024-10-22 18:54:53 瀏覽:661
BGL編譯器手冊 發布:2024-10-22 18:54:51 瀏覽:378
linux使用命令 發布:2024-10-22 18:45:43 瀏覽:254