We can generate OTP using HOTP - RFC 4226 and TOTP - RFC 6238.

But the generated OTP normally has a checking function authenticator.check(token, secret);. But this only checks if the OTP is valid.

How to store any data such that it will be encrypted in the storage of the system and can only be accessed after entering the OTP.


OTP is not made for that. OTP is used as a second factor authentication to avoid a leaked password compromising the whole account, and is usually rate-limited, and usually locks itself during a small interval if mismatched (I've seen one minute lock after 3 errors). That is the main usage for OTP.

Encrypting with OTP does not make sense. As the token you enter is usually 4-8 digits long, bruteforcing it will be trivial. You cannot rate-limit a decryption routine, so nothing stops an attacker to try every possible digit very, very fast.

Even if you create a system that protects the key somehow, an attacker can tamper with the clock and send all possible combinations.

  • Also, OTP is - by its very nature - not intended for encryption. Encryption implies that the data will later be decrypted. Decrypting the data requires using the key again, which means it's not a One Time Password at all. – CBHacking Dec 5 at 1:21

Short answer: you don't. At best, you can use the OTP to secure access to a key stored somewhere.

OTPs are for authentication, not encryption. You can write software that checks a user's OTP before granting access to data. This is just standard authentication, such as the way that Github (for example) checks your OTP at login before granting access to your account; it won't encrypt the data. If you want the data to be encrypted, you need somewhere to store the key.

OTPs are far too short to use for key derivation and only briefly valid anyhow (and intended only for one-time use - it's right in the name - so not suitable for something where you'd need the password again like re-deriving a decryption key). You either need to store the key somewhere trusted (and then restrict access to it, possibly using the OTP as an authentication mechanism), or you need to use a longer, multi-use password or similar user credential that can be run through a key-derivation function to produce the encryption/decryption key.

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.