FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java-Programs/AffineCipher.java at master · pooja2405/Java-Programs · GitHub
pooja2405
/
Java-Programs
Public
forked from
codec-akash/DSA-PS-Java-Programs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java-Programs
/
AffineCipher.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
89 lines (76 loc) · 2.99 KB
Breadcrumbs
Java-Programs
/
AffineCipher.java
Copy path
File metadata and controls
89 lines (76 loc) · 2.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
java
.
io
.*;
class
AffineCipher
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
File
input
=
new
File
(
"FILE PATH FOR INPUT"
);
File
output
=
new
File
(
"FILE PATH FOR OUTPUT"
);
BufferedReader
br1
=
new
BufferedReader
(
new
FileReader
(
input
));
BufferedWriter
or
=
new
BufferedWriter
(
new
FileWriter
(
output
));
System
.
out
.
println
(
"Select 1 for Encryption and 2 for decryption: "
);
int
choice
=
Integer
.
parseInt
(
br
.
readLine
());
if
(
choice
==
1
) {
String
pt
=
br1
.
readLine
();
System
.
out
.
print
(
"Enter the value of a in Encryption formula (must be coprime to 26): "
);
int
a
=
Integer
.
parseInt
(
br
.
readLine
());
if
(!
isCoprime
(
a
,
26
)) {
System
.
out
.
println
(
"!!Enter a value CoPrime to 26!!
\n
Run Again with proper value of A"
);
}
else
{
System
.
out
.
print
(
"Enter the value of b in Encryption formula: "
);
int
b
=
Integer
.
parseInt
(
br
.
readLine
());
or
.
write
(
"Encrypted Ciphertext is: "
+
encryptMessage
(
pt
.
toCharArray
(),
a
,
b
));
}
}
else
{
String
pt
=
br1
.
readLine
();
System
.
out
.
print
(
"Enter the value of a in Encryption formula (must be coprime to 26): "
);
int
a
=
Integer
.
parseInt
(
br
.
readLine
());
if
(!
isCoprime
(
a
,
26
)) {
System
.
out
.
println
(
"!!Enter a value CoPrime to 26!!
\n
Run Again with proper value of A"
);
}
else
{
System
.
out
.
print
(
"Enter the value of b in Encryption formula: "
);
int
b
=
Integer
.
parseInt
(
br
.
readLine
());
or
.
write
(
"Decrypted Plaintext is: "
+
decryptCipher
(
pt
,
a
,
b
));
}
}
or
.
close
();
}
static
String
encryptMessage
(
char
[]
msg
,
int
a
,
int
b
) {
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
decryptCipher
(
String
cipher
,
int
a
,
int
b
) {
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
;
}
static
int
gcd
(
int
a
,
int
b
) {
if
(
b
==
0
)
return
a
;
return
gcd
(
b
,
a
%
b
);
}
static
boolean
isCoprime
(
int
a
,
int
b
){
if
(
gcd
(
a
,
b
) ==
1
)
return
true
;
return
false
;
}
}
Back
|
FazBrowse Home
|
New Git URL