ios - AES encrypt string appear \r\n -
when use aes128 encrypt string, if encrypted string long contain \r\n in it.
now have use empty string replace it. why encrypt-string contain \r\n , better way avoid or fix it. thanks.
answers: it's caused base64 encoding process, every 64 characters insert \r\n .
that base64 encoded string.
actual encryption output array of 8-bit bytes, not characters. code base64 encoding encrypted data option insert line breaks every 64 characters, allow better printing of output. when decoded use nsdatabase64decodingignoreunknowncharacters
option remove line breaks .
in particular objective-c create base64 string nsdata
is:
- (nsstring *)base64encodedstringwithoptions:(nsdatabase64encodingoptions)options
the options
include:
nsdatabase64encoding64characterlinelength
set maximum line length 64 characters, after line ending inserted.
which inserts "\r\n" (carriage return, new line) characters each 64 characters.
if not want pass 0 option value.
to decode base64 use objective-c method:
- (instancetype)initwithbase64encodedstring:(nsstring *)base64string options:(nsdatabase64decodingoptions)options
with option: nsdatabase64decodingignoreunknowncharacters
.
apple code:
the default implementation of method reject non-alphabet characters, including line break characters. support different encodings , ignore non-alphabet characters, specify options value of nsdatabase64decodingignoreunknowncharacters.
the thing gives away base64 string length multiple of 4, characters used "a-za-z0-9+/" , trailing "=" characters.
historic note: these days on osx , ios line breaks single "\n" (0x0a) line feed character. when used teletypes terminals "\r" (0x0d) carriage return moved carriage or print head did not move paper next line. "\n" newline moved paper 1 line did move carriage or print head back. 2 distinct mechanical operations. later systems used either "\r\n", "\n\r", "\r" or "\n". unix choose "\n" , osx , ios.
Comments
Post a Comment