当前位置:首页 » 编程语言 » 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 的运用

问题可以关闭了

热点内容
上传文件文件夹找不到 发布:2024-09-20 00:26:32 浏览:914
承台箍筋加密区 发布:2024-09-20 00:26:31 浏览:227
笔记本什么配置能流畅运行cf 发布:2024-09-20 00:14:19 浏览:951
实测华为编译器 发布:2024-09-19 23:50:52 浏览:821
linux汇总 发布:2024-09-19 23:46:39 浏览:452
阿里云服务器环境搭建教程 发布:2024-09-19 23:21:58 浏览:837
黄色文件夹图标 发布:2024-09-19 23:19:22 浏览:684
mysql数据库导出导入 发布:2024-09-19 23:00:47 浏览:183
lua脚本精灵 发布:2024-09-19 23:00:41 浏览:659
任务栏文件夹图标 发布:2024-09-19 22:54:25 浏览:101