Qbasicnews.com

Full Version: XOR encryption
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
For now I had nothing to do so I wrote down a little idea I had
for encryption:
Code:
'encryption/decryption function by red_Marvin

DECLARE FUNCTION dencrypt$ (msg$, key$)

FUNCTION dencrypt$ (msg$, key$)
FOR msgpos = 1 TO LEN(msg$)
keypos = msgpos
WHILE keypos > LEN(key$): keypos = keypos - LEN(key$): WEND
c$ = MID$(msg$, msgpos, 1)
e$ = MID$(key$, keypos, 1)
ec$ = CHR$(ASC(c$) XOR ASC(e$))
emsg$ = emsg$ + ec$
NEXT
dencrypt$ = emsg$
END FUNCTION

The function takes the char at a position in msg$ and XOR's it with
char in key$ at the same position (Note the WHILE line in the
function)

I just wonder is this a good way of encryption? (If the enemy don't
get the key), Because in some movie somebody said that it would
take thousands (or something) of years to decrypt a message with
a 128 bit (or was it byte?) key, (Yeah I know things in movies are
seldom true but ...whatever)

example

PRINT dencrypt ("Hello","123") 'outputs "yW_]]" to screen

PRINT dencrypt ("yW_]]","123") 'outputs "Hello" to screen
XOR encryption isn't too bad.. although if you are encoding plain text documents with a plain text key it might be easy for someone to figure out how it is encrypted because all the encoded characters will fall within a certain range. Of course, somebody will still need to figure out the key.

eg.
All uppercase char's start with 0100 and lowercase start with 0110.

In this case, the only possible combinations for the first 4 bits of the encoded char would be 0000 and 0010.
But if I make something like

Input file
Create 64 byte (each char is int(rnd*255+.5)) key
Encrypt file
Output encryypted file to blah.enc
Output key to blah.key

It would be quite hard to break, no?
Anything that's just one password over another won't work.

English (or any other) language have certain letter frequencies. It's unavoidable. It's possible to use these frequencies to decrypt anything with just a "layer" of encryption, given a long enough message and short enough key.

If your key is 64 bytes and your message is 128, though, it is impossible to decipher. It just doesn't make sense, because why would you be able to securely send over 64 bytes but not 128?
Agamemnus: True but if I have a big password it's hopefully
avoidable because the for example: the letter "E" isn't always
XOR'ed with the exact same char in the key if not the "E" are
repeating itself with the same frequency as the length of the key
One way to avoid letter frequencies is by using a simple shift algorirthm before you apply your XOR encryption.

Use your password to generate a number n, then shift the ASCII code for the first letter n, the second letter n + 1, then n + 2, and so on.

You can throw a bunch of mathematical crap on that to make it harder to decrypt.