class
GFG
{
static
final
int
SIZE =
100
;
static
String base64Decoder(
char
encoded[],
int
len_str)
{
char
[]decoded_String;
decoded_String =
new
char
[SIZE];
int
i, j, k =
0
;
int
num =
0
;
int
count_bits =
0
;
for
(i =
0
; i < len_str; i +=
4
)
{
num =
0
; count_bits =
0
;
for
(j =
0
; j <
4
; j++)
{
if
(encoded[i + j] !=
'='
)
{
num = num <<
6
;
count_bits +=
6
;
}
if
(encoded[i + j] >=
'A'
&& encoded[i + j] <=
'Z'
)
num = num | (encoded[i + j] -
'A'
);
else
if
(encoded[i + j] >=
'a'
&& encoded[i + j] <=
'z'
)
num = num | (encoded[i + j] -
'a'
+
26
);
else
if
(encoded[i + j] >=
'0'
&& encoded[i + j] <=
'9'
)
num = num | (encoded[i + j] -
'0'
+
52
);
else
if
(encoded[i + j] ==
'+'
)
num = num |
62
;
else
if
(encoded[i + j] ==
'/'
)
num = num |
63
;
else
{
num = num >>
2
;
count_bits -=
2
;
}
}
while
(count_bits !=
0
) {
count_bits -=
8
;
decoded_String[k++] = (
char
)
((num >> count_bits) &
255
);
}
}
return
String.valueOf(decoded_String);
}
public
static
void
main(String[] args)
{
char
encoded_String[] =
"TUVOT04="
.toCharArray();
int
len_str = encoded_String.length;
len_str -=
1
;
System.out.printf(
"Encoded String : %s\n"
,
String.valueOf(encoded_String));
System.out.printf(
"Decoded_String : %s\n"
,
base64Decoder(encoded_String, len_str));
}
}