티스토리 뷰
왜?
일반인의 관점에서 암호화라면 암호화긴 하지만...
컴퓨터 사이언스 관점에서 암호화라고 하면 의미가 다르다.
Base64는 'Encode코드화/Decode복원'이다.
전송시 깨짐을 방지하고 문자열의 완결을 보장하기 위해 사용하는 것이고 다른사람이 알아보지 못 하도록 하는 암호화는 아니다.
기술설명은 생략.
Sample 3가지
- Java 기본 패키지
- Apache codec
testCompile group: 'commons-codec', name: 'commons-codec', version: '1.11' - Spring core
testCompile group: 'org.springframework', name: 'spring-core', version: '5.1.0.RELEASE'
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class Base64Test {
//given global
private static final String source = "username:password";
//given each
@Before
public void before() {
System.out.println(source);
}
}
// 1
// java.util.Base64
// @Test
public void base64_javautil() {
//when
byte[] encoded = java.util.Base64.getEncoder().encode(source.getBytes());
System.out.println(new String(encoded));
byte[] decoded = java.util.Base64.getDecoder().decode(encoded);
System.out.println(new String(decoded));
//then
System.out.println(source.equals(new String(decoded)));
Assert.assertNotEquals(source, new String(encoded));
Assert.assertEquals(source, new String(decoded));
}
// 2
// org.apache.commons.codec.binary.Base64
@Test
public void base64_apache_commons_codec() {
//when
byte[] encoded = org.apache.commons.codec.binary.Base64.encodeBase64(source.getBytes());
System.out.println(new String(encoded));
byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(encoded);
System.out.println(new String(decoded));
//then
System.out.println(source.equals(new String(decoded)));
Assert.assertNotEquals(source, new String(encoded));
Assert.assertEquals(source, new String(decoded));
}
// 3
// org.springframework.util.Base64Utils
@Test
public void base64_spring_util() {
//when
byte[] encoded = org.springframework.util.Base64Utils.encode(source.getBytes());
System.out.println(new String(encoded));
byte[] decoded = org.springframework.util.Base64Utils.decode(encoded);
System.out.println(new String(decoded)); //then
System.out.println(source.equals(new String(decoded)));
Assert.assertNotEquals(source, new String(encoded));
Assert.assertEquals(source, new String(decoded));
}
}
'Tech' 카테고리의 다른 글
Golang - Base64(Encoding, Decoding) Sample Code (0) | 2023.02.14 |
---|---|
Java - Url(Encoding, Decoding) Sample Code (0) | 2023.02.14 |
Scala - Hello World + Gradle Setting (0) | 2018.10.02 |
Kotlin - Hello World + Gradle Setting (0) | 2018.10.02 |
JavaScript - open source CDN (0) | 2018.10.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- BASE64
- index
- enum
- Knowhow
- vscode
- Kotlin
- Algorithm
- ActiveRecord
- Regex
- git
- error
- golang
- Source
- HelloWorld
- 노가다
- ChatGPT
- urldecode
- 사용법
- RubyOnRails
- Mac
- JQuery
- spring
- version
- Scala
- cdn
- gradle
- sample
- URLEncode
- Snippet
- js
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함