D
D
davidnum952016-01-19 11:49:26
Algorithms
davidnum95, 2016-01-19 11:49:26

How to rewrite this algorithm in C#?

Good afternoon. There is some algorithm for decrypting a string encrypted in base64 on adobe as3. The string is taken from the arraybyte field from the json file. The essence is not entirely clear, so I just decided to rewrite the code in C # on the forehead. The output should be a meaningful text, but krakozyabry comes out.
Original adobe as3 code obtained by disassembler:

//сам алгоритм
private function descriptBFF(base64:String) : String
      {
         var s:String = "";
         var i:uint = 1;
         var length:uint = base64.charCodeAt(0);
         if(length % 2 == 1)
         {
            length--;
         }
         while(i < length)
         {
            s = s + (base64.charAt(i + 1) + base64.charAt(i));
            i = i + 2;
         }
         s = s + base64.slice(length + 2);
         return s;
      }

//его использование
 b64 = new Base64Decoder();
 b64.decode(this.descriptBFF(book.bytearray));

My implementation in C#:
//Алгоритм
public static string DecryptBFF(string base64) {
      string s = "";
      int i = 1;
      int lenght = (Int32)base64[0];
      
      if(lenght % 2 == 1) {
        lenght--;
      }
      
      while(i < lenght) {
        s = s + (base64[i + 1] + base64[i]);
        i = i + 2;
      }
      s = s + base64.Substring(lenght + 2);
         	return s;
    }
//Использование
byte[] data;
data = Convert.FromBase64String(DecryptBFF(book.Content));
book.Content = Encoding.UTF8.GetString(data);

If anyone is interested, I can send the JSON file.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Nemiro, 2016-01-20
@davidnum95

Summarizing the discussion in the comments.
bytearray - does not contain text. This is the body of the Flash file .
In a string:
Treat characters as strings, not as character codes:
Otherwise, there are no errors. The usage code is something like this:

byte[] data;
data = Convert.FromBase64String(DecryptBFF(book.Content));
File.WriteAllBytes("output.swf", data);

The output.swf file can be opened in Flash player or browser.
Let's hope it's legal :-)

V
Vladimir Martyanov, 2016-01-19
@vilgeforce

And why are you reinventing the wheel with base64 decoding? Ready not to be found or what?

A
Ai Lab, 2016-01-20
@vpuhoff

you apparently repeated the fraud a little more than completely, the problem is most likely just in the encoding, Encoding.UTF8.GetString(data);, who said that it was UTF8?) You probably need to try everything and one of them will return a meaningful text

G
Giwinax, 2019-02-06
@Giwinax

in the end, were able to figure out how to decode these krakozyabry? I’m sitting on this problem myself now, I rewrote the function on the pluses, the result is zero, only the first ~ 100 characters of the ubff file change, subsequent attempts to run through base64 did not give results, I would like to hear your solution, if there is one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question