使用私钥在python中简单加密/解密lib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3815656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simple encrypt/decrypt lib in python with private key
提问by aschmid00
Is there a simple way to encrypt/decrypt a string with a key?
有没有一种简单的方法可以用密钥加密/解密字符串?
Something like:
就像是:
key = '1234'
string = 'hello world'
encrypted_string = encrypt(key, string)
decrypt(key, encrypted_string)
I couldn't find anything simple to do that.
我找不到任何简单的方法来做到这一点。
采纳答案by aaronasterling
http://www.dlitz.net/software/pycrypto/should do what you want.
http://www.dlitz.net/software/pycrypto/应该做你想做的。
Taken from their docs page.
取自他们的文档页面。
>>> from Crypto.Cipher import DES
>>> obj=DES.new('abcdefgh', DES.MODE_ECB)
>>> plain="Guido van Rossum is a space alien."
>>> len(plain)
34
>>> obj.encrypt(plain)
Traceback (innermost last):
File "<stdin>", line 1, in ?
ValueError: Strings for DES must be a multiple of 8 in length
>>> ciph=obj.encrypt(plain+'XXXXXX')
>>> ciph
'1,3Nq4DY7T2pA251s03,0j00272I15w5613dgb/>>> from pyDES import * # pyDes if installed from pip
>>> ciphertext = triple_des('a 16 or 24 byte password').encrypt("secret message", padmode=2) #plain-text usually needs padding, but padmode = 2 handles that automatically
>>> ciphertext
')\xd8\xbfFn#EY\xcbiH\xfa\x18\xb4\xf7\xa2' #gibberish
6'
>>> obj.decrypt(ciph)
'Guido van Rossum is a space alien.XXXXXX'
回答by Himel Das
pyDESis a DES and Triple-DES implementation completely written in python.
pyDES是完全用 Python 编写的 DES 和 Triple-DES 实现。
Here's a simple and portable example that should be secure enough for basic string encryption needs. Just put the pyDES module in the same folder as your program and try it out:
这是一个简单且可移植的示例,对于基本的字符串加密需求来说应该足够安全。只需将 pyDES 模块与您的程序放在同一文件夹中并尝试一下:
Sender's computer
发件人的电脑
>>> from pyDES import *
>>> plain_text = triple_des('a 16 or 24 byte password').decrypt(')\xd8\xbfFn#EY\xcbiH\xfa\x18\xb4\xf7\xa2', padmode=2)
>>> plain_text
"secret message"
Recipient's computer
收件人的电脑
import crypt
import getpass
import os.path
def auth_func():
return (raw_input('Username:'), getpass.getpass('Password:'))
def xor(a,b):
assert len(b) >= len(a)
return "".join([chr( ord(a[i]) ^ ord(b[i])) for i in range(len(a))])
# create a new credentials file if needed
if not os.path.exists('cred'):
with open('cred', 'w') as f:
user, pwd = auth_func()
f.write ("{}\n".format(user))
f.write ("{}\n".format(xor(pwd, crypt.crypt('secret', 'words'))))
f.close()
# read credentials and print user/password
with open('cred', 'r') as f:
user, pwd = f.read().split('\n')[:2]
print user
print xor(pwd, crypt.crypt('secret', 'words'))
回答by andrew cooke
for python 2, you should use keyczar http://www.keyczar.org/
对于 python 2,你应该使用 keyczar http://www.keyczar.org/
for python 3, until keyczar is available, i have written simple-crypt http://pypi.python.org/pypi/simple-crypt
对于 python 3,在 keyczar 可用之前,我写了 simple-crypt http://pypi.python.org/pypi/simple-crypt
i'm answering this two years late as things have changed since the question was asked.
我回答这个问题晚了两年,因为自从提出这个问题以来情况发生了变化。
note that the previous answers to this question use weak ciphers (by today's standards) and don't have any key strengthening. the two recommendations here are likely more secure.
请注意,此问题的先前答案使用弱密码(按照今天的标准)并且没有任何密钥强化。这里的两个建议可能更安全。
回答by dsamersoff
The simplest and fastest way to encrypt short text fragment with preshaired key is use one of cryptohash functions (md5, sha etc).
使用 preshaired 密钥加密短文本片段的最简单和最快的方法是使用加密哈希函数之一(md5、sha 等)。
i.e. calc md5 of your key, then xor your string fragment with this md5 hash. if you need to encode text fragmen longer than length of md5 - do md5(md5 hash) and encrypt next fragment.
即计算你的密钥的 md5,然后用这个 md5 哈希对你的字符串片段进行异或。如果您需要对长度超过 md5 的文本片段进行编码 - 执行 md5(md5 hash) 并加密下一个片段。
Security of this solution is worse than with 3-DES but enough in average case (i.e. to store not very secure password in config file) and it doesn't requre anything besides base python distro.
这个解决方案的安全性比 3-DES 差,但在一般情况下足够了(即在配置文件中存储不是很安全的密码),除了基本的 python 发行版之外,它不需要任何东西。
If you need better security - look for one of AES, Blowfish etc implementation, but to really benefit AES you need to do some additional work to mix your data with random ones.
如果您需要更好的安全性 - 寻找 AES、Blowfish 等实现之一,但要真正使 AES 受益,您需要做一些额外的工作来将您的数据与随机数据混合。
回答by Alan Marchiori
To expand on dsamersoff's answer.. This is simple and insecure, but for certain tasks may be useful. here's some code:
扩展 dsamersoff 的答案.. 这很简单且不安全,但对于某些任务可能有用。这是一些代码:
##代码##
