Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 16th, 2012, 02:59 PM
Mr909 Mr909 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 32 Mr909 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 20 sec
Reputation Power: 1
New To Python, Help with encryption/bitwise operators

Okay, so I'm not new to the coding game but the previous language I used to use was DarkBASIC, designed to be easy but not necessarily optimal.

So, in the interest of testing my encryption algorithm with a more reliable system, I was wondering if someone could outline the basics of this process:

-Read a file, byte for byte, into a keycode 'c' (Byte) array, and store the name of that array as an accompanying string. Calling the array "wfile".
-Generate a 'c' array of the given length of the previous file (for sake of example, we can have all the bytes be 255 or whatever.) Calling this "cstream"
-XOR the wfile by cstream, calling this "outfile"
creating the file of the array "outfile", byte for byte, and giving it a name based on the original with an altered extension.

Currently, I've seen lots of methods to do one or the other, but I'm confused about the conversion between the list that is read in from the file and the list that is the byte array, and I have yet to see an outline of bitwise operators.

I'm looking through Pycrypto to see if I can Frankenstein pieces of it onto this engine, but until that works, is there anyone out there who might have done something similar or who would be willing to write it out?

Reply With Quote
  #2  
Old December 16th, 2012, 11:35 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,358 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 33 m 51 sec
Reputation Power: 383
Encryption and decryption uses the same function
Code:
import itertools

def change(text,codekey):
    return ''.join(['%c'%(ord(a)^ord(b)) for (a,b,) in zip(text,itertools.cycle(codekey))])

text='Rock, Paper, and Scissors in Python'
codekey='blort'
encryption = change(text,codekey)
print(encryption)
decryption = change(encryption,codekey)
print(decryption)
assert(text == decryption)
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : December 16th, 2012 at 11:38 PM. Reason: must be late!

Reply With Quote
  #3  
Old December 17th, 2012, 11:16 AM
Mr909 Mr909 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 32 Mr909 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 20 sec
Reputation Power: 1
Yeah, that's fine and all I guess, forgive me if I didn't make my request clear.

I'm trying to:

-Learn how to perform bitwise operations
-Learn how to read bytes into an array (from the array module) from a file
-Learn how to write byte into a file from an array

It seems you did the first thing (and yes, I know that the two processes I described were exactly the same) but it still leaves me in the dark as to how to implement it. The system you gave leaves out what is probably the most crucial element; actually applying an encryption algorithm onto a file.

If I'm using a key of the length specified, do I even need itertools? What does itertools even do? I'm not familiar with the module.

See, my algorithm process, once I can implement it, generates a key of a length equal to (or greater than) the length of the file. So there shouldn't be a need to cycle, like in the example you've provided, right?

Does "change" actually do the bitwise calculations with "ord" and whatnot? I'm not familiar with most of Python's syntax.

I'm sorry if that's too many questions. Thank you for your help, I'll start looking into tutorials now that your code has given me a general direction to work with. Thank you again.

Reply With Quote
  #4  
Old December 17th, 2012, 11:35 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,358 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 33 m 51 sec
Reputation Power: 383
Performing the bitwise operators is easy?
For example:

int(a) ^ int(b) # exclusive or

There are 16 bitwise operations for 2 binary inputs. Implement all of them. An educational exercise. Also implement shift and rotate left, right, with and without sign bit.




learn to read bytes into an array. Learn to open a file, read the file, and study the array module. Use contextual with statement.

with open('somefile','somemode') as varname:
statements




No, you don't need itertools if you made the key length match the text length. itertools will probably use less memory.




&|^ >> << work on integers. ord converts a character to an integer. There are other ways.

Last edited by b49P23TIvg : December 17th, 2012 at 11:47 AM.

Reply With Quote
  #5  
Old December 17th, 2012, 01:45 PM
Mr909 Mr909 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 32 Mr909 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 20 sec
Reputation Power: 1
Well, my current algorithm is mostly dependent on XOR, but I'm working on implementing some other methods.

Yes, I know they're easy, but I didn't know the syntax or where to find it. In the language I'm accustomed to, XOR is:
operandA ~~ operandB
So you could see now that knowing how to implement them is not the same as knowing what they are.

I was going to do it byte-for-byte using the array module because it's supposed to be quicker than a list and would be ideal for byte-for-byte calculations, right?

Okay. I'll go look up a method for the byte reading and writing. Thanks so much for all your help.

Reply With Quote
  #6  
Old December 17th, 2012, 02:35 PM
Mr909 Mr909 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 32 Mr909 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 53 m 20 sec
Reputation Power: 1
So, here's what I got for a test array-

Code:
from array import array

def create_sawtooth(a,b,c,d):
    result = array('B')
    for i in range(0,a):
        result.append(85)
    for i in range(0,b):
        result.append(170)
    for i in range(0,c):
        result.append(85)
    for i in range(0,d):
        result.append(170)
    return result


I was wondering how to optimize it for indefinite arguments. I know there is some way to just have it repeat, but I don't know what it is. Keep in my, switching from 85 every first iteration to 170 every second iteration.
EDIT: So you know, that isn't my actual algorithm, it's a secondary algorithm I use on top of my general encryption to obfuscate it more.

Reply With Quote
  #7  
Old December 17th, 2012, 03:28 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,358 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 33 m 51 sec
Reputation Power: 383

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > New To Python, Help with encryption/bitwise operators

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap