FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cryptography/src/cryptography/ciphers/keyword/Keyword.java at master · norkator/cryptography · GitHub
norkator
/
cryptography
Public
Notifications
You must be signed in to change notification settings
Fork
44
Star
222
Code
Issues
2
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
cryptography
/
src
/
cryptography
/
ciphers
/
keyword
/
Keyword.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
112 lines (96 loc) · 2.57 KB
Breadcrumbs
cryptography
/
src
/
cryptography
/
ciphers
/
keyword
/
Keyword.java
Copy path
File metadata and controls
112 lines (96 loc) · 2.57 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package
cryptography
.
ciphers
.
keyword
;
import
java
.
util
.
HashMap
;
import
cryptography
.
Mode
;
public
class
Keyword
{
public
static
void
main
(
String
[]
args
) {
}
public
static
String
keyword
(
String
input
,
String
key
,
Mode
mode
) {
input
=
input
.
toLowerCase
();
char
[]
table
=
createTable
(
removeDuplicates
(
key
.
toLowerCase
()));
StringBuilder
output
=
new
StringBuilder
();
if
(
mode
==
Mode
.
ENCRYPT
) {
for
(
int
i
=
0
;
i
<
input
.
length
();
i
++) {
output
.
append
(
getChar
(
input
.
charAt
(
i
),
table
,
true
,
true
));
}
}
if
(
mode
==
Mode
.
DECRYPT
) {
for
(
int
i
=
0
;
i
<
input
.
length
();
i
++) {
output
.
append
(
getChar
(
input
.
charAt
(
i
),
table
,
true
,
false
));
}
}
return
output
.
toString
().
toUpperCase
();
}
private
static
char
[]
createTable
(
String
key
) {
final
char
NULLCHAR
=
'\u0000'
;
char
[]
ALPHABET
=
"abcdefghijklmnopqrstuvwxyz"
.
toCharArray
();
char
[]
table
=
new
char
[
26
];
char
[]
alpha
=
ALPHABET
.
clone
();
char
[]
keyArr
=
key
.
toCharArray
();
for
(
int
i
=
0
;
i
<
keyArr
.
length
;
i
++) {
table
[
i
] =
keyArr
[
i
];
for
(
int
j
=
0
;
j
<
26
;
j
++) {
if
(
alpha
[
j
] ==
table
[
i
]) {
alpha
[
j
] =
NULLCHAR
;
break
;
}
}
}
int
index
=
keyArr
.
length
;
for
(
int
i
=
0
;
i
<
26
;
i
++) {
if
(
alpha
[
i
] !=
NULLCHAR
) {
table
[
index
] =
alpha
[
i
];
index
++;
}
}
return
table
;
}
private
static
String
removeDuplicates
(
String
in
) {
HashMap
<
Character
,
Boolean
>
used
=
new
HashMap
<
Character
,
Boolean
>();
char
[]
letters
=
in
.
toCharArray
();
String
out
=
""
;
for
(
char
letter
:
letters
) {
if
(!
used
.
containsKey
(
letter
)) {
out
+=
letter
;
used
.
put
(
letter
,
true
);
}
}
return
out
;
}
private
static
String
getChar
(
char
in
,
char
[]
table
,
boolean
keepCharacters
,
boolean
encode
) {
char
[]
ALPHABET
=
"abcdefghijklmnopqrstuvwxyz"
.
toCharArray
();
char
loweredIn
=
lowerChar
(
in
);
int
index
=
getIndex
(
loweredIn
,
encode
,
table
);
if
(
index
== -
1
) {
if
(
keepCharacters
)
return
charToString
(
in
);
else
return
""
;
}
else
if
(
encode
) {
return
charToString
(
table
[
index
]);
}
else
{
return
charToString
(
ALPHABET
[
index
]);
}
}
private
static
char
lowerChar
(
char
in
) {
return
Character
.
toLowerCase
(
in
);
}
private
static
String
charToString
(
char
in
) {
return
String
.
valueOf
(
in
);
}
private
static
int
getIndex
(
char
in
,
boolean
encode
,
char
[]
table
) {
char
[]
ALPHABET
=
"abcdefghijklmnopqrstuvwxyz"
.
toCharArray
();
if
(
encode
) {
for
(
int
i
=
0
;
i
<
26
;
i
++) {
if
(
ALPHABET
[
i
] ==
in
)
return
i
;
}
return
-
1
;
}
else
{
for
(
int
i
=
0
;
i
<
26
;
i
++) {
if
(
table
[
i
] ==
in
)
return
i
;
}
return
-
1
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL