Decode Base64Url to Base64 -- Swift

Decode base64URL to base64 -- Swift

"base64url" differs from the standard Base64 encoding in two aspects:

  • different characters are used for index 62 and 63 (- and _ instead
    of + and /)
  • no mandatory padding with = characters to make the string length
    a multiple of four.


Here is a possible conversion function:

func base64urlToBase64(base64url: String) -> String {
var base64 = base64url
.replacingOccurrences(of: "-", with: "+")
.replacingOccurrences(of: "_", with: "/")
if base64.characters.count % 4 != 0 {
base64.append(String(repeating: "=", count: 4 - base64.characters.count % 4))
return base64


let base64url = "hJQWHABDBjoPHorYF5xghQ"
let base64 = base64urlToBase64(base64url: base64url)
print(base64) // hJQWHABDBjoPHorYF5xghQ==

if let data = Data(base64Encoded: base64) {
print(data as NSData) // <8494161c 0043063a 0f1e8ad8 179c6085>

For the sake of completeness, this would be the opposite conversion:

func base64ToBase64url(base64: String) -> String {
let base64url = base64
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: "=", with: "")
return base64url

Update for Swift 4:

func base64urlToBase64(base64url: String) -> String {
var base64 = base64url
.replacingOccurrences(of: "-", with: "+")
.replacingOccurrences(of: "_", with: "/")
if base64.count % 4 != 0 {
base64.append(String(repeating: "=", count: 4 - base64.count % 4))
return base64

Can not use base64 decode in swift

The problem with your base64 encoded string is that it is not properly terminated. You just need to add "==" to the end of your string:

let base64Code = "eyJyb2xlcyI6WyJVc2VyIiwiTWFpbnRhaW5lciJdLCJhdWQiOiJzdHVkZW50cyIsImV4cCI6MTYwMTk5MjU5OCwiaWF0IjoxNjAxOTg4OTk4LCJpc3MiOiJzZXJ2aWNlIHByb2plY3QiLCJzdWIiOiI0NWI1ZmJkMy03NTVmLTQzNzktOGYwNy1hNThkNGEzMGZhMmYifQ"

let decodeBase64Code = Data(base64Encoded: base64Code + repeatElement("=", count: base64Code.count.isMultiple(of: 4) ? 0 : 4 - base64Code.count % 4))!
let json = String(data: decodeBase64Code, encoding: .utf8)! // "{"roles":["User","Maintainer"],"aud":"students","exp":1601992598,"iat":1601988998,"iss":"service project","sub":"45b5fbd3-755f-4379-8f07-a58d4a30fa2f"}"

How to decode Swift 5 version?

Do not use NSString. Do not use NSMutableData. Do not use NSLog. This is Swift, not Objective-C. Use Data.

Example of decoding a string which has been Base 64 encoded:

let s = "d2VsbCBob3dkeSB0aGVyZQ=="
let d = Data(base64Encoded: s)!
let s2 = String(data: d, encoding: .utf8)!
print(s2) // well howdy there

That may not be identically what you are trying to do (I couldn't figure out what you were trying to do), but it should get you started. Just consult the documentation on Data.

Decoding Base64 String Returns nil

Your string contains underscore character ('_' U+005F LOW LINE), which is invalid as Base-64.

There are some variations of Base-64, so you need to find which one your Gmail something is using. (Wikipedia Base-64.)

Assuming it uses base64url URL- and filename-safe, you may need to write something like this:

extension String {
func urlSafeBase64Decoded() -> String? {
var st = self
.replacingOccurrences(of: "_", with: "/")
.replacingOccurrences(of: "-", with: "+")
let remainder = self.count % 4
if remainder > 0 {
st = self.padding(toLength: self.count + 4 - remainder,
withPad: "=",
startingAt: 0)
guard let d = Data(base64Encoded: st, options: .ignoreUnknownCharacters) else{
return nil
return String(data: d, encoding: .utf8)


let base64 = "WW91ciBhY2NvdW50IHNhbG1hbm1hamlkMTRAZ21haWwuY29tIGlzIGxpc3RlZCBhcyB0aGUgcmVjb3ZlcnkgZW1haWwgZm9yDQpzaWRyYWlicmFoaW0wMzQ5QGdtYWlsLmNvbS4gRG9uJ3QgcmVjb2duaXplIHRoaXMgYWNjb3VudD8gQ2xpY2sgaGVyZQ0KPGh0dHBzOi8vYWNjb3VudHMuZ29vZ2xlLmNvbS9BY2NvdW50RGlzYXZvdz9hZHQ9QU9YOGtpcHBKRG5OX19KVGloejdoUHRnMlFwRHRRb3VETWVUalRMN2J4YjA3b2dJRC1xYm54QnZPRElTUjB3Q1h6RHlYQSZyZm49MTI3JmFuZXhwPWdpdmFiLWZhLS1tZHYyLWZhPg0KR29vZ2xlIEFQSXMgRXhwbG9yZXIgd2FzIGdyYW50ZWQgYWNjZXNzIHRvIHlvdXIgbGlua2VkIEdvb2dsZSBBY2NvdW50DQoNCg0Kc2lkcmFpYnJhaGltMDM0OUBnbWFpbC5jb20NCg0KSWYgeW91IGRpZCBub3QgZ3JhbnQgYWNjZXNzLCB5b3Ugc2hvdWxkIGNoZWNrIHRoaXMgYWN0aXZpdHkgYW5kIHNlY3VyZSB5b3VyDQphY2NvdW50Lg0KQ2hlY2sgYWN0aXZpdHkNCjxodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20vQWNjb3VudENob29zZXI_RW1haWw9c2lkcmFpYnJhaGltMDM0OUBnbWFpbC5jb20mY29udGludWU9aHR0cHM6Ly9teWFjY291bnQuZ29vZ2xlLmNvbS9hbGVydC9udC8xNTY3ODQ3ODEyMDAwP3JmbiUzRDEyNyUyNnJmbmMlM0QxJTI2ZWlkJTNELTE1NzQzMDEyNjk4Mjg2MTM3OTQlMjZldCUzRDElMjZhbmV4cCUzRGdpdmFiLWZhLS1tZHYyLWZhPg0KWW91IHJlY2VpdmVkIHRoaXMgZW1haWwgdG8gbGV0IHlvdSBrbm93IGFib3V0IGltcG9ydGFudCBjaGFuZ2VzIHRvIHlvdXINCkdvb2dsZSBBY2NvdW50IGFuZCBzZXJ2aWNlcy4NCsKpIDIwMTkgR29vZ2xlIExMQywgMTYwMCBBbXBoaXRoZWF0cmUgUGFya3dheSwgTW91bnRhaW4gVmlldywgQ0EgOTQwNDMsIFVTQQ0K"

print(base64.urlSafeBase64Decoded() ?? "* decoding failed*")


You received this email to let you know about important changes to your
Google Account and services.
© 2019 Google LLC, 1600 Amphitheatre Parkway, Mountain View, CA 94043, USA

How can I encode a string to Base64 in Swift?

I don’t have 6.2 installed but I don’t think 6.3 is any different in this regard:

dataUsingEncoding returns an optional, so you need to unwrap that.

NSDataBase64EncodingOptions.fromRaw has been replaced with NSDataBase64EncodingOptions(rawValue:). Slightly surprisingly, this is not a failable initializer so you don’t need to unwrap it.

But since NSData(base64EncodedString:) is a failable initializer, you need to unwrap that.

Btw, all these changes were suggested by Xcode migrator (click the error message in the gutter and it has a “fix-it” suggestion).

Final code, rewritten to avoid force-unwraps, looks like this:

import Foundation

let str = "iOS Developer Tips encoded in Base64"
println("Original: \(str)")

let utf8str = str.dataUsingEncoding(NSUTF8StringEncoding)

if let base64Encoded = utf8str?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))

println("Encoded: \(base64Encoded)")

if let base64Decoded = NSData(base64EncodedString: base64Encoded, options: NSDataBase64DecodingOptions(rawValue: 0))
.map({ NSString(data: $0, encoding: NSUTF8StringEncoding) })
// Convert back to a string
println("Decoded: \(base64Decoded)")

(if using Swift 1.2 you could use multiple if-lets instead of the map)

Swift 5 Update:

import Foundation

let str = "iOS Developer Tips encoded in Base64"
print("Original: \(str)")

let utf8str = .utf8)

if let base64Encoded = utf8str?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) {
print("Encoded: \(base64Encoded)")

if let base64Decoded = Data(base64Encoded: base64Encoded, options: Data.Base64DecodingOptions(rawValue: 0))
.map({ String(data: $0, encoding: .utf8) }) {
// Convert back to a string
print("Decoded: \(base64Decoded ?? "")")

Swift: Files appear to be corrupt after base64 encoding

I was able to find a solution that works for me.

First I used an online base64 encoder to encode a simple xml file.
I then used the generated base64 string to re-create my POST using Postman (great tool, only just discovered it).

This worked and my file was attached to the specified record and not corrupted.

I then used the feature of Postman that generates code to see what it thought my base64String used for filedata should look like. I found that all the '+' characters were replaced with "%2B".

A bit of further investigation found that the query item in Swift does some of it's own percent encoding, however this answer gave an explanation why the '+' character is valid and not percent encoded by default.

The end result was that I removed my manual percent encoding from the original base64String:

let fileAsString = attachment.base64EncodedString() + "=="

And I added it when generating the httpBody from the query items:

request.httpBody = Data(query!.replacingOccurrences(of: "+", with: "%2B").utf8)

Convert between UIImage and Base64 string


First we need to have image's NSData

//Use image name from bundle to create NSData
let image : UIImage = UIImage(named:"imageNameHere")!
//Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)!

//OR next possibility

//Use image's path to create NSData
let url:NSURL = NSURL(string : "urlHere")!
//Now use image to create into NSData format
let imageData:NSData = NSData.init(contentsOfURL: url)!

Swift 2.0 > Encoding

let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)

Swift 2.0 > Decoding

let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!

Swift 3.0 > Decoding

let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!

Encoding :

let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)

Decoding :

let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!
yourImageView.image = decodedimage

Swift 3.0

let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
let decodedimage = UIImage(data: dataDecoded)
yourImageView.image = decodedimage


iOS7 > version

You can use NSData's base64EncodedStringWithOptions

Encoding :

- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

Decoding :

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];

iOS 6.1 and < version

First Option : Use this link to encode and decode image

Add Base64 class in your project.

Encoding :

 NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
NSString *strEncoded = [Base64 encode:data];

Decoding :

 NSData* data = [Base64 decode:strEncoded ];;
image.image = [UIImage imageWithData:data];

Another Option: Use QSUtilities for encoding and decoding

data from base64 url

Actually you can decode Base64 data from an URL (see for
example Base64 Decoding in iOS 7+ where this is demonstrated in Objective-C). The format is a bit different from what
you have:

let url = URL(string: "data:application/octet-stream;base64,SGVsbG8gd29ybGQh")!
let data = try! Data(contentsOf: url)

print(String(data: data, encoding: .utf8)!) // Hello world!

(Error checking omitted for brevity.)

Related Topics

Leave a reply