如何使用PyCrypto添加/更改RSA priv密钥 [英] How add/change password for RSA priv key using PyCrypto

查看:582
本文介绍了如何使用PyCrypto添加/更改RSA priv密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人能够帮助我。
我使用PyCrypto生成一对RSA密钥。公钥和
私钥。
我尝试添加/更改私钥的密码,我不知道如何
这样做。



这是一个的代码。

  #encoding:utf-8 
来自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

#如何添加或更改密码private_alice_key

在M2Crypt中,函数generate pair key RSA.gen_key接受函数回调参数,我可以返回自己的密码。

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

如何在PyCrypto中做。
感谢回复

解决方案

PyCrypto没有函数,可以管理RSA密码。



您可以使用 ezPyCrypto 首页)模块,它是PyCrypto模块的顶层。它具有简单的界面,可让您:




  • 生成,导出和导入公钥和私钥



  • 使用密码保护您的私人密钥

  • 使用安全套接字发送数据创建 b
  • 选择您喜欢的任何公钥大小(2048位推荐)

  • 在RSA和ElGamal之间选择公钥,IDEA,DES3,Blowfish,ARC4,IDEA

  • 使用256位的会话密钥和防御常见的RSA和ElGamal攻击,这会让任何企图侵犯您隐私的人感到沮丧。



使用方式:

  
example7.py
演示使用私钥的密码

import ezPyCrypto

mysecret =不要看在这!!!
raw =这里是一个要加密的字符串

#创建一个键对象
k = ezPyCrypto.key(passphrase = mysecret)

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

#加密此密钥对
enc = k.encString(raw)

#新密钥对象和导入密钥(带密码)
k1 = ezPyCrypto.key(publicAndPrivateKey,passphrase = mysecret)

#解密文本
dec = k.decString )

#test
如果dec == raw:
print使用正确的密码短语成功解密
else:
print失败的地方

print尝试现在与一个坏的密码短语
尝试:
k2 = ezPyCrypto.key(publicAndPrivateKey,passphrase =破解尝试)
,除了ezPyCrypto.CryptoKeyError:
print糟糕 - 我们的微弱破解尝试失败了(这是一件好事)。
else:
print破解尝试成功 - 我们不安全
#我们在 - 让我们掠夺
dec2 = k2.decString(enc)



建立



你看看ezCryptoPy源,那么你会看到密钥实际上是使用BlueFish算法加密/解密:

 #解密密码
blksiz = 8#lazy of me

#为passphrase创建临时对称密码对象 -
#hardwire到Blowfish
ppCipher = Blowfish.new(密码,
Blowfish.MODE_CFB,
self._passIV [0:blksiz])
enclen = len(keyobj)
decpriv =''
i = 0
; enclen:
decbit = ppCipher.decrypt(keyobj [i:i + blksiz])
decpriv + = decbit
i + = blksiz
keyobj = decpriv [0:size]

这意味着,您可以使用以前的代码示例编写自己的密码短语句柄,而不安装ezPyCrypto。这里可以找到很多代码示例,如何自己动手:
Nullege代码搜索



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



python exec()函数和命令行函数ssh-keygen( doc ):



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


maybe somebody be able to help me. I'm using PyCrypto to generate a pair of RSA keys. The public key and private key. I try to add/change a password to the private key, and I do not know how to do it.

This is a piece of my code.

#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

In M2Crypt the function generate pair key RSA.gen_key take give function callback argument and I can return my own password.

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

How to do it in PyCrypto. Thanks for the reply

解决方案

PyCrypto has no function, which can manage RSA passphrase.

Instead, You can use ezPyCrypto (homepage) module, which is built top of PyCrypto module. It has simpler interface and lets you:

  • Generate, export and import public and private keys
  • Encrypt and decrypt strings with ease
  • Optionally create encrypted data as email-friendly text
  • Sign and verify strings (incl. documents)
  • Protect your private key with a passphrase
  • Create 'streams', for sending data through secured sockets
  • Choose any public key size you like (2048-bit recommended)
  • Choose between RSA and ElGamal for public key, and IDEA, DES3, Blowfish, ARC4, IDEA for session key
  • Rest in the comfort of security, with 256-bit session keys and defences against common RSA and ElGamal attacks, which will painfully frustrate anyone seeking to violate your privacy.

Usage:

"""
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)

Build it

If you look into ezCryptoPy source,then you'll see key is actually encrypted/decrypted by using BlueFish algorithm:

   # 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]

That means, you can write your own passphrase handler by using previous code example without installing ezPyCrypto. Here can you find many code examples, how do to it yourself: Nullege code search

My first and alternative solution:

You can use python exec() function and commandline function "ssh-keygen"(doc):

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

这篇关于如何使用PyCrypto添加/更改RSA priv密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆