업무관련

AES256 암복호화 키값 변환(BYTE TO HEX)

IronAreum 2024. 8. 19. 15:49
728x90

신규 서비스 암복화 개발시 

Key와 iv값을 주고 받는데 

보통 byte 혹은 hex btye로 데이터를 주고받는데

데이터 변환시 참고하자 ! 

 

public static void main(String[] args) {
    String str = "test1234 한글포함";
    System.out.println("입력 : " + str);
    
    byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
    StringBuilder result = new StringBuilder();
    for (byte b : bytes) {
        result.append(String.format("%02X", b));
    }
    String hexStr = result.toString();
    System.out.println("HEX : " + hexStr);
    
    
    int len = hexStr.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(hexStr.charAt(i), 16) << 4)
                             + Character.digit(hexStr.charAt(i+1), 16));
    }
    
    String org = new String(data, StandardCharsets.UTF_8);
    System.out.println("ORG : " + org);
    
}

result

입력 : test1234 한글포함
HEX : 746573743132333420ED959CEAB880ED8FACED95A8
ORG : test1234 한글포함

 

728x90

 

https://emunhi.com/view/202110/25100131085?menuNo=10000

 

emunhi

public static void main(String[] args) {     String str = "test1234 한글포함";     System.out.println("입력 : " + str);          byte[] bytes = str.getBytes(StandardCharsets.UTF_8);     StringBuilder result = new StringBuilder();   

emunhi.com

 

728x90