Crypto.Random可以用来安全地生成一个强密钥和一个初始化向量吗?
这里的情况我有一个PHP脚本使用aes256,CBC密钥和IV大小都是32字节长
data= '123456789abcdef'
from Crypto.Cipher import AES
a = AES.new('oqufXQ(?bc=6_hR2I3sMZChDpb6dDlw4',2,'fOaiIOkD8*9Xeu_s4_bb87Ox_UG+D9GA')
print a.encrypt(data)
Run Code Online (Sandbox Code Playgroud)
和我得到的错误
<type 'exceptions.ValueError'>: IV must be 16 bytes long
Traceback (most recent call last):
File "/base/data/home/apps/s~xxxxxxx/1.155074369696961822/main.py", line 4, in <module>
Run Code Online (Sandbox Code Playgroud)
php代码有效
echo base64_encode(encrypt('0123456789abcdef')) ;
function encrypt($data)
{
return mcrypt_encrypt(MCRYPT_RIJNDAEL_256 ,'oqufXQ(?bc=6_hR2I3sMZChDpb6dDlw4', $data , MCRYPT_MODE_CBC, utf8_encode('fOaiIOkD8*9Xeu_s4_bb87Ox_UG+D9GA') );
}
Run Code Online (Sandbox Code Playgroud)
我不能改变IV大小
请注意,我不太熟悉Python,只需要一种加密数据的方法,因为它将是一个引擎.
仍然无法让这个工作.我的问题是关于如何使解密线工作.这是我写的:
class IVCounter(object):
@staticmethod
def incrIV(self):
temp = hex(int(self, 16)+1)[2:34]
return array.array('B', temp.decode("hex")).tostring()
def decryptCTR(key, ciphertext):
iv = ciphertext[:32] #extracts the first 32 characters of the ciphertext
#convert the key into a 16 byte string
key = array.array('B', key.decode("hex")).tostring()
print AES.new(key, AES.MODE_CTR, counter=IVCounter.incrIV(iv)).decrypt(ciphertext)
return
Run Code Online (Sandbox Code Playgroud)
我的错误信息是:
ValueError:'counter'参数必须是可调用对象
我只是无法弄清楚pycrypto是如何让我将第三个参数组织成新的.
有人可以帮忙吗?谢谢!
在实施以下建议后编辑新代码.还是卡住了!
class IVCounter(object):
def __init__(self, start=1L):
print start #outputs the number 1 (not my IV as hoped)
self.value = long(start)
def __call__(self):
print self.value #outputs 1 - need this to …Run Code Online (Sandbox Code Playgroud) 我使用PyCrypto模块为消息生成密文.
>>> a=AES.new("1234567890123456")
>>> m='aaaabbbbccccdddd'
>>> a.encrypt(m)
'H\xe7\n@\xe0\x13\xe0M\xc32\xce\x16@\xb2B\xd0'
Run Code Online (Sandbox Code Playgroud)
我希望这个输出像一个 hashlib
>>> from hashlib import sha1
>>> sha1(m).hexdigest()
'68b69b51da162fcf8eee65641ee867f02cfc9c59'
Run Code Online (Sandbox Code Playgroud)
也就是说,我需要一个干净的字符串而不是带有十六进制标记\x和字符串的字符串.
PyCrypto有没有办法实现这一目标?
如果是,如何执行加密和解密?
如果不是,我怎样才能将字符串转换为我需要的字符串?
想知道通过openssl将AES_128_CTR加密转换为PyCrypto的正确方法.
首先,我通过openssl进行了加密,如下所示:
openssl enc -aes-128-ctr -in input.mp4 -out output.openssl.mp4 -K 7842f0a1ebc38f44e3e0c81943f68582 -iv d01f40dfc8ec8cd9
Run Code Online (Sandbox Code Playgroud)
然后,我尝试通过PyCrypto做同样的事情:
from Crypto.Cipher import AES
from Crypto.Util import Counter
key = '7842f0a1ebc38f44e3e0c81943f68582'
iv = 'd01f40dfc8ec8cd9'
ctr_e = Counter.new(128, initial_value=int(iv, 16))
encryptor = AES.new(key.decode('hex'), AES.MODE_CTR, counter=ctr_e)
with open('output.pycrypto.mp4', 'wb') as fout:
with open('input.mp4', 'rb') as fin:
fout.write(encryptor.encrypt(fin.read()))
Run Code Online (Sandbox Code Playgroud)
我认为他们应该是相似的,但它不是:
diff output.openssl.mp4 output.pycrypto.mp4
Binary files output.openssl.mp4 and output.pycrypto.mp4 differ
Run Code Online (Sandbox Code Playgroud) 我需要能够处理一些具有高值的unicode字符,所以我重新安装了Python 2.7.10 with option --enable-unicode=ucs4 --prefix("wide-build").
然后我开始收到以下错误:
...
from Crypto.Cipher import _ARC4
ImportError: /home/fast/usr/local/lib/python2.7/site-packages/Crypto/Cipher/_ARC4.so: undefined symbol: PyUnicodeUCS2_FromString
Run Code Online (Sandbox Code Playgroud)
我意识到undefined symbol: PyUnicodeUCS2_FromString必须是因为新的构建,所以我尝试重新安装一切新的(新的Python和新的pip和新安装的库).我仍然有同样的错误.
是不是可以使用CryptoPython广泛构建的库?
我找不到任何关于此的文件.有没有已知的解决方法?
感谢您的任何帮助!
我正在使用pycrypto模块进行AES加密。使用文档,我记下了以下函数,但是它IV must be 16 bytes long总是会出错,但是我使用的是16字节长的IV。
def aes_encrypt(plaintext):
"""
"""
key = **my key comes here**
iv = binascii.hexlify(os.urandom(16)) # even used without binascii.hexlify)
aes_mode = AES.MODE_CBC
obj = AES.new(key, aes_mode, iv)
ciphertext = obj.encrypt(plaintext)
return ciphertext
Run Code Online (Sandbox Code Playgroud) [我是Python 2.7和AWS Lambda的新手,感谢任何帮助]
我按照AWS Lambda教程创建了一个virtualenv,以包含与使用paramiko相关联的Python库,将文件作为AWS Lambda上的计划任务复制到SFTP服务器,以运行以下脚本:
import paramiko
def worker_handler(event, context):
host = "sftpserver.testdpom.com"
port = 22
transport = paramiko.Transport((host, port))
sftp = paramiko.SFTPClient.from_transport(transport)
username = "xxxx"
password = "xxxxxx"
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put("test.txt", "test.txt")
sftp.close()
transport.close()
return
{
'message' : "Script execution completed. See Cloudwatch logs for complete output"
}
Run Code Online (Sandbox Code Playgroud)
python脚本在我的本地机器上正常工作但是当我在AWS Lambda上测试包时,我得到错误"ImportError:No module named _constant_time"和下面的堆栈跟踪.
您能否在AWS Lambda环境中考虑此错误的任何可能原因?
File "/var/task/paramiko/kex_group1.py", line 111, in _parse_kexdh_reply
self.transport._verify_key(host_key, sig)
File "/var/task/paramiko/transport.py", line 1617, in _verify_key …Run Code Online (Sandbox Code Playgroud) 我知道一个与此非常类似的问题(如何在Python中加密并在Java中解密?)但我有一个不同的问题.
我的问题是,我无法正确解密Java.尽管使用了正确的密钥和IV,我仍然在解密后获得垃圾字符.我在Java中没有任何编译/运行时错误或异常,因此我相信我正在使用正确的参数进行解密.
Python加密代码 -
from Crypto.Cipher import AES
import base64
key = '0123456789012345'
iv = 'RandomInitVector'
raw = 'samplePlainText'
cipher = AES.new(key,AES.MODE_CFB,iv)
encrypted = base64.b64encode(iv + cipher.encrypt(raw))
Run Code Online (Sandbox Code Playgroud)
Java解密代码 -
private static String KEY = "0123456789012345";
public static String decrypt(String encrypted_encoded_string) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
String plain_text = "";
try{
byte[] encrypted_decoded_bytes = Base64.getDecoder().decode(encrypted_encoded_string);
String encrypted_decoded_string = new String(encrypted_decoded_bytes);
String iv_string = encrypted_decoded_string.substring(0,16); //IV is retrieved correctly.
IvParameterSpec iv = new IvParameterSpec(iv_string.getBytes());
SecretKeySpec skeySpec = new …Run Code Online (Sandbox Code Playgroud) pycrypot已安装(当我运行pip list结果之一是时pycrypto (2.6.1))
它可以工作,但是当我想使用MODE_CCM它时返回:module 'Crypto.Cipher.AES' has no attribute 'MODE_CCM'
有什么线索吗?
我的Python版本: Python 3.5.2 :: Anaconda 4.2.0 (x86_64)
pycrypto ×10
python ×10
encryption ×4
aes ×3
aws-lambda ×1
cfb-mode ×1
java ×1
openssl ×1
packages ×1
paramiko ×1
python-2.7 ×1
unicode ×1