Implementation of Affine Cipher in Java

In this program, we are going to study the Affine Cipher Algorithm that provides us the monopolistic solution of the given input. For this purpose, we are using the Java(core) programming language.

Implementation of Affine Cipher Algorithm in Java (core)

  1. class Affin
    { 
      static int a = 17; 
      static int b = 20; 
    
      static String Message(char[] msg) 
      { 
        String cipher = ""; 
        for (int i = 0; i < msg.length; i++) 
        { 
          if (msg[i] != ' ') 
          { 
            cipher = cipher 
                + (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A'); 
          }
          else 
          { 
            cipher += msg[i]; 
          } 
        } 
        return cipher; 
      }

     

  2. Here first we have created a class named Affin and declared two variables named ‘p’ and ‘q’ and assigned integer values to them. Then we created a new Method name ‘Message’and inside it initially the cipher text is empty. In cipher each letter is written.
  3. The main idea is enciphered with function (ax+b) and mod of 26. Likewise using this idea we get the Decrypted string.
  4. static String Cipher(String cipher) 
      { 
        String msg = ""; 
        int a_inv = 0; 
        int flag = 0; 
        for (int i = 0; i < 26; i++) 
        { 
          flag = (a * i) % 26; 
          if (flag == 1) 
          { 
            a_inv = i; 
          } 
        } 
        for (int i = 0; i < cipher.length(); i++) 
        { 
          if (cipher.charAt(i) != ' ') 
          { 
            msg = msg + (char) (((a_inv * 
                ((cipher.charAt(i) + 'A' - b)) % 26)) + 'A'); 
          } 
          else 
          { 
            msg += cipher.charAt(i); 
          } 
        } 
    
        return msg; 
      }

     

  5. The Range of ASCII alphabets is between (65-90). Here first it will execute the for instruction so it will set the flag=1. The flag is set to 1 and take the multiplicative inverse of the number. And convert the number to its ASCII value between (65-90||A-Z).
  6. If the flag is not set to 1 that is if flag=0 then it will just skip all these steps and just simply append the space character.
  7. public static void main(String[] args) 
      { 
        String msg = "CODESPEEDY"; 
        String cipherText = Message(msg.toCharArray()); 
        System.out.println("Encrypted Message is : " + cipherText);  
        System.out.println("Decrypted Message is: " + Cipher(cipherText)); 
    
      } 
    } 
    

     

  8. Here we just call the two methods that we have written that is the Cipher and Message Functions. And we just pass the string input as “Code Speedy”. The output we get is the Encrypted string and Decrypted string.
  9.  We enter the string and find the result of the string and set the flag is set to 1. It finds the multiplicative inverse and using the ASCII values it finds the encrypted and the decrypted string.
  10. OUTPUT:
  11. Encrypted Message is : CYTKOPKKTM
    Decrypted Message is: CODESPEEDY

    PROGRAM:

    class Affin
    { 
      static int a = 17; 
      static int b = 20; 
    
      static String Message(char[] msg) 
      { 
        String cipher = ""; 
        for (int i = 0; i < msg.length; i++) 
        { 
          if (msg[i] != ' ') 
          { 
            cipher = cipher 
                + (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A'); 
          }
          else 
          { 
            cipher += msg[i]; 
          } 
        } 
        return cipher; 
      } 
    
      static String Cipher(String cipher) 
      { 
        String msg = ""; 
        int a_inv = 0; 
        int flag = 0; 
        for (int i = 0; i < 26; i++) 
        { 
          flag = (a * i) % 26; 
          if (flag == 1) 
          { 
            a_inv = i; 
          } 
        } 
        for (int i = 0; i < cipher.length(); i++) 
        { 
          if (cipher.charAt(i) != ' ') 
          { 
            msg = msg + (char) (((a_inv * 
                ((cipher.charAt(i) + 'A' - b)) % 26)) + 'A'); 
          } 
          else 
          { 
            msg += cipher.charAt(i); 
          } 
        } 
    
        return msg; 
      } 
      public static void main(String[] args) 
      { 
        String msg = "CODESPEEDY"; 
        String cipherText = Message(msg.toCharArray()); 
        System.out.println("Encrypted Message is : " + cipherText);  
        System.out.println("Decrypted Message is: " + Cipher(cipherText)); 
    
      } 
    } 
    
    

Also read: Implement Caesar Cipher in Java

Leave a Reply

Your email address will not be published. Required fields are marked *