- 2,163
- 191
See this code please
https://codeshare.io/EBkJrO
Edit: I have fixed the Example in byteArray function
https://codeshare.io/EBkJrO
Edit: I have fixed the Example in byteArray function
Last edited:
I meant thisPeterDonis said:In Python 3, the "string" data type (i.e.,str) isn't bytes, it's Unicode. The Python 3 "byte string" data type isbytes.
>>> bin(97)
'0b1100001'
>>> type(bin(97))
<class 'str'>
>>>
For instance you can create a XOR encryption that can take the key as bits (a binary key) or you can create an ASCII key (see this site https://www.dcode.fr/xor-cipher)PeterDonis said:This doesn't make sense. One byte is one byte, whether you call it a "string" or something else.
In my previous post, I was trying to say that you'll need a lot of storage if you store the key as binary. But an ASCII key will be more useful in terms of storage.PeterDonis said:What does make a difference, though, is that if you store the key as a single string, you have one string object in Python, which will take storage equal to the number of bytes in the string plus the Python overhead for one string object. Whereas if you store the key as a bunch of one-byte strings, you will need storage equal to the number of bytes in the string, plus the Python overhead for the same number of Python string objects (in your case, ten) instead of just one string object. That can be a lot more storage.
Wow, that is some pretty complicated code for doing something pretty simple. I think the best learning point you can take away from this is 'representing binary data as a string of 0's and 1's is a really bad idea'.Arman777 said:See this code please
https://codeshare.io/EBkJrO
Edit: I have fixed the Example in byteArray function
bytes, or a string (using all values 0-255) if you must, or even a list or array of integers. Anything but a string of '1's and '0's.Then you are using the wrong operation. You don't want to convert an integer to a string. You want to convert a string (the message) into an integer (or a sequence of integers).Arman777 said:I meant this
You are very confused.Arman777 said:In my previous post, I was trying to say that you'll need a lot of storage if you store the key as binary. But an ASCII key will be more useful in terms of storage.
Yes, and this is a 64-bit integer. It's not a string of 64 bytes that are either "0" or "1".Arman777 said:If your message is "password," and if you want to make the XOR encryption "unbreakable," you need to create a random binary key that is 64 bits long.
First, most of those characters aren't ASCII characters, so the term "ASCII" is incorrect.Arman777 said:For instace, I have generated a random binary key. So for an unbreakable message you'll need to store this.
0000110011010000111111100100101110110010010011110110110000101011
but if you convert this into ASCII (or as ASCII key), you'll need to store just
♀ÐþK²Ol+
The "unprintable" characters aren't ASCII characters to begin with, as noted above. But more important, viewing them as "characters" makes no sense. As above, you have a 64-bit integer. You can represent integers directly in Python as integers.Arman777 said:Here, the only problem is that the binary representation of the key can also represent the unprintable ASCII characters.
Exactly! Except that there is no need for the bin() function anywhere. You have an integer; if you want to convert it to a character, you call ord() on it, not bin().Arman777 said:Instead of turning 67 and 45 into bytes and then doing XOR bitwise I could have just do
bin(67 ^ 45)
which gives the correct answer. This approach will definately shorten my code
What are you talking about? You just did understand it (in what I quoted above and responded to with "Exactly!").Arman777 said:You guys are giving hints but I cannot understand something that i don't know...
What? Why are you throwing away the right answer right after you found it?Arman777 said:Hex is a good approach
Again, the OP seems to be confused about the difference between a numeric digit character, like '0' and a numeral, like 0.PeterDonis said:What you are calling "binary" are Python strings that contain "0" and "1" characters that can be interpreted as the bits in a binary representation of an integer
Yes you are right I have realized that later on.PeterDonis said:Then you are using the wrong operation. You don't want to convert an integer to a string. You want to convert a string (the message) into an integer (or a sequence of integers).
Yes you are right. The problem was I was not aware of that kind of operation was avaliable in python. So that was why I was trying to strange stuff.PeterDonis said:You say this bin() conversion is an intermediate step, but I don't think you've fully thought through what you are doing. If you already have an integer representing a character in the message and an integer representing a character in the key, you can just xor them directly in Python. There is no need to convert them into these "binary" strings.
Yes I have also realized thatPeterDonis said:Exactly! Except that there is no need for the bin() function anywhere. You have an integer; if you want to convert it to a character, you call ord() on it, not bin().
Well It was a late enlightenment for me. In my head I was keep repeating what these guys want.PeterDonis said:What are you talking about? You just did understand it (in what I quoted above and responded to with "Exactly!").
I was just thinking that can be a good idea. But I can also change the encrypted message to to a list of integers (or hex) which seems more reasonable. Such asPeterDonis said:What? Why are you throwing away the right answer right after you found it?
I think @arman here means that he realizes hex can be useful for external representation of arbitrary 8 bit values; hex is much more compact and easier to read than strings of 0's and 1's and does not suffer the problem of unprintable characters of direct extended ASCII rendering.PeterDonis said:What? Why are you throwing away the right answer right after you found it?Arman777 said:
Hex is a good approach
Yes exactly. I was talking about external representationpbuk said:I think @arman here means that he realizes hex can be useful for external representation of arbitrary 8 bit values; hex is much more compact and easier to read than strings of 0's and 1's and does not suffer the problem of unprintable characters of direct extended ASCII rendering.
The light regarding internal representation does seem to have clicked on![]()
That goes a long way towards explaining why I spent the last couple of pages thinking that at least one of us is an idiot. Sure, you can do the whole thing in display-types ; it's a CLI ; machine efficiency isn't an issue.Arman777 said:Yes exactly. I was talking about external representation
No one has to be an idiot. Its important to learn from our mistakes and we may not be know everything.hmmm27 said:one of us is an idiot
I have finished the coding actually. I even created a GUIhmmm27 said:What have you accomplished, and how are you stuck ?
Arman777 said:I have finished the coding actually. I even created a GUI
https://github.com/seVenVo1d/random/tree/master/XOR%20Encryption
Thats possible. I am changing the names and the code itself most of the times.lyuc said:Your link is wrong.
I have thought about it and limit the output only printible characters or as you have pointed out I could have removed the '\x' in front of themlyuc said:As a further exercise, you might want to change the output into some kind of more compact format.
Thanks for checking it out.lyuc said:I looked at the non-gui code.