Exetools

Exetools (https://forum.exetools.com/index.php)
-   General Discussion (https://forum.exetools.com/forumdisplay.php?f=2)
-   -   Code snippet for Base34 Encoding (https://forum.exetools.com/showthread.php?t=19457)

TempoMat 03-13-2020 01:17

Code snippet for Base34 Encoding
 
Hi,

Does anyone has a code snippet for Base34 encoder/decoder preferably in x86 asm, C, C++, classic Delphi to share?

Thanks

PS:
In case anyone is wondering why I am posting here instead of in the request forum; well it is because I was inform that I don't have any permission to post there, when I tried to start a new thread.

Kurapica 03-13-2020 01:44

https://gist.github.com/JohannesMP/a911b7dc02bb0586e3111a0cbd2dc0e2

Credits to the original coder.

TempoMat 03-13-2020 02:17

Code snippet for converting a byteArray to Base34
 
Quote:

Originally Posted by Kurapica (Post 119526)
https://gist.github.com/JohannesMP/a911b7dc02bb0586e3111a0cbd2dc0e2

Credits to the original coder.

Thnaks for the link
Howover I have tried that and many others already and this particular one does not work for me, because that code snippet is for bases 2...36.

I forgot to state in my first post that the search is for conversion from ByteArray to Base34.

So to use this code snippet one has to first convert the byteArray (==BigNumber) to Binary or Base10 and then to Base34.

Alternative suggestion is to use BigInteger Library for the division, which I thought I could avoid.

runio 03-13-2020 02:37

https://en.wikipedia.org/wiki/Base36

Base34 is (0-9), (A-X)

Example code converted from Base36
Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static char *base34enc(long unsigned int value)
 {
        char base34[34] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWX";
        char buffer[14];
        unsigned int offset = sizeof(buffer);

        buffer[--offset] = '\0';
        do {
                buffer[--offset] = base34[value % 34];
        } while (value /= 34);

          return strdup(&buffer[offset]);
  }

static long unsigned int base34dec(const char *text)
{
    return strtoul(text, NULL, 34);
}

int main()
{
    printf("The number(unsigned long integer) is %lu\n", base34dec("ABCDEF"));
    return 0;
}


TempoMat 03-13-2020 03:02

@runio

If I am not mistaken the unsigned long integer type should be in the range [0, +18,446,744,073,709,551,615].
Therefore the code snippet will not work for a number like B9DDE784B6FFC653DFEC3E94D6B610 (965075674130144583043497186064512528) or larger.

Regards

sendersu 03-14-2020 03:33

One need to say "long long" in order to get that 18....... super massive number
otherwise its just 4^32 (4G) limit

https://en.wikipedia.org/wiki/C_data_types

chants 03-14-2020 07:46

This is pretty basic computer science stuff here. Converting between data types and bases. Since you appear to want to use bignums though the best we can give you is pseudo code for languages like C#or Java.

The implementation details of bignum which likely converts the byte array first into an int array for efficient computation are another question altogether. Though implementing a bignum library from scratch is not too difficult and a good learning experience.

So the byte array practically speaking is useless. You need division and modulo by 34 so no tricks there. Now base 32 as a power of 2 would be a different story. In conclusion get or make a bignum library, convert your byte array, and to be efficient make a diagram function and loop on it as you do the conversion. Gor decoding same thing just multiply by 34 and add.

chants 03-14-2020 10:23

DivMod or DivRem got autocorrected to diagram :)

TempoMat 03-14-2020 20:33

Quote:

Originally Posted by chants (Post 119540)
This is pretty basic computer science stuff here. Converting between data types and bases. Since you appear to want to use bignums though the best we can give you is pseudo code for languages like C#or Java.

I wanted to avoid using the BigNum library.

Quote:

Originally Posted by chants (Post 119540)
The implementation details of bignum which likely converts the byte array first into an int array for efficient computation are another question altogether. Though implementing a bignum library from scratch is not too difficult and a good learning experience.

So the byte array practically speaking is useless. You need division and modulo by 34 so no tricks there. Now base 32 as a power of 2 would be a different story. In conclusion get or make a bignum library, convert your byte array, and to be efficient make a diagram function and loop on it as you do the conversion. Gor decoding same thing just multiply by 34 and add.

Sorry for not making myself clearer.
I am familier with the use of BigNumber libraries.
For instances I have been able to factorize some modulus N numbers upto 512 Bits and with the help of an assembler code Ziggy (thanks a bunch for that) provided me some years ago, been able to keygen a lot of apps with the use of one or a combination of Drizz, Fleur or FGIntRSA libraries.

So on 12.03.2020 after my last post, I did the rough asm implementation with Fleur's BigNum library in the form
Code:

Base34CharSet='ABCDEFGHIJKLMNPQRSTUVWXYZ123456789' ; as an example
bnX ;== The BigNumber
bnY ;== TheBase in this case 34
bnRem
.repeat
  BigDiv32, bnX, bnY, bnRem, 0
  Out(i)=Base34CharSet(bnRem)
  inc i
.until bnX=0

The results were ok for a regular Base34 encoding.
I forgot to give a feedback after that. Sorry for that

The results however did not correspond to the samples of Base34Strings I fed in to the Progi to decode.
It is a simple RSA 120-Bit with what now looks like a home breed Base342ByteArray decoding routine.

Quote:

Originally Posted by chants (Post 119540)
Now base 32 as a power of 2 would be a different story.

Yes bases with powers of 2 are relative easy to implement.
An example will be to convert the ByteArray to binary bits and then convert the resulted bits in groups (size of a group corresponding to the nibble size of the base) for the indices to access the BaseXCharSet.

I must applaud the author for only including the Base34 decoding algorithm in his app and also not using those default ciphers in the BigNum library he used for the asymmetric encryption.

I will continue to analyze his decoding algorithm to see if I can somehow be able to reverse it.

Thanks again to all.

TempoMat 03-29-2020 01:36

Resolved:Code snippet for Base34 Encoding
 
Just want to give a feedback on how I resolved the problem:
There was a small trick I didn’t pay much attention to at the beginning.
The protection is an RSA-120 bits Sign algo for generating and then Base34 encoding the result to give key.
Now the trick with the decoding was what kept me trying different Base34 Encode/Decode routines.
This is the procedure used for generating or needed to create a valid key:
Code:

  1.        RSASign(15 ByteString)        =>Res1
  The 15.Byte is a checksum of the first 14 Bytes  with the bytes at position 1 and 3 interchanged
  2.        ByteArray2Binary(Res1)        =>Res2
  3.        First take and group the bits at the old positions of Res2 and then concat with the bits at the even positions=>Res3
  4.        RegistrationKey=Binary2Base34(Res3)

Regards,
TemPoMat

chants 03-29-2020 09:53

"ByteArray2Binary" sounds redundant as an array of bytes or words or hints, etc are already in binary form. Expansion to 1 byte per bit is pointless and just something a procedural oriented programmer would go for since its unnecessary unless some special vectorized code is utilized. Probably a lot of masterful bit twiddlers in the asm and RE world :)

"old" should be odd regardless congrats on solving.

TempoMat 03-29-2020 17:58

Quote:

Originally Posted by chants (Post 119629)
"old" should be odd regardless congrats on solving.

Yes it was supposed to be odd and not old.
Sorry for the mistake.

Nevertheless I believe the sentence was still understandable, given that in the same context was the phrase "concat with the bits at the even positions"

Cheers and everyone stay safe.
TemPoMat


All times are GMT +8. The time now is 15:22.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX