Skip to content
Back to Blog
Security

otpauth URI Explained: Recover a 2FA Secret from a QR Code

A 2FA setup QR code holds one otpauth:// line with your secret. Learn each field, then use two in-browser tools to read it and set up a new phone.

9 min read

A 2FA setup QR code contains a single line of text, an otpauth://totp/... URI. The Base32 string after secret= is the key itself. With that one line, a standard authenticator app can rebuild exactly the same codes, so a screenshot of the setup QR is enough to restore your 2FA on a new device, as long as the service hasn’t replaced the secret since.

A QR code exported from Google Authenticator’s Transfer codes screen uses a different format, and one code can hold several accounts. Our decoder reads it too, as described separately below.

This guide breaks the URI down field by field, then walks through the recovery with two in-browser tools: read the screenshot, check the secret against your old codes, and enroll a new app. Every example uses the public test secret JBSWY3DPEHPK3PXP from the specification, never a real key.

The otpauth URI, Field by Field

The format is defined on the Key Uri Format page of the Google Authenticator project:

otpauth://TYPE/LABEL?PARAMETERS

A typical setup URI looks like this:

otpauth://totp/Example:alice@example.com?secret=JBSWY3DPEHPK3PXP&issuer=Example
FieldRequired / defaultWhat to watch for
typeRequiredtotp here. hotp is the counter-based variant and needs a counter parameter instead of a period
labelRequiredFor example Example:alice@example.com: the account name, optionally prefixed by the issuer and a colon (literal or %3A). Neither part may contain a colon
secretRequiredBase32: letters A–Z and digits 2–7. The spec says = padding should be omitted
issuerStrongly recommended. If absent, may be taken from the label prefixIf both are present, they should be equal
algorithmOptional, default SHA1SHA256 and SHA512 are the other allowed values
digitsOptional, default 66 or 8
periodOptional, default 30Seconds per code

Two details cause most of the confusion when you read a real URI:

  • Percent-encoding. The label and issuer are URL-encoded, so %20 is a space, %40 is @ and %3A is a colon. ACME%20Co:john.doe@email.com is the spec’s own example.
  • Missing parameters mean defaults. A URI with only secret and issuer is an ordinary SHA-1, 6-digit, 30-second TOTP. When you copy the settings by hand, write those defaults down too.

The spec page was last edited in 2018. Its notes from that time say Google Authenticator ignored algorithm and period, and ignored digits on Android. It doesn’t document how today’s apps behave, so if your URI uses non-default values, check the codes after you enroll the new app.

For how the secret, the clock and HMAC turn into a 6-digit code, see how TOTP works.

Step 1: Read the URI from a Screenshot

Open the QR code decoder, then choose, drop or paste the screenshot. The image is decoded in your browser and is not uploaded.

For a setup QR code, the decoder labels the result 2FA account (otpauth) and splits the URI into the fields from the table above: OTP type, issuer, account, secret key, algorithm, digits and period. The secret is masked by default, both in its field and in the Original payload. Select Show secret to reveal it. If a field doesn’t follow the Key Uri Format, the decoder lists the reasons instead of the fields.

What to do next depends on the result:

  • TOTP, with every field valid: the decoder offers Open in TOTP generator. You’ll use it in step 2.
  • HOTP, with every field valid: the decoder shows the fields but doesn’t offer that button. This guide covers TOTP only, so the rest of it doesn’t apply.
  • Reasons instead of fields: the URI doesn’t follow the Key Uri Format, and the decoder doesn’t offer the button either. Check the URI against the table above. The rest of this guide needs a valid TOTP URI.
  • Labeled Google Authenticator export: the QR code came from Google Authenticator’s transfer feature. See Google Authenticator export QR codes.
  • Any other label: the text is neither an otpauth:// URI nor an export, and the rest of this guide doesn’t apply to it either.

Step 2: Check the Secret Against Your Old Codes

Before you rely on the secret, prove it is the right one. If the old device still works, compare codes side by side.

  1. In the decoder, select Open in TOTP generator. The TOTP generator opens on the Generate tab with the secret and the Advanced options (algorithm, digits, period) filled in from the URI. Unlike the decoder, the Base32 secret box shows the secret in plain text.
  2. If you have the URI as text instead, open the generator and paste the whole otpauth:// URI into Base32 secret. It fills in the same fields. An HOTP URI gets a message that the generator only supports TOTP, and an invalid parameter gets an explanation.
  3. Compare the Current code with the code on the old device. They should be identical within the same time window.

If they differ, check the computer’s clock first, then each parameter. A single mistyped character changes every code.

If you’d rather check in a terminal, the Python library pyotp reads the whole URI:

import pyotp

uri = "otpauth://totp/Example:alice@example.com?secret=JBSWY3DPEHPK3PXP&issuer=Example"
print(pyotp.parse_uri(uri).now())

Step 3: Restore the Account on a New Device

  1. In the TOTP generator, keep the secret and the advanced options from step 2, then switch to the Set up tab.
  2. Check Issuer and Account. They were filled in from the URI, so the new app shows a recognizable name. If Issuer is empty, type the service’s name.
  3. Scan the QR code on the screen with the new authenticator app. Check that the app shows the same code as the Generate tab.
  4. Sign in to the service with a code from the new app to confirm it works.

The Set up tab also shows the rebuilt otpauth:// URI, which some password managers accept as a pasted value. Our QR code generator can encode any text as a QR image, but a QR of this URI is as sensitive as the original screenshot.

Rotate the secret if the screenshot might have been exposed. Set up 2FA again in the service’s security settings so the account gets a new secret, then confirm that codes from the old secret are rejected. After that, the old screenshot is useless to anyone who finds it.

Google Authenticator Export QR Codes

Google Authenticator has its own transfer path: on the old phone, Transfer codes → Export codes creates a QR code, and on the new phone, Transfer codes → Import codes scans it. When you export more than one account, the old phone may create more than one QR code (Google’s help page).

An export QR code doesn’t hold an otpauth://totp/ URI. Its text starts with otpauth-migration://offline?data=, and the accounts in that QR code are all encoded in the single data value. Google documents this code only as something to scan with Import codes and hasn’t published the format. Our QR code decoder reads it with a definition the community worked out by reverse engineering, not with an official specification.

Decode the export QR code the same way as in step 1. It is read in your browser and is not uploaded. The decoder labels the result Google Authenticator export and gives each account its own row: the issuer and account name as the row title, then the OTP type, algorithm and digits, plus the period for TOTP or the counter for HOTP. Every secret is masked, and so is the whole encoded value in the Original payload. To see a secret, select Show secret in that account’s row. It reveals that row only; nothing on the page shows or copies all the secrets at once.

What a row offers depends on the account:

  • TOTP: Open in TOTP generator opens the account in the TOTP generator, where you compare codes with the old phone as in step 2. Rebuild standard QR code shows a standard otpauth:// QR code for the account. Scan it with the new authenticator app, then sign in once with a code from the app, as in step 3.
  • HOTP: the row shows the counter and offers Rebuild standard QR code, but not Open in TOTP generator. The TOTP generator is time-based and can’t produce HOTP codes.
  • MD5: the account can only be displayed, and the row has neither of those two buttons. Standard otpauth URIs and the TOTP generator don’t support MD5, and no codes can be generated for it here.

The export stores no period. The decoder therefore gives every TOTP account a 30-second period, and if the service uses a different one, the codes won’t match. Compare them with the old phone, as in step 2.

If the export produced several QR codes, decode them one at a time to see the accounts in each. When you choose several images at once, the decoder shows where each image belongs, such as QR code 1 of 2 in this export, and names any QR codes that are missing, but it doesn’t list the accounts.

An export QR code holds the secrets of every account in it. Keep it, and any QR code you rebuild from it, as carefully as a setup screenshot (see below). The TOTP generator doesn’t read the export text itself; open each account from the decoder instead. If the new phone also uses Google Authenticator, you can still scan the export with Import codes as Google describes.

The same help page says codes saved to your Google Account sync automatically when you sign in to Google Authenticator on a new device. In that case you may not need a QR code at all.

Keep the Secret Secret

  • The secret is the second factor. TOTP codes are computed from the secret and the current time, so anyone with the URI or a screenshot of the QR can generate valid codes for as long as the secret stays active.
  • Store setup screenshots like passwords. An encrypted password manager or an encrypted drive is a reasonable place. A camera roll that syncs to a photo cloud is not. Delete copies you don’t need.
  • Never paste a URI into a search box or a chat. A search engine receives whatever you type into its box, and a chat message lives in someone else’s logs. If a secret has ended up in either place, rotate it as described in step 3.

FAQ

Does an old 2FA QR screenshot still work?

Yes, as long as the service still uses that secret. The QR code holds fixed text and has no expiry of its own. Once you reset or re-enroll 2FA on the service, the old secret stops working and so does the screenshot.

My secret has lowercase letters or spaces. Is it valid?

Usually, yes. Base32 is designed to be case-insensitive (RFC 4648), and spaces are often added only to make the key easier to read. Our TOTP generator strips spaces and uppercases the secret. Letters outside A–Z and digits other than 2–7 are invalid.

Why does the URI contain %40 or %20?

The label and issuer are URL-encoded. %40 is @, %20 is a space and %3A is a colon. Our QR code decoder and TOTP generator decode them for you. If you type the account name into a new app by hand, decode them first. A spec-compliant secret is plain Base32 without padding, so it contains no percent signs.

Can someone work out the secret from my 6-digit codes?

Not in practice. RFC 4226’s security analysis concludes that an attacker who collects many valid codes gains no significant advantage over random guessing, and the best attack is brute force. The secret is what has to be protected, not the codes.

Can I get the secrets out of a Google Authenticator export QR code?

Yes. Our QR code decoder reads otpauth-migration:// export codes in your browser and lists one account per row, with each secret masked until you show that row. The format comes from community reverse engineering, not from Google. TOTP accounts always get a 30-second period, MD5 accounts can only be displayed, and HOTP accounts show their counter but can’t be opened in the TOTP generator. See Google Authenticator export QR codes for the steps.

References

Tags: Two-Factor Authentication TOTP Security QR Code 2FA

Related Articles

View all articles