Logo




Subscribe:
RSS 2.0 | Atom 1.0
Categories:

Sign In


[Giagnocavo]Michael::Write()

 Tuesday, January 13, 2004
Base32 in .NET
I haven't seen any .NET Base32 implementations, but various people have expressed interest in having some simpler way to represent binary data (such as an encrypted keycode).  So, I'm posting a sample Base32 encoding.  Note that this does not conform to the Base32 standard encoding, but uses it's own set of characters (useful for keycodes, where we don't want to have to differentiate between 0 and O.  Thanks to Juan Gabriel for making the code much better :).

Update 2004-2-5: Thanks to Philippe Cheng for fixing a bug that caused extra (harmless) output. (See comments for details).

using System;
using System.Text;
 
public sealed class Base32 {
 
      // the valid chars for the encoding
      private static string ValidChars = "QAZ2WSX3" + "EDC4RFV5" + "TGB6YHN7" + "UJM8K9LP";
 
      /// <summary>
      /// Converts an array of bytes to a Base32-k string.
      /// </summary>
      public static string ToBase32String(byte[] bytes) {
            StringBuilder sb = new StringBuilder();         // holds the base32 chars
            byte index;
            int hi = 5;
            int currentByte = 0;
 
            while (currentByte < bytes.Length) {
                  // do we need to use the next byte?
                  if (hi > 8) {
                        // get the last piece from the current byte, shift it to the right
                        // and increment the byte counter
                        index = (byte)(bytes[currentByte++] >> (hi - 5));
                        if (currentByte != bytes.Length) {
                              // if we are not at the end, get the first piece from
                              // the next byte, clear it and shift it to the left
                              index = (byte)(((byte)(bytes[currentByte] << (16 - hi)) >> 3) | index);
                        }
 
                        hi -= 3;
                  } else if(hi == 8) {
                        index = (byte)(bytes[currentByte++] >> 3);
                        hi -= 3;
                  } else {
 
                        // simply get the stuff from the current byte
                        index = (byte)((byte)(bytes[currentByte] << (8 - hi)) >> 3);
                        hi += 5;
                  }
 
                  sb.Append(ValidChars[index]);
            }
 
            return sb.ToString();
      }
 
 
      /// <summary>
      /// Converts a Base32-k string into an array of bytes.
      /// </summary>
      /// <exception cref="System.ArgumentException">
      /// Input string <paramref name="s">s</paramref> contains invalid Base32-k characters.
      /// </exception>
      public static byte[] FromBase32String(string str) {
            int numBytes = str.Length * 5 / 8;
            byte[] bytes = new Byte[numBytes];
 
            // all UPPERCASE chars
            str = str.ToUpper();
 
            int bit_buffer;
            int currentCharIndex;
            int bits_in_buffer;
 
            if (str.Length < 3) {
                  bytes[0] = (byte)(ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
                  return bytes;
            }
 
            bit_buffer = (ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
            bits_in_buffer = 10;
            currentCharIndex = 2;
            for (int i = 0; i < bytes.Length; i++) {
                  bytes[i] = (byte)bit_buffer;
                  bit_buffer >>= 8;
                  bits_in_buffer -= 8;
                  while (bits_in_buffer < 8 && currentCharIndex < str.Length) {
                        bit_buffer |= ValidChars.IndexOf(str[currentCharIndex++]) << bits_in_buffer;
                        bits_in_buffer += 5;
                  }
            }
 
            return bytes;
      }
}

 
Code | Security
Tuesday, January 13, 2004 1:22:46 PM UTC  #    Comments [3]  |  Trackback

Learning languages with Office 2003

I'm currently studying Korean.

I just finished downloading all four Office 2003 MUI CDs, and installed Japanese, Korean, and Chinese (Traditional and Simplified) MUIs for XP and Office, complete with speech and handwriting.

It's great!  I use Word a lot now to test spelling/grammar, get simple definitions, find the Hanja (Korean for Chinese character) for a word and vice-versa, and so on.

I'd like to learn Japanese someday, but currently all I know I've learned from watching anime.  Even so, the Japanese IME is easy enough to use that I can type in (not knowing much Hiragana)  something I hear in a movie, and translate from that, right inside Word.  Very interesting.

With a digitizer pad, I can practise my Hangul (Korean alphabet) too.  Probably would have been more useful a while ago, as I can type Korean as fast as I write it now, and I'd imagine I'll soon be typing it much faster than I write (as it should be). 

Personal
Tuesday, January 13, 2004 1:14:54 PM UTC  #    Comments [0]  |  Trackback

 Thursday, January 08, 2004
TSA: Bathroom lines dangerous

http://news.bbc.co.uk/2/hi/asia-pacific/3376459.stm

“passengers are asked not to congregate near the planes' toilets“

TSA spokesperson: "We frequently say security is not a spectator sport... we can't be successful about stopping terrorism without everyone playing a role“

The point he misses is that the role of the passengers shouldn't be to prevent terrorism.  The plane itself should handle that.  If having passengers “congregate” near toilets is a real threat, then the plane has much more serious security problems that need to be dealt with.  Where did they find these people who come up with this nonsense?  TSA == Totally Senile Administration?

Security
Thursday, January 08, 2004 4:29:59 PM UTC  #    Comments [0]  |  Trackback

 Tuesday, December 30, 2003
Secure it? Nah, let's just make it illegal.

Stoplights can be changed to green via an infrared signal.  This is so that police and ambulances can get through traffic faster.  Some people are using it to their own benefit.  So, the US government is trying to make these devices illegal if not authorized.  Hmm, sounds like cell phone companies when responding to the thread of eavesdroppers.  Why bother adding any security measures when we can just make it illegal?  I mean, no one ever breaks the law, right?

http://www.omaha.com/index.php?u_np=0&u_pg=1636&u_sid=958234

Security
Tuesday, December 30, 2003 3:41:18 AM UTC  #    Comments [2]  |  Trackback

The First Post
Hi there.  The obligatory first post.  In this blog, I'm going to talk about .NET programming (couldn't have guessed that from the title eh?) and other interesting items (security) as they come along.  Thanks for reading.
Tuesday, December 30, 2003 3:36:33 AM UTC  #    Comments [1]  |  Trackback