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!
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. - def xor_crypt_string(data, key):
-
return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
-
So to call this: - 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: - def encryptXOR(s, key="\x101Z"):
-
output = ""
-
for character in s:
-
for letter in key:
-
character = chr(ord(character) ^ ord(letter))
-
output += character
-
return output
-
-
def decryptXOR(s, key="\x101Z"):
-
output = ""
-
for character in s:
-
for letter in key[::-1]:
-
character = chr(ord(character) ^ ord(letter))
-
output += character
-
return output
-
-
print repr(encryptXOR('Try to encrypt this message using XOR encryption.'))
-
print
-
print repr(decryptXOR(encryptXOR('Try to encrypt this message using XOR encryption.')))
Output: - >>> '/\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'
-
-
'Try to encrypt this message using XOR encryption.'
-
>>>
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. - def xor_crypt_string(data, key):
-
return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
-
So to call this: - decryptedfile = xor_crypt_string(my_data, my_key)
Thanks for your help bvdet :].
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: - def cryptXOR(s, key="\x1027"):
-
output = ""
-
for character in s:
-
for letter in key:
-
character = chr(ord(character) ^ ord(letter))
-
output += character
-
return output
-
-
s = cryptXOR('Try to encrypt this message using XOR encryption.')
-
print repr(s)
-
print repr(cryptXOR(s))
Output: - >>> 'Agl5az5p{vglea5a}|f5xpfftrp5`f|{r5MZG5p{vglea|z{;'
-
'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.
@bvdet
hi,
any one help me bite wise encryption in xor .
when i using the file for xor encryption @bvdet
hi,
any one help me bite wise encryption in xor .
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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?
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
|
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...
| |