0
$\begingroup$

I don't have much previous experience at all with cryptography, this is pretty much the first time I've tried anything similar.

I'm trying to implement an extremely simple XOR encryption system in ComputerCraft for fun, here is the code:

local function xorEncrypt(data, key) 
    local klen = #key local 
    s = "" 
    for i = 1, #data do 
        s = s..string.char(bit.bxor(string.byte(data, i), string.byte(key, i % klen))) 
    end 
return s 
end

The only thing I need to know is, in this case, will the messages still be able to be decrypted if the key that I use is shorter than the data I am encrypting?

$\endgroup$
4
  • $\begingroup$ This is very bad! At the very least, it leaks some information about the structure of the plaintext, which likely leads to a compromise of secrecy. For possible attacks, see this or this question. $\endgroup$
    – yyyyyyy
    Commented Mar 10, 2015 at 14:20
  • $\begingroup$ Thanks for the response! I'll keep that in mind, although this is just Minecraft and I won't be protecting any real sensitive data. Will it still function as intended (encrypt a message with the key, decrypt the output with the key) if the key is shorter than the message? $\endgroup$ Commented Mar 10, 2015 at 14:26
  • $\begingroup$ I see. Yes, it should "work" in the sense you described. $\endgroup$
    – yyyyyyy
    Commented Mar 10, 2015 at 14:39
  • $\begingroup$ Yes, it should work, because of the % klen part. (Exercise: what does that part actually do?) You could've also simply tried it yourself on some test data. (In any case, I'm voting to close this question as off topic, since it's really more about basic programming rather than cryptography. It would be a better fit for Stack Overflow.) $\endgroup$ Commented Mar 10, 2015 at 19:52

3 Answers 3

1
$\begingroup$

Yes, it will work.

However, it's not secure and shouldn't be used in a production system.

Here's what you're doing (mathematically):
You have a key $K$ of length $l_K$ and a message $M$ of length $l_M$ and you just go ahead and $K\oplus M_i$ for all parts $M_i$ of the message of length at most $l_K$.

This is a textbook example for one-time-pad re-use can be broken relatively easy as was pointed out by yyyyyyy in the comments.

For the "how" of breaking this I'll refer you to the already mentioned Q&As:

$\endgroup$
1
$\begingroup$

This cipher system is the original Vernam cipher. (In the original Vernam cipher, the key was stored on a loop of paper tape that repeated over and over).

Like all practical encryption systems, the Vernam cipher has a secret key that it uses over and over.

ciphertext[i] = plaintext[i] bit_xor key[ i % keylength ]

This cipher can be seen as a variant of the Vigenère cipher, and has the same security.

Anyone with the secret key can decrypt a ciphertext message to recover bit-for-bit the original plaintext (no matter what the keylength is) with:

plaintext[i] = ciphertext[i] bit_xor key[ i % keylength ]

Though the cipher is easy to understand and implement, for three centuries it resisted all attempts to break it; this earned it the description le chiffre indéchiffrable (French for 'the indecipherable cipher'). ... Kasiski entirely broke the cipher and published the technique ... In 1863 Friedrich Kasiski was the first to publish a successful general attack on the Vigenère cipher.

-- Wikipedia: Vigenère cipher

The people at the Cipher Exchange say the Vigenère cipher can be solved by pencil and paper methods with enough ciphertext -- ciphertext 15 times the length of the key. The same techniques can be used to break this cipher, given the same amount of ciphertext. (Some of those techniques are mentioned at " What is the limit of plaintext required to break the Vigenère encryption? ").

$\endgroup$
0
$\begingroup$

Yes, It will work but can be easily broken with sufficient cipher text by computing the index of coincidence (the probability that the two random elements of {x = (x1x2, ... xn)} are identical)

You could use modern cryptographic algorithm:

  1. Symmetric Key Cryptography (for larger files)
  2. Public Key Encryption (if plain text is small)

You could also use stream ciphers like Trivium or PRNGs which would be easier to implement.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.