Reputation: 3534
I have a string "ZB3NNxAMNB/x6JpAryCd0g==", which is decoded in java and stores in byte[]. But I am not sure that based on which characterset the decoding is happening in Java.
public byte[] decode(String src) {
return decode(src.getBytes(StandardCharsets.ISO_8859_1));
}
When I checked the method signature of decoding method in Java, shown above, the character set showing by default is ISO_8859_1. Does this meant the character set used for decoding by the following code is ISO_8859_1?
salt = Base64.getDecoder().decode(values.getProperty("s"));
I wanna do the same thing in ObjectiveC, which I did with the following code
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64SaltString options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSISOLatin1StringEncoding];
But I am not sure that the above code is right. Also, the difference is Java code stores the decoded data in to byte [] and in objectiveC, which is returning as String. So I am not able to compare the decoded data and ensure. Can anyone suggest me a way to compare the decoded data? Like converting the decoded data in to a single format, both iOS and Java. Looking forward for help.
Upvotes: 0
Views: 798
Reputation: 171
The java code shows nothing about the encoding. You must know which encoding the java sdk use:
System.out.println("Default Charset=" + Charset.defaultCharset());
if your java sdk runs on UTF-8, the code : src.getBytes(StandardCharsets.ISO_8859_1)
can change utf-8 encoding string to ISO_8859_1.
You can see the code in objective-c :
- (void)testEncoding {
NSString *originStr = @"ñèÒ";
//Encode :
NSData *utf8Data = [originStr dataUsingEncoding:NSUTF8StringEncoding];
NSData *latinData = [originStr dataUsingEncoding:NSISOLatin1StringEncoding];
NSLog(@"1.0: change a string's encoding ==>\n%@ ,%@",utf8Data,latinData);
NSString *utf8Base64String = [utf8Data base64EncodedStringWithOptions:0];
NSString *latinDataBase64String = [latinData base64EncodedStringWithOptions:0];
NSLog(@"1.1 encoding the data(byte[]) to base64string ==>\n%@ ,%@",utf8Base64String,latinDataBase64String);
//Decode :
NSData *utf8DecodeData = [[NSData alloc] initWithBase64EncodedString:utf8Base64String options:0];
NSData *latinDecodeData = [[NSData alloc] initWithBase64EncodedString:latinDataBase64String options:0];
NSLog(@"2.0 decode the base64string to data(byte[])==>\n %@ ,%@",utf8DecodeData,latinDecodeData);
NSString *utf8DecodeString = [[NSString alloc] initWithData:utf8DecodeData encoding:NSUTF8StringEncoding];
NSString *latinDecodeString = [[NSString alloc] initWithData:latinDecodeData encoding:NSISOLatin1StringEncoding];
NSLog(@"2.1 change the data(byte[]) the string with it's encoding ==>\n %@ ,%@",utf8DecodeString,latinDecodeString);
}
the result is
1.0: change a string's encoding ==>
<c3b1c3a8 c29ac392> ,<f1e89ad2>
1.1 encoding the data(byte[]) to base64string ==>
w7HDqMKaw5I= ,8eia0g==
2.0 decode the base64string to data(byte[])==>
<c3b1c3a8 c29ac392> ,<f1e89ad2>
2.1 change the data(byte[]) the string with it's encoding ==>
ñèÒ ,ñèÒ
when you change the string @"ñèÒ"
which is utf-8 encoding by default to NSISOLatin1StringEncoding
, you will get a totoal diffrent data <f1e89ad2>
and base64string 8eia0g==
For your situation, your java code src.getBytes(StandardCharsets.ISO_8859_1)
will get different value when change string to data(byte[]) depends on your java environment. So I do a test to decode your base64string, but I'm not sure which encoding is currect:
NSString *str = @"ZB3NNxAMNB/x6JpAryCd0g==";
NSData *data = [[NSData alloc] initWithBase64EncodedString:str options:0];
NSString *resultString = @"";
for(NSInteger i=1;i<16;i++) {
NSString *strDe = [[NSString alloc] initWithData:data encoding:i];
resultString = [NSString stringWithFormat:@"%@%zd => %@\n",resultString,i,strDe];
}
NSLog(@"%@",resultString);
the result is :
1 => dÍ74ñè@¯ Ò
2 => d˝74æŁÜ@fl µ¼
3 => (null)
4 => (null)
5 => dÍ74ñè@¯ Ò
6 => (null)
7 => (null)
8 => dヘ74咫ッ 旆
9 => dÍ74ńč@Ż Ň
10 => 搝촷ဌ㐟驀꼠鷒
11 => dН74сиљ@Ї ќТ
12 => (null)
13 => (null)
14 => (null)
15 => dÍ74ńčš@Ż ťŇ
Upvotes: 1