1

I have following base64 encoded string:

eyJhbGciOiJSUzI1NiIsImtpZCI6IjdEODU3RjE3RjMwQTBBNzY4OUQ4RTFDMTI0RjRFMzk1MEU2REIyQ0YiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJmWVZfRl9NS0NuYUoyT0hCSlBUamxRNXRzczgifQ

I easy decode it online, for example here

However when i try to decode it in swift i'm not succeed, i used:

func fromBase64() -> String? {
    guard let data = Data(base64Encoded: self) else {
      return nil
    }

    return String(data: data, encoding: .utf8)
  }

But i return nil.

0

1 Answer 1

6

Your string is missing the = padding characters at the end of a base64 output to make the string length divisible by 4. Try with

eyJhbGciOiJSUzI1NiIsImtpZCI6IjdEODU3RjE3RjMwQTBBNzY4OUQ4RTFDMTI0RjRFMzk1MEU2REIyQ0YiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJmWVZfRl9NS0NuYUoyT0hCSlBUamxRNXRzczgifQ==
Sign up to request clarification or add additional context in comments.

2 Comments

that usually occur at the end.. This is a bit misleading. Since his encoded string isn't divisible by 4, he needs 2 == to make it correct.
You're right, I've update my answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.