Saturday, July 28, 2012

Caesar Cipher - RotN


Caesar Cipher, which is also called a substitution cipher, replaces each letter of the alphabet with the three places after that original alphabet. So for a Caesar Cipher the alphabet pattern would be

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

Example : Using Caesar Cipher  “Rapid bits”  translates to  udslg elwv”. Here Rapid bits is plain message (P)  where as “udslg elwv” is  ciphered message (C)

So, if we generalize the idea of replacing each character with a new letter after nth letter, we arrive to the following equation. This can be easily tested by assigning a number to each letter as





Encrypted message is a function of position, so  C=E(P)=f(P+n-1)%26  + 1
Above message can be Decrypted as P=D(C)=(C-n+25)%26 +1

Note in the above equations % is a modulus

Rapid  bits  original number coding without shifting positions is
18 1 16 9 4   2 9 20 19
then if we shift 3 positions, 1st letter “R”,  with number 18, transforms to
C=E(18+3-1)%26 + 1 = 20+1=21
Here is a encrypted message with its number values
21 4 19 12 7  5 12 23 22.

So what is secret here? As you already noticed shifting a letter to nth character is a secret. Since there are only 26 alphabets in English, there are only 25 secret keys. Using brute force attack it is easy to decrypt the messages. However  ASCII table numbering is different from the above 1 to 26 number identification system.

 
 
Here is a script to encrypt a message by choosing a rotating position. You may need to figure out how to decrypt for nth rotation by reversing the flow for different positions.  Rot5, Rot13, Rot18, Rot47 are widely used, but they are not safe at all. Rot is an acronym for rotation.


1)      RotN Encryptor
private string RotNEncrypt(string s, int n, bool includeNumbers)
        {
            string result = "";
            char[] carray = s.ToCharArray();
            for (int i = 0; i < carray.Length; i++)
            {
                int ascii = carray[i];
                int rot = ascii;
                if (ascii > 64 && ascii < 91)
                {
                    rot = rot + n;                   
                    if (rot > 90)
                    {
                        rot += -90 + 64;
                    }
                    if (rot < 65)
                    {
                        rot += -64 + 90;
                    }
                }
                // Lowercase letters are 97 to 122
                else if (ascii > 96 && ascii < 123)
                {
                    rot = rot + n;
                    if (rot > 122) rot += -122 + 96;
                    if (rot < 97) rot += -96 + 122;
                }
                // Numeric digits are 48 to 57
                if (includeNumbers == true && ascii > 47 && ascii < 58)
                {
                    rot = rot + n;
                    if (rot > 47) rot += -57 + 47;
                    if (rot < 58) rot += -47 + 57;
                }

                char character = (Char)rot;
                result = result + character.ToString();
            }
            return result;
        }


2)      Rot13 Encrypter and Decrypter

private string Rot13(string s)
        {
            if (String.IsNullOrEmpty(s)) return s;
            char[] character = s.ToCharArray();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (c >= 97 && c <= 122)
                {
                    int rot = c + 13;
                    if (rot > 122) rot -= 26;
                    character[i] = (char)rot;
                }
                else if (c >= 65 && c <= 90)
                {
                    int rot = c + 13;
                    if (rot > 90) rot -= 26;
                    character[i] = (char)rot;
                }
                else
                {
                    character[i] = c;
                }
            }
            return new string(character);
        }


3)      Rot47 Encryptor and Decryptor

private string Rot47(string s)
        {
            string cipher = "";
            foreach (char c in s.ToCharArray())
            {
                if (c != ' ')
                {
                    int rot = c;
                    rot += 47;
                    if (rot > 126) rot -= 94;
                    if (rot < 33) rot += 94;
                    cipher = cipher + (char)rot.ToString();
                }
                else
                {
                    cipher = cipher + " ";
                }
              
            }
            return cipher;
        }

Monday, July 23, 2012

Get Year, Month, Date From SQL server

If a datetime value stored as int in database like 20120707 the following SQL gives us year as 2012, date as 7, and month as 7

Declare @LocalDateID int
set @LocalDateID=20120707

SELECT DATEPART(year, cast(@LocalDateID as char(8))) [Year],
DATEPART(month, cast(@LocalDateID as char(8))) [Month],
DATEPART(day, cast(@LocalDateID as char(8))) [Date]

Wednesday, July 11, 2012

Researchers and Entrepreneurs- Why, what and how



While reading ASR techniques for Indic languages and HMM, I came across an interesting post and interviews by Prof Raj Reddy ( A famous scientist in AI ) . Few useful questions for researchers and entrepreneurs

Following the wise words in http://seanwise.typepad.com/ the key questions for an entrepreneur include:
  1. Why does anyone (end-user/customer) need this? 
  2. Why you? Why do you have a competitive advantage in brining this product to market? 
  3. Why can't others just copy you? 
  4. Why is it novel, different, better, than anything else? 
  5. How much cash do you want? What are you going to do with it? And what will the results be? 
  6. What's it worth? And why is it worth that much?


Prof. Raj Reddy's Questionnaire for a research proposal/problem is:

  1. What are we trying to do? 
  2. What is the key make-a-difference capability we bring to the table? 
  3. How is it being done today?, How are others approaching the problem? 
  4. What are the limitations of current/proposed approaches? 
  5. What is new in the approach? 
  6. What is the plan for realizing the goal? 
  7. if you succeed who will care? What is the impact? 
  8. How much will it cost and how long will it take? 
  9. What are the mid-term and final exams?

The use of "why" type of questions for entrepreneurs and use of "what" and "how" type of questions for researchers is interesting. And also the final questions for a researcher seems to be the beginning questions for an entrepreneur.

Tuesday, July 10, 2012

Convert yyyymmdd (int ) to Datetime and Datetime to yyyymmdd in C#


string _StartDate = "20120707";
 if (!String.IsNullOrEmpty(StartDate))
      {
            DateTime _ExpiryDate = DateTime.ParseExact(_StartDate, "yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
         }


This will convert 20120707 to 7/7/2012 12:00:00 AM


Now given _ExpiryDate as 7/7/2012 12:00:00 AM, The following will convert from Date to yyyymmdd format


 _ExpiryDate.ToString("yyyyMMdd", System.Globalization.CultureInfo.GetCultureInfo("en-US"));