1

I am wondering how is should i implement one-time pad encryption in my code.

I use 16-byte pages at the moment, and the implementation in Golang from the page: https://github.com/ryanuber/go-otp/blob/master/otp.go (line 83 is the Encrypt() method).

However, the data i need to encrypt sometimes (not always) is larger than 16 byte.

// Page must be at least as long as plain text
if len(page) < len(payload) {
    return nil, fmt.Errorf("otp: insufficient page size")
}

result := make([]byte, len(payload))

for i := 0; i < len(payload); i++ {
    plainText := int(payload[i])
    secretKey := int(page[i]) // ln 95
    cipherText := (plainText + secretKey) % 255
    result[i] = byte(cipherText)
}

What i did is commented out the page length check, so encryption now ignores the length problem; using modulus while indexing page (line 95):

secretKey := int(page[i % len(page)])

So, if i encrypt data buffer say of 10 megabytes, is it still secure, and if not, why?

  • 1
    What is it that you are trying to accomplish, @Tany? Perhaps we can provide some guidance if you state the bigger problem. Why is it that you feel a OTP is required? Unless you will have a motivated and well-financed attacker (eg: NSA) trying to decipher your text, a single AES key is good enough for 10M of encryption. – Neil Smithline Apr 7 '18 at 20:14
  • PS: Nice job describing your question! The code sample is particularly helpful. – Neil Smithline Apr 7 '18 at 20:15
  • 1
    To confirm, you're considering using 16 bytes of pad to encrypt 10 megabytes (10,485,760 bytes) of data? Knowing the relative size helps in inferring the level of risk, and since a 1-to-655,360 ratio is pretty heavily in the "really bad idea" range, it seems worth double-checking that you didn't misplace a couple factors of 10 somewhere. – Ethan Kaminski Apr 8 '18 at 2:49
8

This is a really, really, really bad idea. First off, this is not a one-time pad, as you're using the same pad to encrypt multiple blocks of data. This is exactly the same as sending multiple messages separately encrypted with the same pad.

There are multiple trivial attacks against this scheme:

  1. If you have any known plaintext, the attacker can xor bytes of the known plaintext against the ciphertext and recover bytes of your pad. Then they can move to each position where that pad byte is reused (i.e., +/- len(page)), xor the pad byte, and recover more plaintext.
  2. Frequency analysis at each position is potentially trivial.
  3. Since the value of each pad byte only affects the matching output bytes, brute force attacks are possible against each byte separately (though it will be harder to know which value is correct, it will still be possible).
5

If you use a byte more than once, it's no longer a one time-pad.

If your key repeats, the result can be broken, because the underlying data probably contains redundancies. Googling for something like "breaking repeating key xor" will get you started on techniques.

  • It’s almost like a Vigenère cipher. – jkd Apr 8 '18 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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