-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Re-4aliens/feat/#14_encod
Feat/#14 대칭키 암호화, 복호화 기능 구현
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/com/aliens/backend/encode/EncoderProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.aliens.backend.encode; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.crypto.spec.SecretKeySpec; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
@Component | ||
public class EncoderProperties { | ||
|
||
@Value("${encode.symmetric.key}") | ||
private String secretKey; | ||
|
||
private SecretKeySpec encodeKey; | ||
|
||
@PostConstruct | ||
public void init() { | ||
byte[] byteKey = secretKey.getBytes(StandardCharsets.UTF_8); | ||
encodeKey = new SecretKeySpec(byteKey, "AES"); | ||
} | ||
|
||
public SecretKeySpec getEncodeKey() { | ||
return encodeKey; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/aliens/backend/encode/SymmetricKeyEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.aliens.backend.encode; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import javax.crypto.Cipher; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
@Component | ||
public class SymmetricKeyEncoder { | ||
|
||
private final EncoderProperties encoderProperties; | ||
|
||
public SymmetricKeyEncoder(final EncoderProperties encoderProperties) { | ||
this.encoderProperties = encoderProperties; | ||
} | ||
|
||
public String encrypt(String plaintext) throws Exception { | ||
Cipher cipher = Cipher.getInstance("AES"); | ||
cipher.init(Cipher.ENCRYPT_MODE, encoderProperties.getEncodeKey()); | ||
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8)); | ||
return Base64.getEncoder().encodeToString(encryptedBytes); | ||
} | ||
|
||
public String decrypt(String ciphertext) throws Exception { | ||
Cipher cipher = Cipher.getInstance("AES"); | ||
cipher.init(Cipher.DECRYPT_MODE, encoderProperties.getEncodeKey()); | ||
byte[] decodedBytes = Base64.getDecoder().decode(ciphertext); | ||
byte[] decryptedBytes = cipher.doFinal(decodedBytes); | ||
return new String(decryptedBytes, StandardCharsets.UTF_8); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/com/aliens/backend/encode/SymmetricKeyEncoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.aliens.backend.encode; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class SymmetricKeyEncoderTest { | ||
|
||
@Autowired | ||
private SymmetricKeyEncoder symmetricKeyEncoder; | ||
|
||
@Test | ||
@DisplayName("문자열 암호화 성공") | ||
void encryptTest() throws Exception { | ||
//Given | ||
String givenInput = "testText"; | ||
|
||
//When | ||
String result = symmetricKeyEncoder.encrypt(givenInput); | ||
|
||
//Then | ||
Assertions.assertNotEquals(result, givenInput); | ||
} | ||
|
||
@Test | ||
@DisplayName("문자열 복호화 성공") | ||
void decryptTest() throws Exception { | ||
//Given | ||
String givenInput = "testText"; | ||
String code = symmetricKeyEncoder.encrypt(givenInput); | ||
|
||
//When | ||
String result = symmetricKeyEncoder.decrypt(code); | ||
|
||
//Then | ||
Assertions.assertEquals(result, givenInput); | ||
} | ||
} |