如何使用PyCrypto添加/更改RSA priv密码的密码

Grz*_*orz 6 python cryptography rsa

也许有人能够帮助我.我正在使用PyCrypto生成一对RSA密钥.公钥和私钥.我尝试将密码添加/更改为私钥,我不知道该怎么做.

这是我的一段代码.

#encoding:utf-8
from Crypto.PublicKey import RSA

pass_alice='ala'
private_alice_key = RSA.generate(1024)
public_alice_key  = private_alice_key.publickey()

str_priv = private_alice_key.exportKey()
str_pub  = public_alice_key.exportKey()

print str_priv
print str_pub

# HOW ADD OR CHANGE PASSWORD FOR private_alice_key
Run Code Online (Sandbox Code Playgroud)

在M2Crypt中函数生成对密钥RSA.gen_key取给函数回调参数我可以返回自己的密码.

#example in M2Crypt:
from M2Crypto import RSA
key = RSA.gen_key(1024, 6528, lambda pass:'my_password')
Run Code Online (Sandbox Code Playgroud)

如何在PyCrypto中做到这一点.谢谢回复

tim*_*luz 2

PyCrypto 没有可以管理 RSA 密码的功能。

相反,您可以使用ezPyCrypto主页)模块,它构建在 PyCrypto 模块之上。它具有更简单的界面,让您:

  • 生成、导出和导入公钥和私钥
  • 轻松加密和解密字符串
  • 可选择将加密数据创建为电子邮件友好的文本
  • 签署并验证字符串(包括文档)
  • 使用密码保护您的私钥
  • 创建“流”,用于通过安全套接字发送数据
  • 选择您喜欢的任何公钥大小(推荐 2048 位)
  • 选择 RSA 和 ElGamal 作为公钥,选择 IDEA、DES3、Blowfish、ARC4、IDEA 作为会话密钥
  • 借助 256 位会话密钥以及针对常见 RSA 和 ElGamal 攻击的防御措施,让您安心无忧,这将极大地挫败任何试图侵犯您隐私的人。

用法:

"""
example7.py
Demonstrate the use of passphrases with private keys
"""
import ezPyCrypto

mysecret = "Don't look at this!!!"
raw = "Here is a string to encrypt"

# Create a key object
k = ezPyCrypto.key(passphrase=mysecret)

# Export public/private key
publicAndPrivateKey = k.exportKeyPrivate()

# Encrypt against this keypair
enc = k.encString(raw)

# Create a new key object, and import keys (with passphrase)
k1 = ezPyCrypto.key(publicAndPrivateKey, passphrase=mysecret)

# Decrypt text
dec = k.decString(enc)

# test
if dec == raw:
    print "Successful decryption using correct passphrase"
else:
    print "Failed somewhere"

print "Trying now with a bad passphrase"
try:
    k2 = ezPyCrypto.key(publicAndPrivateKey, passphrase="cracking attempt")
except ezPyCrypto.CryptoKeyError:
    print "Oops - our feeble cracking attempt failed (which is a good thing)."
else:
    print "Cracking attempt succeeded - we're not safe"
    # We're in - let's plunder
    dec2 = k2.decString(enc)
Run Code Online (Sandbox Code Playgroud)

建造它

如果你查看 ezCryptoPy 源代码,那么你会发现密钥实际上是使用 BlueFish 算法加密/解密的:

   # decrypt against passphrase
        blksiz = 8 # lazy of me

        # create temporary symmetric cipher object for passphrase - 
        #hardwire to Blowfish
        ppCipher = Blowfish.new(passphrase,
                                Blowfish.MODE_CFB,
                                self._passIV[0:blksiz])
        enclen = len(keyobj)
        decpriv = ''
        i = 0
        while i < enclen:
            decbit = ppCipher.decrypt(keyobj[i:i+blksiz])
            decpriv += decbit
            i += blksiz
        keyobj = decpriv[0:size]
Run Code Online (Sandbox Code Playgroud)

这意味着,您可以使用前面的代码示例编写自己的密码处理程序,而无需安装 ezPyCrypto。这里你可以找到很多代码示例,自己如何做: Nullege代码搜索

我的第一个也是替代解决方案:

您可以使用 python exec()函数和命令行函数“ssh-keygen”( doc ):

ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]