Sunday, April 20, 2014

Affine Cipher in C#

 

The affine cipher is a type of monoalphabetic substitution cipher, wherein each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule governing which letter goes to which. As such, it has the weaknesses of all substitution ciphers. Each letter is enciphered with the function (ax+b)\mod(26), where b is the magnitude of the shift.

For more information on affine cipher visit here

Following code is written in c# which has the affine cipher algorithm

  1.  
  2. public static string AffineEncrypt(string plainText, int a, int b)
  3. {
  4.     string cipherText = "";
  5.  
  6.     // Put Plain Text (all capitals) into Character Array
  7.     char[] chars = plainText.ToUpper().ToCharArray();
  8.  
  9.     // Compute e(x) = (ax + b)(mod m) for every character in the Plain Text
  10.     foreach (char c in chars)
  11.     {
  12.         int x = Convert.ToInt32(c - 65);
  13.         cipherText += Convert.ToChar(((a * x + b) % 26) + 65);
  14.     }
  15.  
  16.     return cipherText;
  17. }
  18.  
  19.  
  20. ///
  21. /// This function takes cipher text and decrypts it using the Affine Cipher
  22. /// d(x) = aInverse * (e(x)  b)(mod m).
  23. ///
  24. public static string AffineDecrypt(string cipherText, int a, int b)
  25. {
  26.     string plainText = "";
  27.  
  28.     // Get Multiplicative Inverse of a
  29.     int aInverse = MultiplicativeInverse(a);
  30.  
  31.     // Put Cipher Text (all capitals) into Character Array
  32.     char[] chars = cipherText.ToUpper().ToCharArray();
  33.  
  34.     // Computer d(x) = aInverse * (e(x)  b)(mod m)
  35.     foreach (char c in chars)
  36.     {
  37.         int x = Convert.ToInt32(c - 65);
  38.         if (x - b < 0) x = Convert.ToInt32(x) + 26;
  39.         plainText += Convert.ToChar(((aInverse * (x - b)) % 26) + 65);
  40.     }
  41.  
  42.     return plainText;
  43. }
  44.  
  45. ///
  46. /// This functions returns the multiplicative inverse of integer a mod 26.
  47. ///
  48. public static int MultiplicativeInverse(int a)
  49. {
  50.     for (int x = 1; x < 27; x++)
  51.     {
  52.         if ((a * x) % 26 == 1)
  53.             return x;
  54.     }
  55.  
  56.     throw new Exception("No multiplicative inverse found!");
  57. }

Happy Coding !!!!

9 comments :

  1. Hi Sammani,

    May i have this complete code of Affine cypher please. I need to finish one project but i am far from programming at all.

    mali

    ReplyDelete
    Replies
    1. Hi Mali,

      Can you send me your email address. I will email you the source code.

      Delete
    2. Thank you very much Sammani i really appreciate.

      My email is malipylli@gmail.com

      Delete
    3. May i have this complete code of Affine cypher please.
      My email is mneer54@gmail.com

      Delete
  2. your code is the best thing i found on the internet. well done.

    ReplyDelete
    Replies
    1. May i have this complete code of Affine cypher please.
      My email is tacisercetin@gmail.com

      Delete
  3. can you please email me this compelete code .
    alfiamushtaq18@gmail.com

    ReplyDelete
  4. please send your complete code including libraries to the following email. i will really appreciate it sammani
    wajeeha094@gmail.com

    ReplyDelete