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 !!!!

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
  5. Hi, Charles here. I copied some part of your code. .Is it okay with you? :) :/

    ReplyDelete
  6. hi i need your help
    can you send me complete code please.
    parlinsaputra@gmail.com

    ReplyDelete
  7. can i have the entire code please
    ronicacoorey@gmail.com

    ReplyDelete
  8. hi! If you don't mind could you please send me entire code too?
    anxious.fear@mail.ru

    ReplyDelete
  9. Mam please send me the complete code at
    rao.asifalive@gmail.com

    ReplyDelete
  10. could you please send me full of this project please
    rizkys007@gmail.com

    ReplyDelete
  11. please send me full code of this project please
    mohamedsobhi889@gmail.com

    ReplyDelete
  12. please send me full code of this project please
    darkdreez@gmail.com

    ReplyDelete
  13. Bisa tolong bagikan sya kode lengkap dari affine cipher ini karna sya sangat membutuhkan untuk panduan tugas saya trimakasih

    ReplyDelete
  14. Greetings Sammani,

    Can I also Have the Full Code.
    Email: miard.zubair@gmail.com

    Thanks,
    Miard.

    ReplyDelete
  15. Hi Sammani,

    May I have this complete code of Affine cypher please. I need to finish a project.
    My e-mail is viscol.georgiana97@yahoo.com

    ReplyDelete
    Replies
    1. Hi please may I have the complete code my email is shyam424@hotmail.com
      This would be much appreciated
      Thanks in advance

      Delete
  16. Very nice blog post. Thanks for sharing such helpful blog post. Keep posting in future also.

    Web Development Company in Bangalore
    Website Development Company in Bangalore

    ReplyDelete
  17. We offer the best E-Commerce Website Design & Development Company In Mumbai, India. BrainCandy provides services like an Online store, B2B Marketplace website, and many more services.
    Please keep sharing this types of blog, "E-Commerece Website Design & Development Company In Mumbai, India"

    ReplyDelete
  18. Hi Sammani,

    May i have this complete code of Affine cypher please.

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Hello Sammani, can you elaborate more? And how can I get the code? Thank you in advance.
    Local citations

    ReplyDelete

Post a Comment

Popular posts from this blog

Responsive Web Design

Contract First Development in WCF 4.5