473,406 Members | 2,208 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,406 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 11167
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.