// Programmed by : Rajeev Mishra
// Edited on :Notepad++
// Tested on :JDK 1.6
import java.util.Scanner;
import java.util.*;
class encrypt2
{
private int num;
public double[] encrypted()
{
int ctr=0,x=0;
double k,temp=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter a number (>=4) to encrypt:");
num=in.nextInt();
ArrayList
while(num>0)
{
x=num%10;
k=(x*7)/10.0;
a.add(new Double(k));
num/=10;
ctr++;
}
if(!(ctr>=4))
System.exit(0);
Object ai[]=a.toArray();
double arr[]=new double[ai.length];
for(int s=0,f=ai.length-1;s
{
arr[f]=((Double)ai[s]).doubleValue();
}
System.out.println("Initial modification:");
show(arr);
System.out.println("Exchange at even places");
if(!(arr.length%2==0))
{
for(int i=0;i
{
if(i%2==0)
{ temp=arr[i];
arr[i]=arr[i+2];
arr[i+2]=temp;
}
}
}
else
{
for(int i=0;i
{
if(i%2==0)
{ temp=arr[i];
arr[i]=arr[i+2];
arr[i+2]=temp;
}
}
}
show(arr);
System.out.println("Exchange at odd places");
if(!(arr.length%2==0))
{
for(int j=1;j
{
if(!(j%2==0))
{
temp=arr[j];
arr[j]=arr[j+2];
arr[j+2]=temp;
}
}
}
else
{
for(int j=1;j
{
if(!(j%2==0))
{
temp=arr[j];
arr[j]=arr[j+2];
arr[j+2]=temp;
}
}
}
show(arr);
System.out.println("***Encrypted no. is:");
show(arr);
return arr;
}
public void decrypt(double arr[])
{
double temp=0;int z=0;
System.out.println();
System.out.println("Decryption starts now....");
System.out.println("Exchange at odd places");
if(!(arr.length%2==0))
{
for(int j=arr.length-2;j>=3;j--)
{
if(!(j%2==0))
{
temp=arr[j];
arr[j]=arr[j-2];
arr[j-2]=temp;
}
}
}
else
{
for(int j=arr.length-1;j>=3;j--)
{
if(!(j%2==0))
{
temp=arr[j];
arr[j]=arr[j-2];
arr[j-2]=temp;
}
}
}
show(arr);
System.out.println("Exchange at even places");
if(!(arr.length%2==0))
{
for(int i=arr.length-1;i>=2;i--)
{
if(i%2==0)
{ temp=arr[i];
arr[i]=arr[i-2];
arr[i-2]=temp;
}
}
}
else
{
for(int i=arr.length-2;i>=2;i--)
{
if(i%2==0)
{ temp=arr[i];
arr[i]=arr[i-2];
arr[i-2]=temp;
}
}
}
show(arr);
System.out.println("***Decrypted no. is:");
for(int s=0;s
{
arr[s]= arr[s]*10/7;
System.out.printf("%.0f",arr[s]);
}
}
public void show(double arr[])
{
System.out.print(":");
for(int s=0;s
{
System.out.printf("%.0f",arr[s]);
}
System.out.println();
}
public static void main(String args[])
{
encrypt2 e =new encrypt2();
double []arr=e.encrypted();
System.out.println();
e.decrypt(arr);
}
}
thnx
ReplyDelete