我需要一个支持PEM文件以及RSA签名和DES3加密的Python库.pycrypto似乎不支持PEM,它的加载现有密钥的机制没有文档和含义.m2crypto似乎不支持DES/DES3,奇怪的是.
我一直在运行一个openssl子进程,但我宁愿有一些内置的东西,最好是快速的.这存在吗?
(如果不这样,我就会犹豫不决,但是有没有足够高级别的C apis,我可以编写一个特殊用途的扩展而不会自杀/引入漏洞?)
我试图让2个程序使用公钥通过网络共享加密数据,但我遇到了一个困难的问题:共享的信息(密钥和/或加密数据)似乎被修改.我希望尽可能简单地保持加密数据格式和密钥格式,以便与其他语言兼容.为了解决这个问题,我创建了两个程序:Keyreceive和Keysend.它们按此顺序执行:
Keysend.py
import socket
import os
from Crypto.PublicKey import RSA
from Crypto import Random
rng = Random.new().read
RSAkey = RSA.generate(1024, rng)
privatekey = RSAkey
publickey = RSAkey.publickey()
print(privatekey.exportKey()) #export under the 'PEM' format (I think)
print(publickey.exportKey())
file = open("Keys.txt", "w")
file.write(privatekey.exportKey()) #save exported private key
file.close()
data = "hello world"
enc_data = publickey.encrypt(data, 16) #encrypt message with public key
print(str(enc_data))
host = "localhost"
port = 12800
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion.connect((host, port))
connexion.send(str(enc_data)) # send …Run Code Online (Sandbox Code Playgroud) 它显示UnicodeError: 'utf8' codec can't decode byte 0x82 in position 0: unexpected code byte
这是代码:
from Crypto.Cipher import AES
import simplejson as json
key = '0123456789abcdef'
mode = AES.MODE_CBC
encryptor = AES.new(key, mode)
text = '1010101010101010'
json.dumps(encryptor.encrypt(text))
Run Code Online (Sandbox Code Playgroud)
如何避免这个错误?
提前致谢!
所以,我正在使用Pycrypto进行AES加密,现在我将Python 2.7代码移植到Python 3.4.我遇到了以下错误,我无法理解:
TypeError : argument must be read-only pinned buffer, not bytearray.
Run Code Online (Sandbox Code Playgroud)
当我试图显然加密变量的内容时会发生这种情况.确切的行是:
token = b"\0" * 16
final_token = cipher.encrypt(token)
Run Code Online (Sandbox Code Playgroud)
(令牌显然不是000 ......但我正在简化)
我已经在互联网上搜索了一个解决方案,发现这个错误在python 2.6中的websocket lib中发生了很多,但解决方案(使用memoryview(令牌)而不是令牌)没有帮助.有人能解释那里发生了什么吗?我很失落......
我正在尝试在GAE Python中实现Google Identity Toolkit(gitkitv3).用户登录网站后,我收到以下错误:
'PKCS12 format is not supported by the PyCrpto library. '
NotImplementedError: PKCS12 format is not supported by the PyCrpto library. Try converting to a "PEM" (openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > privatekey.pem) or using PyOpenSSL if native code is an option.
Run Code Online (Sandbox Code Playgroud)
基于SO回复,我在x.p12文件上运行以下命令,并使用生成的privatekey.pem文件:
openssl pkcs12 -passin pass:notasecret -in x.p12 -nocerts -passout pass:notasecret -out key.pem
openssl pkcs8 -nocrypt -in key.pem -passin pass:notasecret -topk8 -out privatekey.pem
Run Code Online (Sandbox Code Playgroud)
现在,我收到以下错误:
'X509 certs are not supported by the PyCrypto library. …Run Code Online (Sandbox Code Playgroud) google-app-engine pycrypto oauth-2.0 oauth2client google-oauth
我现在知道RSA公钥/私钥只能一次加密非常短的输入,但任何人都可以提供一种方法来加密任何类型的文件(.txt,.phf,.exe等)只有公钥/私钥?我不想要额外的AES密钥.
这是我的代码,在使用公钥和私钥对进行加密和解密后,我没有收到原始内容.我不关心加密或解密的安全性,我只想让简单的加密解密工作在它可能需要的任何输入上,无论它有多长或多大.
from Crypto.PublicKey import RSA
from Crypto import Random
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
public_key = key.publickey()
f = open('C:\Users\Administrator\Desktop\jack.txt','r').read()
print 'original content: '+ f
enc_data = public_key.encrypt(f, 32)
print 'encrypted data: '
print enc_data
dec_data = key.decrypt(enc_data)
print 'decrypted data: '+ dec_data
Run Code Online (Sandbox Code Playgroud)
这是输出:
original content: Python Cryptography Toolkit
A collection of cryptographic modules implementing various algorithms and protocols.
Subpackages:
Crypto.Cipher
Secret-key (AES, DES, ARC4) and public-key encryption (RSA PKCS#1) algorithms
Crypto.Hash
Hashing algorithms (MD5, SHA, HMAC) …Run Code Online (Sandbox Code Playgroud) 我需要简单地加密python中的一些文本,并能够在JavaScrypt中解密.
到目前为止我在python中:
from Crypto import Random
from Crypto.Cipher import AES
import base64
BLOCK_SIZE = 16
key = "1234567890123456" # want to be 16 chars
textToEncrypt = "This is text to encrypt"
def encrypt(message, passphrase):
# passphrase MUST be 16, 24 or 32 bytes long, how can I do that ?
IV = Random.new().read(BLOCK_SIZE)
aes = AES.new(passphrase, AES.MODE_CFB, IV)
return base64.b64encode(aes.encrypt(message))
def decrypt(encrypted, passphrase):
IV = Random.new().read(BLOCK_SIZE)
aes = AES.new(passphrase, AES.MODE_CFB, IV)
return aes.decrypt(base64.b64decode(encrypted))
print encrypt( textToEncrypt, key )
Run Code Online (Sandbox Code Playgroud)
这是产生文字: ZF9as5JII5TlqcB5tAd4sxPuBXd5TrgE …
我不知道为什么当我使用PyCrypto(Crypto.Cipher-AES)在AES中加密文本时,结果与C中的代码生成的密文不同.
例如,以下代码给了我
99756ed0115f676cef45ae25937bfd63247358a80803dde3fc1eae4953ee7277
Run Code Online (Sandbox Code Playgroud)
代替
CC613A0BDC930DABEA7A26126CE489EA
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
key = '1F61ECB5ED5D6BAF8D7A7068B28DCC8E'
IV = 16 * '\x00'
mode = AES.MODE_CBC
encryptor = AES.new(key, mode, IV=IV)
text = '020ABC00ABCDEFf8d500000123456789'
ciphertext = encryptor.encrypt(text)
print binascii.hexlify(ciphertext)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用python中的crypto库生成一个大的素数(2048位)以实现RSA.但是,我并不真正理解该getPrime()函数的语法.我目前有:
from Crypto.Util import number
n_length = 2048
primeNum = number.getPrime(n_length, randFunc)
Run Code Online (Sandbox Code Playgroud)
我不明白randFunc该getPrime功能应该是什么.
我是密码学的全新人物.我想从服务器端生成RSA密钥对,并将其发送给所有客户端(浏览器).但在此之前,我只是通过加密python中的数据并通过pubnub发送到index.html文件并尝试在JavaScript中解密来测试场景.问题是当我做加密时;
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
print key.exportKey() #<--private key
public_key = key.publickey()
print public_key.exportKey() #<--public key
msg = "hello"
enc_data = public_key.encrypt(msg, 32)
print '----ENCRYPTED DATA----'
enc = enc_data[0]
Run Code Online (Sandbox Code Playgroud)
并发送加密数据enc,它给我这个错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc4 in position 2: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)
我试着把它转换成
enc = base64.b64encode(enc_data[0])
Run Code Online (Sandbox Code Playgroud)
它发送没有错误.但JS解密方法获取无
var enc_from_python = $('#input').val();
console.log("ENCRYPTED data:", enc_from_python);
var decrypt = new JSEncrypt();
decrypt.setPrivateKey($('#privkey').val());
var uncrypted = decrypt.decrypt(enc_from_python);
console.log(">>>",uncrypted); //<-- this is None ! why ?
Run Code Online (Sandbox Code Playgroud)
这两个代码都可以自己进行加/减.我还尝试使用python中收到的密钥对来加密/解析JS中的数据,这很有效.我想问题是来自Pycrypto的编码数据的unicode编码格式不匹配.谁能告诉我在这里我错过了什么.
Python的完整代码:
import time …Run Code Online (Sandbox Code Playgroud) pycrypto ×10
python ×9
cryptography ×3
encryption ×3
rsa ×2
aes ×1
cryptojs ×1
file ×1
google-oauth ×1
javascript ×1
jsencrypt ×1
m2crypto ×1
networking ×1
oauth-2.0 ×1
oauth2client ×1
primes ×1
python-3.x ×1
random ×1
types ×1