views:

85

answers:

3

I have several encoded strings and I need to decode them, my guess is they could be base40 encoding. I really need to decode, but it would be nice to do the encoding as well all in C#. BTW, I have tried all standard types of decoding before coming to the conclusion that it appears to be base40.

I couldn't find anything about base40 encoding/decoding, I found a lot of encoding information about similar things like base32 and of course base64 so I think it should be possible to do a base40.

Here is an example of the encoded string and the correct decoded value. I can provide several other examples if needed. All the string I want to decode are encoded SHA1 hashes.

ENCODED

/KUGOuoESMWYuDb+BTMK1LaGe7k=

DECODED

0xFCA5063AEA0448C598B836FE05330AD4B6867BB9

UPDATE: Turns out is the binary version of the SHA1 string that is then encoded to base64 which is why I was having trouble decoding it. I can give credit to Ignacio Vazquez-Abrams because he showed some output showing base64 but didn't explain his answer nor provided a C# example as requested. So I went and dug deeper thing about what his code was doing in what ever language that was and I came up with the answer I posted with a C# example how to do it.

+2  A: 

What you have there is run-of-the-mill Base64, not Base40.

>>> '/KUGOuoESMWYuDb+BTMK1LaGe7k='.decode('base64')
'\xfc\xa5\x06:\xea\x04H\xc5\x98\xb86\xfe\x053\n\xd4\xb6\x86{\xb9'
Ignacio Vazquez-Abrams
How so? When I decode that string as base64 i get garbage "��:�HŘ�6�3Զ�{�" When I endecode the known result I get "MHhGQ0E1MDYzQUVBMDQ0OEM1OThCODM2RkUwNTMzMEFENEI2ODY3QkI5". Base64 makes the the result bigger than the original, this is smaller than the original. I am not sure what language your sample code is in as well it is not the requested C#.
Rodney Foley
A: 

Okay it is Base64 BUT it the reason it is smaller is because it is a binary version of the sha1, so the only answer provided didn't really provide HOW I can do it and since I asked for C# code, here is one way to do it in C#:

string encoded = "/KUGOuoESMWYuDb+BTMK1LaGe7k=";
StringBuilder builder = new StringBuilder();
foreach (var b in Convert.FromBase64String(encoded))
    builder.Append(string.Format("{0:X}", b));
Console.Out.WriteLine(builder.ToString());
Rodney Foley
+2  A: 

Ignacio is right; that is base-64:

byte[] raw = Convert.FromBase64String("/KUGOuoESMWYuDb+BTMK1LaGe7k=");
foreach(byte b in raw) Console.Write(b.ToString("x2"));

Gives:

fca5063aea0448c598b836fe05330ad4b6867bb9
Marc Gravell
@marc look familar to my answer I post before yours. :) I also gave credit as Ignocio in my comments as being right but not providing a C# answer. I guess I will give it the check to you since you posted the first C# answer other than mine, and that was because of timing. :) Thanks guys!
Rodney Foley