Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 views
in Technique[技术] by (71.8m points)

ios - Decode from Base64 format in swift

enter image description hereI am trying to Decode the encrypt key. I tried decode online I am getting bellow result it was working.

KEY : oPvvNMsiOd//bheeboamB65AXi8r+go9NLz2rTPEJRU=
decode values : 4"9nn@^/+
=43%

when I tried this code it was getting fatal error

let base64Encoded = "oPvvNMsiOd//bheeboamB65AXi8r+go9NLz2rTPEJRU="
let decodedData = Data(base64Encoded: base64Encoded)!
let decodedString = String(data: decodedData, encoding: .utf8)!
print(decodedString)

I tried many ways, I have to Decode from the base64.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

finally I found solution for my issue am getting exact out put.

    extension Character {
        var isAscii: Bool {
            return unicodeScalars.allSatisfy { $0.isASCII }
        }
    }
    extension RangeReplaceableCollection where Self: StringProtocol {
        var asciiPrintable: Self {
            return filter { $0.isAscii }
        }
    }
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            let base64Encoded = "oPvvNMsiOd//bheeboamB65AXi8r+go9NLz2rTPEJRU="
            let decodedData = Data(base64Encoded: base64Encoded, options: [])
            var decodedString: String? = nil
            if let decodedData = decodedData {
                decodedString = String(data: decodedData, encoding: .ascii)
                print(decodedString)
                let ascivalue = decodedString!.asciiPrintable
                print(ascivalue)
            }
        }
        }

Output: 

Optional("???4?"9??nu{17}n|u{07}?@^/+ú
=4??-3?%u{15}")

4"9nn@^/+
=43%

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...