473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

XOR Encryption?

Is there a simple method that can allow for xor encryption on python? What I want to do is pass a file into the method along with the key or integrate the key into the method.

I have seen the ones on google, but they don't really work. The long one only accepts certain file types, and the second one doesn't work at all.

I appreciate all the help!
Feb 13 '10 #1

✓ answered by darktemp

I managed to find a method for xoring which is very simple. bvdet's code is also good, but since xoring is the same method for encrypting and decrypting, it would be simpler to keep encryption and decryption as the same method. I'll just finish and contribute with the code I found.

Expand|Select|Wrap|Line Numbers
  1. def xor_crypt_string(data, key):
  2.     return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
  3.  
So to call this:
Expand|Select|Wrap|Line Numbers
  1. decryptedfile = xor_crypt_string(my_data, my_key)
Thanks for your help bvdet :].

5 11159
bvdet
2,851 Expert Mod 2GB
Here's my first attempt at XOR encryption:
Expand|Select|Wrap|Line Numbers
  1. def encryptXOR(s, key="\x101Z"):
  2.     output = ""
  3.     for character in s:
  4.         for letter in key:
  5.             character = chr(ord(character) ^ ord(letter))
  6.         output += character
  7.     return output
  8.  
  9. def decryptXOR(s, key="\x101Z"):
  10.     output = ""
  11.     for character in s:
  12.         for letter in key[::-1]:
  13.             character = chr(ord(character) ^ ord(letter))
  14.         output += character
  15.     return output
  16.  
  17. print repr(encryptXOR('Try to encrypt this message using XOR encryption.'))
  18. print
  19. print repr(decryptXOR(encryptXOR('Try to encrypt this message using XOR encryption.')))
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> '/\t\x02[\x0f\x14[\x1e\x15\x18\t\x02\x0b\x0f[\x0f\x13\x12\x08[\x16\x1e\x08\x08\x1a\x1c\x1e[\x0e\x08\x12\x15\x1c[#4)[\x1e\x15\x18\t\x02\x0b\x0f\x12\x14\x15U'
  2.  
  3. 'Try to encrypt this message using XOR encryption.'
  4. >>> 
Feb 13 '10 #2
I managed to find a method for xoring which is very simple. bvdet's code is also good, but since xoring is the same method for encrypting and decrypting, it would be simpler to keep encryption and decryption as the same method. I'll just finish and contribute with the code I found.

Expand|Select|Wrap|Line Numbers
  1. def xor_crypt_string(data, key):
  2.     return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
  3.  
So to call this:
Expand|Select|Wrap|Line Numbers
  1. decryptedfile = xor_crypt_string(my_data, my_key)
Thanks for your help bvdet :].
Feb 14 '10 #3
bvdet
2,851 Expert Mod 2GB
Actually, I made a false assumption when I assumed that I had to reverse the order of the key. The same function works both ways. Here's the result:
Expand|Select|Wrap|Line Numbers
  1. def cryptXOR(s, key="\x1027"):
  2.     output = ""
  3.     for character in s:
  4.         for letter in key:
  5.             character = chr(ord(character) ^ ord(letter))
  6.         output += character
  7.     return output
  8.  
  9. s = cryptXOR('Try to encrypt this message using XOR encryption.')
  10. print repr(s)
  11. print repr(cryptXOR(s))
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 'Agl5az5p{vglea5a}|f5xpfftrp5`f|{r5MZG5p{vglea|z{;'
  2. 'Try to encrypt this message using XOR encryption.'
The code you found uses itertools functions. Function cycle() cycles through the letters in the key and XORs letter pairs until the text is exhausted. This may be a better solution because there are no matching text patterns to the original string.
Feb 15 '10 #4
@bvdet
hi,

any one help me bite wise encryption in xor .
May 18 '10 #5
when i using the file for xor encryption





@bvdet
hi,

any one help me bite wise encryption in xor .
May 18 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Cliff | last post by:
We are trying to connect to 3 different Oracle databases using MS Access as the front-end and ODBC as the connection. The problem that we are having is that 1 of the databases requires a...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
7
by: Alan Silver | last post by:
Hello, I am writing a page where sensitive data is collected (over SSL) and stored in a database. I have been looking at the .NET encryption classes, but am a bit confused as to which is best...
2
by: Sumit Gupta | last post by:
Can anyone please tell me how to encrpt string or any kind of Data. Also the Algorithm of Compression. Any Link tutorial etc. Like : Zip or RAR Formats etc.
9
by: sweety | last post by:
Dear All, How to encrypt a C data file and make binary file and then have to read a bin file at run time and decrypt the file and have to read the data. Any help to achive this pls. Would be...
4
by: pintu | last post by:
Hello everybody.. I hav some confusion regarding asymmetric encryption.As asymmetric encryption it there is one private key and one public key.So any data is encrypted using private key and the...
1
by: =?Utf-8?B?bWljcm9ob2Y=?= | last post by:
Short version: Is there a way to configure (preferably programmatically) the max encryption strength that will be used by the framework when connecting to a particular SSL-protected web service? ...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
19
by: klenwell | last post by:
Another request for comments here. I'd like to accomplish something like the scheme outlined at this page here: http://tinyurl.com/3dtcdr In a nutshell, the form uses javascript to hash...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.