In: Computer Science
UNIX ONLY -- DIFFICULT LAB ASSIGNMENT
This is an extremely difficult subject for me. Could you please give step by step instructions on how to write a script called "encrypt_password.sh" and say line by line what the code should be to complete the assignment according to the instructions and examples below?
The assignment will be done entirely in Terminal on Mac
Our Lab assignment is as follows:
In this lab, you will write a script called encrypt_password.sh to encrypt passwords. The script will ask user to enter user name and password repeatedly and will store it in file unencrypted. Then the script will read the user names and the corresponding passwords from the unencrypted file one line at a time, encrypt the password, and store the name and the encrypted password in another file. The objective of this lab is to familiarize and gain proficiency with the following:
• Use of loops (while and for)
• Read input from keyboard
• Read from file line by line
• Use of md5sum utility to generate md5 hash
The md5sum utility (command) computes and checks a MD5 message digest of a file. Message digest is the cryptographic hash of a clear text. Cryptographic hash or message digest is a one-way encryption; once encrypted the hash cannot be decrypted to get the original text. There are many algorithms for generating message digest or hash from text. MD5 is one such algorithm. Other algorithms are SHA256, SHA512etc. Unix/Linux has utilities(commands) to generate message digest using these algorithms. MD5 uses 128-bit encryption and is considered not secured. SHA 256 or SHA 512 is more secured than MD5. Following is the command to create a hash of a file called “input.txt” using MD5algorithm is
md5sum input.txt
The command to create a hash of the string “CSC2500 is fun and interesting” is
echo CSC2500 is fun and interesting | md5sum
Your script should accept two command line arguments. The 1st argument should be number of users and the 2nd argument should be the name of the unencrypted file.
Your script should do the following
1. Accept user name and password using for loop repeatedly (as many times as the 1st argument).
2. Write the user name and password to the designated file (2nd argument) separated by colon (:) as it reads the inputs from user.
3. Once all the names and passwords are written into the file; read the unencrypted name and password one line at a time from the unencrypted file and encrypt the password and write the username and the encrypted password into the encrypted file (separated by colon). The name of the encrypted file should be same as the unencrypted file with an extension .enc. For example, if the name of the unencrypted file is “user”, the name of the encrypted file should be “user.enc”
Following is an example of the execution of the script
./encrypt_password.sh 3 userInfo
Enter Name: user23
Enter Password: lol
Enter Name: user21
Enter Password: hehe
Enter Name: Mike
Enter Password: password
After the successful execution of the script the current directory should contain two files name userInfo and userInfo.enc
The content of userInfo should be
user23:lol
user21:hehe
Mike:password
The content of the userInfo.enc should be
user23:59bcc3ad6775562f845953cf01624225
user21:f68277605308bf967037423eb4f03fcf
Mike:11408e6f3846af9379b7ca60cbf1b913
Following is an example script that reads lines from a file called input.txt and will print 1st and 3rd words of all the lines.
#!/bin/bash
while read -r line #line is a variable which will store the line read from the file
do
first=$(echo $line | cut -d ' ' -f 1)
third=`echo $line | cut -d ' ' -f 3`
echo $first $third done
THANKS!
You could try the below script
-------------------------------------------------------------
#Accepting the user name and password for 'n' no. of users
#!/bin/bash
#Here $2 refers to the second command line arguement
#Unencrypter file name
UNEC_FILE="$2"
#Here $1 refers to the first command line arguement
for ((i=1; i<="$1"; i++))
do
echo "Enter username:"
read username
echo "Enter password:"
read password
echo "$username":"$password" >> "$UNEC_FILE"
done
#Reading the unencrypted file
while IFS= read -r line
do
IFS=':' read -r username password <<< "$line"
hash=`echo "$password" | md5sum`
echo "$username":"$hash" >> "$UNEC_FILE.enc"
done < "$UNEC_FILE"
-----------------------------------------------------------------------------------------------
Save this by an arbitary name like 'test.sh', execute it by calling 'sh test.sh <arg-1> <arg-2>'. For eg, if the no.of users is 3 and the name of the unencrypted file is 'file', we could call this by executing 'sh test.sh 3 file'