CODING JOY

The stunning potpourri of coding and mundane life

AFFINE CIPHER IN JAVA



AMRUHA AHMED
3rd April,2024.


Affine cipher is a simple encryption technique that follows monoalphabetic substitution. This cipher converts plaintext to ciphertext by making use of the numerical equivalent of alphabets . The formula used for encryption is:

ciphertext=((a*plaintext)+b)%26

Where the plaintext refers to numerical equivalent of the plaintext alphabet(such as 0 for A, 1 for B and so on ). Ciphertext refers to the numerical equivalent of the ciphertext alphabet. 'a' is the multiplier and 'b' is the additive shift value. Both 'a' and 'b' serve as keys to this encryption technique.

The value obtained from ciphertext will be converted to the equivalent character.

Though relatively easy to understand and implement, it is shadowed by more secure encryption techniques.

blog1

ARRAYS USED IN THE AFFINE CIPHER PROGRAM:

VARIABLES IN AFFINE CIPHER PROGRAM:

ALGORITHM FOR AFFINE CIPHER:


PROGRAM:

                     
                    import java.util.*;
                    class affinecipher
                    {
                        Scanner ob=new Scanner(System.in);
                        void main()
                        {
                            int a,b;// a- multiplier, b-additive shift value
                            String plaintext,ciphertext="";//to store plaintext and cipher text respectively
                            int i,j;//i and j are loop counters
                            int cc,pos;//cc- storing ciphertext of current letter, pos-stores numerical equivalent of character
                            char start='A';//starting character in alpha array
                            int number[]=new int[26];//array of numerical equivalent of alphabets
                            char alpha[]=new char[26];//array of alphabets from A to Z
                            System.out.print("Enter the multiplier 'a':");
                            a=ob.nextInt();
                            System.out.print("Enter the additive shift 'b':");
                            b=ob.nextInt();
                            System.out.print("Enter the plaintext:");
                            plaintext=ob.next(); 
                            plaintext=plaintext.toUpperCase();
                            for(i=0;i<26;i++)
                            {//storing current alphabet in alpha and its numerical equivalent in number
                                number[i]=i;
                                alpha[i]=start;
                                start++;
                            }
                            for(j=0;j<plaintext.length();j++)
                            {
                                char c=plaintext.charAt(j); //getting the current character of plaintext
                                pos=0;
                                for(i=0;i<alpha.length;i++)
                                {//finding numerical equivalent of 'c'
                                  if(c==alpha[i])
                                  {
                                      pos=i;
                                      break;
                                    }                
                                }
                                cc=((a*pos)+b)%26;//calculating ciphertext of alphabet 'c'     
                                ciphertext=ciphertext+""+alpha[cc];//updating the ciphertext string         
                            }
                            System.out.println("The cipher text is "+ciphertext);
                        }
                    }
                    
             
         
CODE COPIED

DRY RUN:

Suppose if the user enters plaintext as "SECRET", a as 5 and b as 8 , then the computation is carried out as follows: blog1

The final cipher text obtained is UCSPCZ

OUTPUT:

Enter the multiplier 'a':5

Enter the additive shift 'b':8

Enter the plaintext:SECRET

The cipher text is UCSPCZ