Decoding an obfuscated value - Unable to convert data to string - ios

I am working on importing a file from a legacy C++ codebase. The file itself is technically a JSON but one of the values is obfuscated with an XOR function in the C++ codebase before I receive it and looks something like this.
{"version": 15, "data": "C\u0016Q45\u0010 46QY\\\u0011\n\u0019a\u0003\u0019}\u001apg"}
The value for "data" is technically a UTF8 string.
I know the algorithm used to decode that data into a usable JSON string. However, I am unable to get swift to parse this into a [String : String] apparently due to formatting issues.
Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 31."
So far I have managed to get the specific data in question by isolating the subdata
let encryptedData = data.subdata(in: dataPrefixLength ..< dataLength - 2)
Even still, I cannot seem to get swift to parse this into a String when I attempt to do so with UTF8 encoding.
String(data: encryptedData, encoding: .utf8)
It's also worth noting that the String description seems alright in the debugger when inspecting the data itself.
I'd really appreciate any advice.
Thanks!

From your example it looks as if the obfuscation generates binary data and the binary data is put into a string. That's not allowed in JSON. Therefore, it's invalid JSON and any parser with a reasonable amount of validation will reject it.
As an example, take the start of the string:
"C\u0016
It starts with the character "C". That's valid.
Then an escaped character with a hexadecimal values follows: 0016. However, U+0016 is not a valid Unicode codepoint. Therefore it's rejected. It's probably supposed to be the binary byte 0x16. But you can't put that into a JSON string.
You have two options:
Fix the source of the data, e.g. by using a Base64 encoding before putting binary data into JSON.
Write your own JSON parser to handle the invalid JSON.

Related

Compare two String Types?

I am trying to compare two strings. But this is where I do not understand.
First String comes from a URLSession. What I am doing is, convert incoming data of URLSession, to String using,
let phpResponse = String(data: data!, encoding: .utf8);
And Now I need to compare this string with Plain String which is,
let isWhat = "Success";
But when I try to compare them using ==operator, it says strings are not similar. Actually these two variables shows the same content when I print them into Console.
Then I tried to find the types of these two variables using type(of:) operator and both variables were String. I'm not sure what I am missing here. Can someone help me? Why it says that these two Strings are not same?

Multiple encryption append to a file

I have a log of a programs state. This log can be manualy or time interval saved on a file for persistant storage. Before saving it to the file it is encrypted with RNCryptor.
My current appending(saving) to file flow:
Read file
Decript information from the read string
Concat decrypted string with the new string
Encrypt the concatenated string
Write it to file
What I imagine:
Encode new string
Append to file
When I read this I will have to build a string from all the encoded strings. But I don't know how to decrypt the file with multiple encrypted blocks in it. How to differentiate where one ends and another begins.
Also is this the best performance choice. The text in the file at maximum could get to 100MB(Possibly it will never get this big).
Is using Core Data viable? Each append as different record or something. And core data could be encrypted so no need for RNCryptor.
Would appreciate code in Objective-C if any.
There are many things you can do:
Easiest would be to encode the ciphertexts to text (e.g. with Base64) and write each encoded ciphertext to a new line. You need encoding for that, because the ciphertext itself might contain bytes that can be interpreted as newline control characters, but that won't happen with a text encoding. The problem with this is that it blows up the logs unnecessarily (e.g. by 33% if Base64 is used)
You can prepend each unencoded ciphertext with its length (e.g. big-endian int32 encoding) and write both as-is to a file in binary mode. If you begin reading the file from the beginning, then you can distinguish each ciphertext, because you know how long the following ciphertext is and when the next encoded length starts. The blowup is only as big as the encoding of the ciphertext length for each ciphertext.
Use a binary delimiter such as 0x0101 between ciphertexts, but such a delimiter might still appear in the ciphertexts, so you need to escape it if you find it somewhere in the ciphertext. This is a little tricky to get right.
If the amount of logs is small (few MB), then you can find a library to append to a ZIP file.
You can use the array to store the information and then read and write that array to file. find Example here.
Steps :
Read Array from the file.
Add the New Encrypted string to array.
Write array to file.

Convert unicode characters to their respective language letter in ios

I've unicode character text for indian language(telugu) like this
పురాణాలు
I'm getting the above text from database to an xml file format. I'm reading the xml file and
when i am printing the text it is showing as పురాణాలు
Is there any way print the text as it is without any encoded character type &#...?
How are you parsing the XML? A proper XML parser should decode the numeric references.
I'm guessing that you are attempting to hand parse an XML document instead of relying on NSXMLParser. If so, you really should use an XML parser. Bad Guess on my part, it's likely that the entities are being double encoded.
To answer your question directly, Objective C HTML escape/unescape shows how to decode entities with a quick and dirty method.

Rails oracle raw16

I'm using Rails 3.2.1 and I have stuck on some problem for quite long.
I'm using oracle enhanced adapter and I have raw(16) (uuid) column and when I'm trying to display the data there is 2 situations:
1) I see the weird symbols
2) I'm getting incompatible character encoding: Ascii-8bit and utf-8.
In my application.rb file I added the
config.encoding = 'utf-8'
and in my view file I added
'#encoding=utf-8'
But so far nothing worked
I also tried to add html_safe but it failed .
How can I safely diaply my uuid data?
Thank you very much
Answer:
I used the unpack method to convert the
binary with those parameters
H8H4H4H4H12 and in the end joined the
array :-)
The RAW datatype is a string of bytes that can take any value. This includes binary data that doesn't translate to anything meaningful in ASCII or UTF-8 or in any character set.
You should really read Joel Spolsky's note about character sets and unicode before continuing.
Now, since the data can't be translated reliably to a string, how can we display it? Usually we convert or encode it, for instance:
we could use the hexadecimal representation where each byte is converted to two [0-9A-F] characters (in Oracle using the RAWTOHEX function). This is fine for display of small binary field such as RAW(16).
you can also use other encodings such as base 64, in Oracle with the UTL_ENCODE package.

Visual Basic.NET "The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'System

I am trying to read a binary file in which i have been appending data using a BinaryWriter object. I keep getting this error:
"The output char buffer is too small to contain the decoded
characters, encoding 'Unicode (UTF-8)' fallback
'System.Text.DecoderReplacementFallback'."
My file has characters like | which i suspect are the problem but I don't know how to solve it.
The most probable reason is that your file contains some binary data, that does not represent valid UTF-8 codepoint, at the place from which you are trying to read UTF-8 character.
This can happen if your read algorithm lose "synchronization" with your write algorithm and tries to read character from the wrong place, where something else (not a character) was written.

Resources