-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase64_encode.java
332 lines (265 loc) · 9.91 KB
/
base64_encode.java
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
+-----------------------------------------------------------------------------------------------+
| A simple Java programme for base64 encoding |
+-----------------------------------------------------------------------------------------------+
| Copyright ( c ) 2013 Anjan Borah < [email protected] > |
+-----------------------------------------------------------------------------------------------+
| Author - Anjan Borah |
+-----------------------------------------------------------------------------------------------+
| GitHub - https://github.com/anjanborah/Base64_Encode |
+-----------------------------------------------------------------------------------------------+
*/
//----------[ Import Section ]---------------------------------------------------------------------
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
//----------[ Class base64_encode ]----------------------------------------------------------------
public class base64_encode
{
public static void main( String args[] )
{
try {
Scanner _get = new Scanner( System.in );
System.out.print( " ##### # \n" );
System.out.print( "##### ## #### ###### # # # # \n" );
System.out.print( "# # # # # # # # # \n" );
System.out.print( "##### # # #### ##### ###### ####### \n" );
System.out.print( "# # ###### # # # # # \n" );
System.out.print( "# # # # # # # # # # \n" );
System.out.print( "##### # # #### ###### ##### # \n\n" );
System.out.print( "###### # # #### #### ##### ###### \n" );
System.out.print( "# ## # # # # # # # # \n" );
System.out.print( "##### # # # # # # # # ##### \n" );
System.out.print( "# # # # # # # # # # \n" );
System.out.print( "# # ## # # # # # # # \n" );
System.out.print( "###### # # #### #### ##### ###### \n\n" );
System.out.print( "Enter the raw string--------------------------\n\n" );
String _raw_string = _get.nextLine();
Base64Encode _object = new Base64Encode( _raw_string );
System.out.print( "\nEncoded String--------------------------------\n\n" + _object.get_encoded_string() + "\n" );
} catch( Exception exception ) {
System.out.println( "Exception - " + exception.toString() );
}
}
}
//----------[ Class Base64Encode ]-----------------------------------------------------------------
class Base64Encode
{
public Base64Encode( String _raw_string )
{
try {
this.start_engine( _raw_string );
} catch( Exception exception ) {
System.out.println( "Exception - " + exception.toString() );
}
}
private void start_engine( String _raw_string ) throws Exception
{
this._raw_string = _raw_string;
this._raw_string_length = this.get_string_length( this._raw_string );
this._append_needed = this.is_append_needed( this._raw_string_length );
this.populate_eight_bit_array();
this.populate_six_bit_array();
this.populate_base64_values();
this.create_encoded_string();
}
private int get_string_length( String _string )
{
return _string.length();
}
private boolean is_append_needed( int _length )
{
boolean _append_needed;
if( _length % 3 == 0 ) {
_append_needed = false;
this._append_count = 0;
} else {
_append_needed = true;
this._append_count = 3 - ( _length % 3 );
}
return _append_needed;
}
private void populate_eight_bit_array()
{
for( int i=0; i<( this._raw_string_length + this._append_count ); i++ ) {
if( i >= this._raw_string_length ) {
this._eight_bit_array.add( "00000000" );
} else {
try {
this._eight_bit_array.add( this.get_eight_bit_string( ( int )this._raw_string.charAt( i ) ) );
} catch( Exception exception ) {
System.out.println( "Exception - " + exception.toString() );
}
}
}
}
private String get_eight_bit_string( int _ASCII ) throws Exception
{
String _eight_bit_string = "";
int _zero_or_one;
while( true ) {
_zero_or_one = _ASCII % 2;
if( _zero_or_one == 1 ) {
_eight_bit_string = "1" + _eight_bit_string;
_ASCII = _ASCII / 2;
if( _ASCII == 0 ) {
break;
} else {
continue;
}
} else {
_eight_bit_string = "0" + _eight_bit_string;
_ASCII = _ASCII / 2;
if( _ASCII == 0 ) {
break;
} else {
continue;
}
}
}
int _eight_bit_string_length = _eight_bit_string.length();
if( _eight_bit_string_length < 8 ) {
for( int i=0; i<( 8 - _eight_bit_string_length ); i++ ) {
_eight_bit_string = "0" + _eight_bit_string;
}
}
return _eight_bit_string;
}
private void populate_six_bit_array() throws Exception
{
String _long_eight_bit_string = "";
Iterator< String > _it = this._eight_bit_array.iterator();
while( _it.hasNext() ) {
_long_eight_bit_string = _long_eight_bit_string + _it.next();
}
for( int i=0; i<( _long_eight_bit_string.length() / 6 ); i++ ) {
int m, n;
m = ( 6 * i );
n = m + 6;
this._six_bit_array.add( _long_eight_bit_string.substring( m, n ) );
}
}
private void populate_base64_values() throws Exception
{
for( String _six_bit_string : this._six_bit_array ) {
this._base64_values.add( this.get_base64_index_value( _six_bit_string ) );
}
}
private String get_base64_index_value( String _string )
{
int _base64_value = 0;
for( int i=0; i<_string.length(); i++ ) {
if( _string.charAt( i ) == '1' ) {
_base64_value = _base64_value + this.my_pow( 2, ( 6 - ( i + 1 ) ) );
}
}
return String.valueOf( _base64_value );
}
private int my_pow( int _base, int _power )
{
int _raise = 0;
if( _power == 0 ) {
_raise = 1;
} else {
int _temp = 1;
for( int i=1; i<=_power; i++ ) {
_temp = _temp * _base;
}
_raise = _temp;
}
return _raise;
}
private void create_encoded_string()
{
this._final_encoded_string = "";
for( String _base64_value : this._base64_values ) {
this._final_encoded_string = this._final_encoded_string + String.valueOf( this.base64_index_table[ Integer.parseInt( _base64_value ) ] );
}
if( this._append_needed == true ) {
this._final_encoded_string = this._final_encoded_string.substring( 0, ( this._final_encoded_string.length() - this._append_count) );
for( int i=1; i<=this._append_count; i++ ) {
this._final_encoded_string = this._final_encoded_string + "=";
}
}
}
public String get_encoded_string()
{
return this._final_encoded_string;
}
private String _raw_string;
private int _raw_string_length;
private boolean _append_needed;
private int _append_count;
private List< String > _eight_bit_array = new ArrayList< String >();
private List< String > _six_bit_array = new ArrayList< String >();
private List< String > _base64_values = new ArrayList< String >();
private String _final_encoded_string;
private char base64_index_table[] = {
'A', /* Index number - 0 */
'B', /* Index number - 1 */
'C', /* Index number - 2 */
'D', /* Index number - 3 */
'E', /* Index number - 4 */
'F', /* Index number - 5 */
'G', /* Index number - 6 */
'H', /* Index number - 7 */
'I', /* Index number - 8 */
'J', /* Index number - 9 */
'K', /* Index number - 10 */
'L', /* Index number - 11 */
'M', /* Index number - 12 */
'N', /* Index number - 13 */
'O', /* Index number - 14 */
'P', /* Index number - 15 */
'Q', /* Index number - 16 */
'R', /* Index number - 17 */
'S', /* Index number - 18 */
'T', /* Index number - 19 */
'U', /* Index number - 20 */
'V', /* Index number - 21 */
'W', /* Index number - 22 */
'X', /* Index number - 23 */
'Y', /* Index number - 24 */
'Z', /* Index number - 25 */
'a', /* Index number - 26 */
'b', /* Index number - 27 */
'c', /* Index number - 28 */
'd', /* Index number - 29 */
'e', /* Index number - 30 */
'f', /* Index number - 31 */
'g', /* Index number - 32 */
'h', /* Index number - 33 */
'i', /* Index number - 34 */
'j', /* Index number - 35 */
'k', /* Index number - 36 */
'l', /* Index number - 37 */
'm', /* Index number - 38 */
'n', /* Index number - 39 */
'o', /* Index number - 40 */
'p', /* Index number - 41 */
'q', /* Index number - 42 */
'r', /* Index number - 43 */
's', /* Index number - 44 */
't', /* Index number - 45 */
'u', /* Index number - 46 */
'v', /* Index number - 47 */
'w', /* Index number - 48 */
'x', /* Index number - 49 */
'y', /* Index number - 50 */
'z', /* Index number - 51 */
'0', /* Index number - 52 */
'1', /* Index number - 53 */
'2', /* Index number - 54 */
'3', /* Index number - 55 */
'4', /* Index number - 56 */
'5', /* Index number - 57 */
'6', /* Index number - 58 */
'7', /* Index number - 59 */
'8', /* Index number - 60 */
'9', /* Index number - 61 */
'+', /* Index number - 62 */
'/' /* Index number - 63 */
};
}
//-------------------------------------------------------------------------------------------------