def _testGenerateKey(self, privKey, pubKey, privData, pubData, keyType): self.assertEquals(keys.makePublicKeyString(pubKey, 'comment', keyType), pubData) self.assertEquals(keys.makePublicKeyString(privKey, 'comment', keyType), pubData) self.assertEquals(keys.makePrivateKeyString(privKey, kind=keyType), privData) encData = keys.makePrivateKeyString(privKey, passphrase='test', kind=keyType) self.assertEquals( keys.getPrivateKeyObject(data = encData, passphrase = 'test').__getstate__(), privKey.__getstate__())
def _testGenerateKey(self, privKey, pubKey, privData, pubData, keyType): self.assertEquals(keys.makePublicKeyString(pubKey, 'comment', keyType), pubData) self.assertEquals( keys.makePublicKeyString(privKey, 'comment', keyType), pubData) self.assertEquals(keys.makePrivateKeyString(privKey, kind=keyType), privData) if keyType != 'lsh': encData = keys.makePrivateKeyString(privKey, passphrase='test', kind=keyType) self.assertEquals( keys.getPrivateKeyObject(data=encData, passphrase='test').__getstate__(), privKey.__getstate__())
def _saveKey(key, options): if not options['filename']: kind = keys.objectType(key) kind = {'ssh-rsa':'rsa','ssh-dss':'dsa'}[kind] filename = os.path.expanduser('~/.ssh/id_%s'%kind) options['filename'] = raw_input('Enter file in which to save the key (%s): '%filename).strip() or filename if os.path.exists(options['filename']): print '%s already exists.' % options['filename'] yn = raw_input('Overwrite (y/n)? ') if yn[0].lower() != 'y': sys.exit() if not options['pass']: while 1: p1 = getpass.getpass('Enter passphrase (empty for no passphrase): ') p2 = getpass.getpass('Enter same passphrase again: ') if p1 == p2: break print 'Passphrases do not match. Try again.' options['pass'] = p1 comment = '%s@%s' % (getpass.getuser(), socket.gethostname()) open(options['filename'], 'w').write( keys.makePrivateKeyString(key, passphrase=options['pass'])) os.chmod(options['filename'], 33152) open(options['filename']+'.pub', 'w').write( keys.makePublicKeyString(key, comment = comment)) pubKey = keys.getPublicKeyString(data=keys.makePublicKeyString(key, comment=comment)) print 'Your identification has been saved in %s' % options['filename'] print 'Your public key has been saved in %s.pub' % options['filename'] print 'The key fingerprint is:' print ':'.join(['%02x' % ord(x) for x in md5.new(pubKey).digest()])
def getRSAKeys( ): if not (os.path.exists('public.key') and os.path.exists('private.key')): # generate a RSA keypair print "Generating RSA keypair..." from Crypto.PublicKey import RSA KEY_LENGTH = 1024 rsaKey = RSA.generate(KEY_LENGTH, common.entropy.get_bytes) publicKeyString = keys.makePublicKeyString(rsaKey) privateKeyString = keys.makePrivateKeyString(rsaKey) # save keys for next time file('public.key', 'w+b').write(publicKeyString) file('private.key', 'w+b').write(privateKeyString) print "done." else: publicKeyString = file('public.key').read( ) privateKeyString = file('private.key').read( ) return publicKeyString, privateKeyString
def getRSAKeys(): if not (os.path.exists(‘public.key’) and os.path.exists(‘private.key’)): # generate a RSA keypair print "Generating RSA keypair…" from Crypto.PublicKey import RSA KEY_LENGTH = 1024 rsaKey = RSA.generate(KEY_LENGTH, common.entropy.get_bytes) publicKeyString = keys.makePublicKeyString(rsaKey) privateKeyString = keys.makePrivateKeyString(rsaKey) # save keys for next time file(‘public.key’, ‘w+b’).write(publicKeyString) file(‘private.key’, ‘w+b’).write(privateKeyString) print "done."
def getRSAKeys(): if not (os.path.exists('public.key') and os.path.exists('private.key')): # generate a RSA keypair print "Generating RSA keypair..." from Crypto.PublicKey import RSA KEY_LENGTH = 1024 rsaKey = RSA.generate(KEY_LENGTH, common.entropy.get_bytes) publicKeyString = keys.makePublicKeyString(rsaKey) privateKeyString = keys.makePrivateKeyString(rsaKey) # save keys for next time file('public.key', 'w+b').write(publicKeyString) file('private.key', 'w+b').write(privateKeyString) print "done." else: publicKeyString = file('public.key').read() privateKeyString = file('private.key').read() return publicKeyString, privateKeyString
def keygen(filepath): key = RSA.generate(1024, common.entropy.get_bytes) # Create and write the private key file. # . Generate the string. privk = keys.makePrivateKeyString(key) # . Write the file privf = open(filepath, 'w') privf.write(privk) privf.close() # . Fix the permissions os.chmod(filepath, 33152) # Create and write the public key file. # . Generate the string. pubk = keys.makePublicKeyString(key) # . Write the file. pubf = open('%s.pub' % filepath, 'w') pubf.write(pubk) pubf.close()
if e.args[0] != 'encrypted key with no passphrase': raise else: if not options['pass']: options['pass'] = getpass.getpass('Enter old passphrase: ') key = keys.getPrivateKeyObject(options['filename'], passphrase = options['pass']) if not options['newpass']: while 1: p1 = getpass.getpass('Enter new passphrase (empty for no passphrase): ') p2 = getpass.getpass('Enter same passphrase again: ') if p1 == p2: break print 'Passphrases do not match. Try again.' options['newpass'] = p1 open(options['filename'], 'w').write( keys.makePrivateKeyString(key, passphrase=options['newpass'])) print 'Your identification has been saved with the new passphrase.' def displayPublicKey(options): if not options['filename']: filename = os.path.expanduser('~/.ssh/id_rsa') options['filename'] = raw_input('Enter file in which the key is (%s): ' % filename) try: key = keys.getPrivateKeyObject(options['filename']) except keys.BadKeyError, e: if e.args[0] != 'encrypted key with no passphrase': raise else: if not options['pass']: options['pass'] = getpass.getpass('Enter passphrase: ') key = keys.getPrivateKeyObject(options['filename'], passphrase = options['pass'])