-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBase64Crypto.java
More file actions
40 lines (35 loc) · 2.04 KB
/
Base64Crypto.java
File metadata and controls
40 lines (35 loc) · 2.04 KB
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
import java.util.Base64;
public class Base64Crypto
{
static final String SEPARATOR = "----------------------------------------";
static String[] tests = new String[] { "Hello" , "This is a test" , "I want to encode me then decode me :D" };
public static void main ( String[] args )
{
for ( String test : tests )
{
String encoded = encrypt ( test );
String decrypted = decrypt ( encoded );
System.out.printf ( "%s%n%s -> %s%n%s -> %s%n" , SEPARATOR , test , encoded , encoded , decrypted );
}
}
static String encrypt ( String plainText )
{
return Base64.getEncoder ().encodeToString ( plainText.getBytes () );
}
static String decrypt ( String base64 )
{
return new String ( Base64.getDecoder ().decode ( base64 ) );
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Output //
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Hello -> SGVsbG8= //
// SGVsbG8= -> Hello //
// ---------------------------------------- //
// This is a test -> VGhpcyBpcyBhIHRlc3Q= //
// VGhpcyBpcyBhIHRlc3Q= -> This is a test //
// ---------------------------------------- //
// I want to encode me then decode me :D -> SSB3YW50IHRvIGVuY29kZSBtZSB0aGVuIGRlY29kZSBtZSA6RA== //
// SSB3YW50IHRvIGVuY29kZSBtZSB0aGVuIGRlY29kZSBtZSA6RA== -> I want to encode me then decode me :D //
//////////////////////////////////////////////////////////////////////////////////////////////////////////
}