2018년 4월 4일 수요일

[Java] Base64 Encoding, Decoding Examples


1. Base64 Encoding, Decoding with java.util.Base64

1
2
3
4
5
6
7
8
9
static void testBase64Jdk() {
        String s = "Test Str";
        Encoder e = java.util.Base64.getEncoder();
        Decoder d = java.util.Base64.getDecoder();
        byte[] eb = e.encode(s.getBytes());
        byte[] db = d.decode(eb);
        System.out.println(new String(eb));
        System.out.println(new String(db));
    }


Result:
VGVzdCBTdHI=
Test Str



2. Base64 Encoding, Decoding with org.apache.commons.codec.binary.Base64


1
2
3
4
5
6
7
static void testBase64Commons() {
        String s = "Test Str";
        byte[] eb = Base64.encodeBase64(s.getBytes());
        byte[] db = Base64.decodeBase64(eb);
        System.out.println(new String(eb));
        System.out.println(new String(db));        
    }


Result:
VGVzdCBTdHI=
Test Str



댓글 없음:

댓글 쓰기