01.
public
class
EncryptUtils {
02.
public
static
final
String DEFAULT_ENCODING=
"UTF-8"
;
03.
static
BASE64Encoder enc=
new
BASE64Encoder();
04.
static
BASE64Decoder dec=
new
BASE64Decoder();
05.
06.
public
static
String base64encode(String text){
07.
try
{
08.
String rez = enc.encode( text.getBytes( DEFAULT_ENCODING ) );
09.
return
rez;
10.
}
11.
catch
( UnsupportedEncodingException e ) {
12.
return
null
;
13.
}
14.
}
15.
16.
public
static
String base64decode(String text){
17.
18.
try
{
19.
return
new
String(dec.decodeBuffer( text ),DEFAULT_ENCODING);
20.
}
21.
catch
( IOException e ) {
22.
return
null
;
23.
}
24.
25.
}
26.
27.
public
static
void
main(String[] args){
28.
String txt=
"some text to be encrypted"
;
29.
String key=
"key phrase used for XOR-ing"
;
30.
System.out.println(txt+
" XOR-ed to: "
+(txt=xorMessage( txt, key )));
31.
String encoded=base64encode( txt );
32.
System.out.println(
" is encoded to: "
+encoded+
" and that is decoding to: "
+ (txt=base64decode( encoded )));
33.
System.out.print(
"XOR-ing back to original: "
+xorMessage( txt, key ) );
34.
35.
}
36.
37.
public
static
String xorMessage(String message, String key){
38.
try
{
39.
if
(message==
null
|| key==
null
)
return
null
;
40.
41.
char
[] keys=key.toCharArray();
42.
char
[] mesg=message.toCharArray();
43.
44.
int
ml=mesg.length;
45.
int
kl=keys.length;
46.
char
[] newmsg=
new
char
[ml];
47.
48.
for
(
int
i=
0
; i<ml; i++){
49.
newmsg[i]=(
char
)(mesg[i]^keys[i%kl]);
50.
}
51.
mesg=
null
; keys=
null
;
52.
return
new
String(newmsg);
53.
}
54.
catch
( Exception e ) {
55.
return
null
;
56.
}
57.
}
58.
59.
}