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

pythonaes

發布時間: 2022-01-09 10:01:16

python 中 crypto 的aes加密怎麼使用

在剛開始知道這個模塊的時候,連基本的Crypto模塊的安裝都花了很多很多時間來搞,也不知道什麼情況反正是折騰很久了才安裝起的,記得是包安裝起來了,但使用的時候始終提示找不到Crypto.Cipher模塊。然後怎麼解決的呢?
一、把我的python換成了64位的,本來電腦就是64位的也不知道之前是啥情況安裝成32位的了。(O(∩_∩)O哈哈~)
二、安裝了VCForPython27.msi
三、在cmd中執行:
pip install pycrypto -i http://mirrors.aliyun.com/pypi/simple/1

經過上邊兒的幾個步驟,我是能夠成功執行
from Crypto.Cipher import AES1

現在上一個實例代碼:
# !/usr/bin/env python
# coding: utf-8
'''

'''

from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex

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

def myencrypt(self, text):
length = 16
count = len(text)
print count
if count < length:
add = length - count
text= text + ('\0' * add)

elif count > length:
add = (length -(count % length))
text= text + ('\0' * add)

# print len(text)
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
self.ciphertext = cryptor.encrypt(text)
return b2a_hex(self.ciphertext)

def mydecrypt(self, text):
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
plain_text = cryptor.decrypt(a2b_hex(text))
return plain_text.rstrip('\0')

if __name__ == '__main__':
mycrypt = MyCrypt('abcdefghjklmnopq')
e = mycrypt.myencrypt('hello,world!')
d = mycrypt.mydecrypt(e)
print e
print d
0414243

在cmd中執行結果:

㈡ 如何使用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];
};

㈢ Python問題,AES加密,當需要加密的字元串不足16位時,試編寫代碼用空格來補充位置

Python問題,AES加密,當需要加密的字元串不足

㈣ 如何在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"

㈤ python aes里iv是什麼意思

就是一種加密方式。。。網路告訴我們這是目前世界上最強的。。。

㈥ python aes模塊怎麼導入

把自寫模塊和當前的python文件放在同一個文件夾,然後通過如下語句導入: from . import mole_name

㈦ python中AES 用ECB模式加密之後為什麼和C#加密之後的結果不一樣

AES是美國國家標准技術研究所NIST旨在取代DES的21世紀的加密標准。 AES的基本要求是,採用對稱分組密碼體制,密鑰長度的最少支持為128、192、256,分組長度128位,演算法應易於各種硬體和軟體實現。1998年NIST開始AES第一輪分析、測試和徵集

㈧ 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'

㈨ python aes 可以加密json 串嗎

json就是個字元串 。就算各種語言提供了json相關的依賴包 ,那還是可以轉換為字元串,既然是字元串 、哪加密就不是問題了吧,轉回來先解密然後再轉json對象 。

㈩ 關於AES加密解密python 的運用

問題可以關閉了

熱點內容
javarest 發布:2024-09-19 09:28:43 瀏覽:753
密碼子的原料是什麼 發布:2024-09-19 09:11:42 瀏覽:348
半夜編程 發布:2024-09-19 09:11:36 瀏覽:104
海康威視存儲卡質量如何 發布:2024-09-19 08:55:35 瀏覽:941
python3默認安裝路徑 發布:2024-09-19 08:50:22 瀏覽:517
環衛視頻拍攝腳本 發布:2024-09-19 08:35:44 瀏覽:419
sqlserveronlinux 發布:2024-09-19 08:16:54 瀏覽:257
編程常數 發布:2024-09-19 08:06:36 瀏覽:953
甘肅高性能邊緣計算伺服器雲空間 發布:2024-09-19 08:06:26 瀏覽:163
win7家庭版ftp 發布:2024-09-19 07:59:06 瀏覽:717