Java Program to Implement Affine Cipher

This is a java program to implement Affine Cipher. 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.

Here is the source code of the Java Program to Implement Affine Cipher. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class AffineCipher
  7. {
  8.     public static String encryptionMessage(String Msg)
  9.     {
  10.         String CTxt = "";
  11.         int a = 3;
  12.         int b = 6;
  13.         for (int i = 0; i < Msg.length(); i++)
  14.         {
  15.             CTxt = CTxt + (char) ((((a * Msg.charAt(i)) + b) % 26) + 65);
  16.         }
  17.         return CTxt;
  18.     }
  19.  
  20.     public static String decryptionMessage(String CTxt)
  21.     {
  22.         String Msg = "";
  23.         int a = 3;
  24.         int b = 6;
  25.         int a_inv = 0;
  26.         int flag = 0;
  27.         for (int i = 0; i < 26; i++)
  28.         {
  29.             flag = (a * i) % 26;
  30.             if (flag == 1)
  31.             {
  32.                 a_inv = i;
  33.                 System.out.println(i);
  34.             }
  35.         }
  36.         for (int i = 0; i < CTxt.length(); i++)
  37.         {
  38.             Msg = Msg + (char) (((a_inv * ((CTxt.charAt(i) - b)) % 26)) + 65);
  39.         }
  40.         return Msg;
  41.     }
  42.  
  43.     public static void main(String[] args)
  44.     {
  45.         Scanner sc = new Scanner(System.in);
  46.         System.out.println("Enter the message: ");
  47.         String message = sc.next();
  48.         System.out.println("Message is :" + message);
  49.         System.out.println("Encrypted Message is : "
  50.                 + encryptionMessage(message));
  51.         System.out.println("Decrypted Message is: "
  52.                 + decryptionMessage(encryptionMessage(message)));
  53.         sc.close();
  54.     }
  55. }

Output:

$ javac AffineCipher.java
$ java AffineCipher
 
Enter the message: 
SANFOUNDRY
Message is :SANFOUNDRY
Encrypted Message is : VTGIJBGCSN
9
Decrypted Message is: SANFOUNDRY

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.