-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.java
More file actions
155 lines (144 loc) · 3.38 KB
/
Encryption.java
File metadata and controls
155 lines (144 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.*;
import java.lang.Math;
public class Encryption
{
double theta,alpha,base;
double x,y,x_,y_,xnext,ynext;
int xx,yy,key,n,l;
char a,b;
double[] Encrypt(String plain, Encryption en)
{
en.l=plain.length();
double ciph[]=new double[en.l];
for(int i=0;i<en.l;i+=2)
{
en.x=(int)plain.charAt(i);
en.y=(int)plain.charAt(i+1);
//System.out.print(en.x+" "+en.y+" ");
if(i<en.l-2)
{
en.xnext=(int)plain.charAt(i+2);
en.ynext=(int)plain.charAt(i+3);
}
else
{
en.xnext=en.key;
en.ynext=en.key;
}
//System.out.println(en.x+" "+en.y+" ");
en.x_+=en.xnext;
en.y_+=en.ynext;
en.x_=(en.x*Math.cos(en.theta))-(en.y*Math.sin(en.theta));
en.y_=(en.x*Math.sin(en.theta))+(en.y*Math.cos(en.theta));
//System.out.print(en.x_+" "+en.y_+" ");
ciph[i]=en.x_;
ciph[i+1]=en.y_;
}
System.out.println();
return ciph;
}
public static void main(String [] deep)
{
int c;
Scanner sc=new Scanner(System.in);
Dcryption dry = new Dcryption();
Encryption ery = new Encryption();
Encryption en=new Encryption();
System.out.print("enter key value :");
en.key=sc.nextInt();
for(int i=en.key+1;;i++)
{
c=0;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
c++;
}
if(c==0)
{
en.n=i;
break;
}
}
en.base=en.key^en.n;
en.theta=Math.toRadians(en.n%360);
en.alpha=Math.toRadians(360-en.n);
System.out.print("enter key plain text :");
String plain=sc.next();
en.l=plain.length();
double ciph[]=new double[en.l];
ciph=ery.Encrypt(plain,en);
System.out.print("Cipher text :");
for(int i=0;i<en.l;i++)
{
System.out.print(ciph[i]+" ");
}
System.out.println();
String deciph=dry.Decrypt(ciph,en);
System.out.println("DeCipher text :"+deciph);
sc.close();
}
}
class Dcryption
{
static double[] reverse(double a[], int n)
{
double[] b = new double[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
return b;
}
String Decrypt(double ciph[],Encryption en)
{
String decipher="";
char deci[] = new char[en.l];
reverse(ciph, en.l);
/*for(int i=0;i<en.l;i+=2)
{
en.x=ciph[i];
en.y=ciph[i+1];
en.x_=(en.x*Math.cos(en.alpha))-(en.y*Math.sin(en.alpha));
en.y_=(en.x*Math.sin(en.alpha))+(en.y*Math.cos(en.alpha));
//System.out.println(en.x_+" "+en.y_+" ");
en.xx=(int)Math.round(en.x_);
en.yy=(int)Math.round(en.y_);
//System.out.println(en.xx+" "+en.yy+" ");
en.a=(char)en.xx;
en.b=(char)en.yy;
//System.out.println(en.a+" "+en.b+" ");
decipher+=en.a;
decipher+=en.b;
}*/
for(int i=0;i<en.l;i+=2)
{
en.x=ciph[i];
en.y=ciph[i+1];
if(i==0)
{
en.xnext=en.key;
en.ynext=en.key;
}
else
{
en.xnext=(int)decipher.charAt(i-2);//ciph[i+2];
en.ynext=(int)decipher.charAt(i-1);//ciph[i+3];
}
//System.out.println();
//System.out.println(en.xnext+" "+en.ynext);
en.x_=(en.x*Math.cos(en.alpha))-(en.y*Math.sin(en.alpha));
en.y_=(en.x*Math.sin(en.alpha))+(en.y*Math.cos(en.alpha));
en.x-=en.xnext;
en.y-=en.ynext;
en.xx=(int)Math.round(en.x_);
en.yy=(int)Math.round(en.y_);
en.a=(char)en.xx;
en.b=(char)en.yy;
decipher+=en.a;
decipher+=en.b;
}
return decipher;
}
}