Coders Packet

Square Encryption of String In Java

By Kirti Sandip Phegade

Today we write a program that performs some encryption and decryption tasks on a given string by using the Java programming language

we have a string S, the task is to encrypt as well as decrypt the given string again to the original form.

Input: “java object oriented programming language

Output: jboegngajrdrguvei*a*aacepmlg*tnrmaeo*toin.

Program

public class SquareEncrypt
{
    public static void main(String[] nt)
    {
        String a_string = "java object oriented programming language";

        if(a_string.length()%6!=0)
        {

            int x=a_string.length()%6;
            for (int u=1;u<=6-x;u++)
            {
                a_string=a_string+'.';                  //completing string
            }
        }
        String x=a_string.replace(' ', '*');            //replacing space with *

        int number=x.length()/6;
        char[][] encrypted=new char[number][6];            //array to get encrypted message
        int i=0,k=0;
        while(k!=x.length())
        {
            for(int j=0;j<6;j++)
            {
                encrypted[i][j]=x.charAt(k);
                k++;
            }
            i++;
        }

        System.out.println();

        for(int j=0;j<6;j++)
        {
            for(int s=0;s<i;s++)
            {
                System.out.print(encrypted[s][j]);
            }
        }

        System.out.println("\n\n");
    }
}

 

 

Steps:-

1) Fing length of String ( java object oriented programming language )

2) create the square matrix of given string

   j a v a * o

   b j e c t *

   o r i e n t

   e d * p r o

   g r a m m i

   n g * l a n

   g u a g e .

3) Read the matrix column-wise to get the encrypted string.

4) print encrypted string

Output

jboegngajrdrguvei*a*aacepmlg*tnrmaeo*toin.

 

 

Download Complete Code

Comments

No comments yet