Hi
I would like to write the script that produces at least 6 encrypted password using crypt module and save them in a file called password.txt
but I get error?

and How can I add more than 6 encrypted password ?

import crypt
import random, string
password = input("Please ender your password? ")

def getsalt(chars = string.letters + string.digits):
    # generate a random 2-character 'salt'
    return random.choice(chars) + random.choice(chars)

print crypt.crypt(password, getsalt())

output


Please ender your password? python
Traceback (most recent call last):
  File "./crypt.py", line 4, in <module>
    password = input("Please ender your password? ")
  File "<string>", line 1, in <module>
NameError: name 'python' is not defined

Recommended Answers

All 9 Replies

That error means you're on Python 2.7.3 or Python 2.7.2; the thing is that there's a difference between the function 'input()' and the funtion raw_input() (raw_input() is prior to Python 3.x). input() needs a number to be inserted, otherwise it will throw an error if strings are inserted (which is your case), but raw_input() will take anything, and it will return a string. So, what you'll need is to change input() into raw_input():

import crypt
import random, string
password = raw_input("Please ender your password? ")
def getsalt(chars = string.letters + string.digits):
    # generate a random 2-character 'salt'
    return random.choice(chars) + random.choice(chars)
print crypt.crypt(password, getsalt())

Ohhh, thank you so much
By the way How can I add more than 6 encrypted password and write it to password.txt?

I get error?

def password():
    passwordlist = 6*[None]
    passwordnr = 0
    while passwordnr < 6:
        passwordlist[passwordnr] = input("Enter your password?: \n")
        passwordnr +=1
    return passwordlist
p = password()

I wonder if its right? I need my encryption password also write to file?

#!/usr/bin/python
import crypt
import random, string


def getsalt(chars = string.letters + string.digits):
    # generate a random 2-character 'salt'
    return random.choice(chars) + random.choice(chars)

password = raw_input("Please ender your password? ")

print crypt.crypt(password, getsalt())

f = open ('password.txt', 'w')
f.write(password)

password encryption is the good security option.

Please why I get this error when I run my code?

Please ender your password? egg
Traceback (most recent call last):
  File "./crypt.py", line 2, in <module>
    import crypt
  File "/root/crypt.py", line 10, in <module>
    print crypt.crypt(password, getsalt())

Please why I get this error when I run my code?

Don't call your file crypt.py if it is importing a module named crypt. Remove any file crypt.py or crypt.pyc from your folder.

Thanks Sir.
How can I wirte both password and encryption password to file(password.txt)?
And how I chose 6 time?I mean put 6 password and eccrypt them and wirte the to the password.txt
It seems my list above not work!!!

My Par2 script

is about script called passwordcracker.py that gets a sample password from user interface, encrypt it with crypt module and compare it with the saved passwords in dictionary.txt to find a match. Print out appropriate message if the password is found or not. Give it a try to find a password from the dictionary.txt and give the result of both conditions.

Nothing happen when I run passwordcracker.py
my dictionary.txt like
apple
orange
egg
lemon
grapes
secret
strawberry
password

and my password.txt like
victim: HX9LLTdc/jiDE: 503:100:Iama Victim:/home/victim:/bin/sh
root: DFNFxgW7C05fo: 504:100: Markus Hess:/root:/bin/bash

#!/usr/bin/python
# -*- coding: utf-8 -*-
import crypt


def testPass(cryptPass):
    salt = cryptPass[0:2]
    dictFile = open('dictionary.txt', 'r')
    for word in dictFile.readlines():
        word = word.strip('\n')
        cryptWord = crypt.crypt(word, salt)
        if cryptWord == cryptPass:
            print '[+] Found Password: ' + word + '\n'
            return
    print '[-] Password Not Found.\n'
    return


def main():
    passFile = open('passwords.txt')
    for line in passFile.readlines():
        if ':' in line:
            user = line.split(':')[0]
            cryptPass = line.split(':')[1].strip(' ')
            print '[*] Cracking Password For: ' + user
            testPass(cryptPass)


if __name__ == '__main__':
    main()

You do not test for the password for the user so if any user's password is found it will return True. This works for me but again will accept any user's password (and note the added print statements to see what is happening).

import crypt

line="victim: HX9LLTdc/jiDE: 503:100:Iama Victim:/home/victim:/bin/sh"
cryptPass = line.split(':')[1].strip(' ')
salt = cryptPass[0:2]
print "cryptPass",  cryptPass, salt

test_file="""apple
orange
egg
lemon
grapes
secret
strawberry
password"""

dictFile = test_file.split("\n")  
for word in dictFile:
    cryptWord = crypt.crypt(word, salt)
    print "     ", cryptWord, cryptPass,
    if cryptWord == cryptPass:
        print "FOUND",
    print

Thank you so much Sir

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.