OStack程序员社区-中国程序员成长平台

标题: ios - 在 iOS 上解码 base64 数据 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 02:04
标题: ios - 在 iOS 上解码 base64 数据

我有一个 base64 字符串,我可以在 macOS 上通过将字符串分成每行 16 个字节(字符)然后使用 OpenSSL 进行解码来解码:

示例data.enc:

RwSBFR7iQ5vQYLqZ
E60hJEyOaU4VVGcq
tvnB2A1zQEylNdN9
tTZIOfwGMwDbMZln
nvT7lm80L1MkvDNH
qivdRAV4Ymtizw3F
d3GWBZ+xFVIAk0H3
lQWzAr43G7TRCYfc
r5Ijuj0qGIEF1FLp
Y6tlYNMI0C3J6UXH
qnd3vu4XuwIHi1gV
ciOxuP+iqgjvjM3O
qy8sU0YITC3ywxPA
Sh14azvcPu9F1/KY
S1J1JzN3XsTzscHf
kdry0hbz5H0l+6Um
Uz9Sb9HTGxxx9mwF
B821I8DfZc2RGx8W
mFuaNzgLFhS6CdwF
G9yALPk4wBUqIxEA
y4w/oLPosQqZ7Z9L
JnQ4SA==

解码命令:

openssl enc -d -base64 -in data.enc -out data.bin

这是 RAW base64 编码的字符串:

R3LSQUyakgQKXoxCqw/v/o2LwPPthWSOgoyCKV/p60WE7tIVLTIf0mGiSGhXwK91cXYygsCoXyTEdMY4bquMhhV09R2DXVVD79IXBp+7mGSdTcYh4FUvZCMUmWCqjje2PiZUXIzfDgwrJcxKplNrX6T/oc70quz/X4rZv1Qu6BMDBzF/otsC0Q1qx3QzUobxsRaUaCltb1CSdExTZ3zWR8anuz5eaBct3ymM8XNA8zYv6CB44/P+yS6CqGOza/a5R5x04A06Un0hu+d+hsSxW2F9zdeODz3t7cPVZYiW9nyC9XzIXH01Oi25uHuC0otxLlfXU4adMHB4Lj6YrA3Uxg==

data.bin 中的数据是 PKI 加密的,我可以在 iOS 上很好地解密它。如果我 cat data.bin 我看到(仅显示前几个字符)加密数据:

Gr?AL?? ^?B????????d????)_??E???-2?a?HhW??uqv2???

但是,如果我尝试在 iOS 上:

NSString *path = [[NSBundle mainBundle] pathForResource"data" ofType"enc"];
NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSData *data = [[NSData alloc] initWithBase64EncodedString:text options:0];

那么数据总是nil



Best Answer-推荐答案


注意:Base64 解码输出不是字符串,它是二进制数据。返回 nil 是因为它不是 UTF-8 字符串,并且任意数据通常不是有效的 UTF-8。

NSString *base64 = @"RwSBFR7iQ5vQYLqZE60hJEyOaU4VVGcqtvnB2A1zQEylNdN9tTZIOfwGMwDbMZlnnvT7lm80L1MkvDNHqivdRAV4Ymtizw3Fd3GWBZ+xFVIAk0H3lQWzAr43G7TRCYfcr5Ijuj0qGIEF1FLpY6tlYNMI0C3J6UXHqnd3vu4XuwIHi1gVciOxuP+iqgjvjM3Oqy8sU0YITC3ywxPASh14azvcPu9F1/KYS1J1JzN3XsTzscHfkdry0hbz5H0l+6UmUz9Sb9HTGxxx9mwFB821I8DfZc2RGx8WmFuaNzgLFhS6CdwFG9yALPk4wBUqIxEAy4w/oLPosQqZ7Z9LJnQ4SA==";
NSLog(@"base64: %@", base64);
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64 options:nil];
NSLog(@"data: %@", data);

输出:

base64: RwSBFR7iQ5vQYLqZE60hJEyOaU4VVGcqtvnB2A1zQEylNdN9tTZIOfwGMwDbMZlnnvT7lm80L1MkvDNHqivdRAV4Ymtizw3Fd3GWBZ+xFVIAk0H3lQWzAr43G7TRCYfcr5Ijuj0qGIEF1FLpY6tlYNMI0C3J6UXHqnd3vu4XuwIHi1gVciOxuP+iqgjvjM3Oqy8sU0YITC3ywxPASh14azvcPu9F1/KYS1J1JzN3XsTzscHfkdry0hbz5H0l+6UmUz9Sb9HTGxxx9mwFB821I8DfZc2RGx8WmFuaNzgLFhS6CdwFG9yALPk4wBUqIxEAy4w/oLPosQqZ7Z9LJnQ4SA==

data: <47048115 1ee2439b d060ba99 13ad2124 4c8e694e 1554672a b6f9c1d8 0d73404c a535d37d b5364839 fc063300 db319967 9ef4fb96 6f342f53 24bc3347 aa2bdd44 0578626b 62cf0dc5 77719605 9fb11552 009341f7 9505b302 be371bb4 d10987dc af9223ba 3d2a1881 05d452e9 63ab6560 d308d02d c9e945c7 aa7777be ee17bb02 078b5815 7223b1b8 ffa2aa08 ef8ccdce ab2f2c53 46084c2d f2c313c0 4a1d786b 3bdc3eef 45d7f298 4b527527 33775ec4 f3b1c1df 91daf2d2 16f3e47d 25fba526 533f526f d1d31b1c 71f66c05 07cdb523 c0df65cd 911b1f16 985b9a37 380b1614 ba09dc05 1bdc802c f938c015 2a231100 cb8c3fa0 b3e8b10a 99ed9f4b 26743848>

关于ios - 在 iOS 上解码 base64 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44912522/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4